Search code examples
c#.netazureazure-functionsgithub-actions

Azure Functions in .NET 7 (isolated) published to Azure, 0 functions loaded


I'm trying to deploy an Azure Function (isolated) with .NET 7 to MS Azure using a GitHub Actions workflow. Everything seems to run fine, but for some reason, the Function is not loaded when I deploy it to the cloud. When I run the function local host, everything is fine (works on my machine), once deployed it doesn't.

public class DemoFunction
{
    private readonly ILogger _logger;

    public DemoFunction(ILoggerFactory loggerFactory)
    {
        _logger = loggerFactory.CreateLogger<DemoFunction>();
    }

    [Function("DemoFunction")]
    public HttpResponseData Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestData req)
    {
        var response = req.CreateResponse(HttpStatusCode.OK);
        response.Headers.Add("Content-Type", "text/plain; charset=utf-8");
        response.WriteString("Demo function works!");

        return response;
    }
}

The Function App contains only a single function with an HTTP binding (no authentication, anonymous requests allowed). I use Bicep to deploy infra and GH Actions to deploy the Function App. Log Analytics shows this: enter image description here

So for some reason it does find the function, but not load it. If I navigate to the Functions blade in the Azure Portal I don't see any function. The project is just a POC project and hosted publicly available at https://github.com/nikneem/function-deployment-with-gh-actions

Any ideas?


Solution

  • Oh wow, I found the answer in the deployment process. In my GitHub Actions workflow, I did a dotnet publish of the Azure Functions project to an output folder. I zipped the content of the output folder and published that zip file as a workflow artifact.

    Then in the deployment, I downloaded the workflow artifact (e.g. that zip file) and did a Zip Deployment using that zip file.

    Now apparently, something went wrong with this zip file, so I removed that manual zip action, mainly because I found out the deployment step in the GitHub Action also works when you pass a folder containing all published files in.

    So the publish step in my GH Actions workflow now looks like this:

    publish-functions:
      runs-on: ubuntu-latest
      needs: versionize
      steps:
        - uses: actions/checkout@v3
        - uses: actions/setup-dotnet@v3
          with:
            dotnet-version: "7.0.x"
        - name: Restore packages
          working-directory: src
          run: dotnet restore
        - name: Publish functions app
          working-directory: src
          run: dotnet publish $FUNCTIONS_PATH/$FUNCTIONS_PROJECT -c Release --no-restore -o functionsout /p:Version=${{needs.versionize.outputs.semver}}
        - name: Upload functions artifact
          uses: actions/upload-artifact@v3
          with:
            name: player-functions
            path: src/functionsout/*
    

    and the deployment step looks like this:

    deploy-function-app:
      runs-on: ubuntu-latest
      needs: [publish-functions, deploy-infrastructure-prod]
      steps:
        - uses: actions/download-artifact@v3
          with:
            name: player-functions
            path: function
        - uses: azure/login@v1
          with:
            creds: ${{secrets.AZURE_PROD}}
        - name: Deploy Azure Functions app
          uses: Azure/functions-action@v1
          with:
            app-name: ${{needs.deploy-infrastructure-prod.outputs.functionResourceName}}
            package: function
    

    Taking a peek at the logs that the deployment process produces, it will still create a zip file and deploy that. But this is a different zip file than I created manually. All works fine now ;)