Deploying Azure Functions With Github Actions

Friday, Jan 10, 2020 4 minute read Tags: serverless azure-functions devops azure
Hey, thanks for the interest in this post, but just letting you know that it is over 3 years old, so the content in here may not be accurate.

When I was creating my Azure Functions to generate social images I decided to give GitHub Actions a spin as the deployment tool, after all, I quite liked them when I updated my blog. So let’s have a look at how to use GitHub Actions to deploy Azure Function.

There’s a lot of context and terminology on getting started with GitHub Actions in my other blog post that I’d encourage you to read first if you’re new to GitHub Actions, since I won’t cover it all in detail here.

Setting Up Our Action

We’ll start by creating our GitHub Action file at .github/workflows/devops-workflow.yml in our git repo:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
name: Build and Deploy
env:
    OUTPUT_PATH: ${{ github.workspace }}/.output
    DOTNET_VERSION: "3.1.100"

on:
    push:
        branches:
            - master

jobs:

We’ll use some environment variables for the output and .NET version (since the Functions in my image generator are .NET Functions, but you don’t need that if they are non-.NET that you’re using) and specify that this workflow will only run when code is pushed to the master branch.

The Build Job

If it’s a .NET Function we’ll need to compile it, if it’s Node.js install the npm packages, use pip if it’s Python and Maven for Java. This is what the role of the Build job will handle, preparing the assets we need to deploy to Azure, so let’s create a Build job:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
build:
    runs-on: ubuntu-latest
    steps:
        - name: "Checkout"
          uses: actions/checkout@master

        - name: Setup Dotnet ${{ env.DOTNET_VERSION }}
          uses: actions/setup-dotnet@v1
          with:
              dotnet-version: ${{ env.DOTNET_VERSION }}

        - name: Publish functions
          run: dotnet publish --configuration Release --output ${{ env.OUTPUT_PATH }}

This will use the actions/setup-dotnet@v1 Action from the marketplace to install the right version of .NET (based on our environment variable) and use the .NET CLI to publish the output.

Now it’s time to package the output for the deployment job:

1
2
3
4
5
- name: Package functions
  uses: actions/upload-artifact@v1
  with:
      name: functions
      path: ${{ env.OUTPUT_PATH }}

Tada 🎉! You have an artifact for the Functions, ready to be deployed.

The Deployment Job

1
2
3
4
5
deploy:
    runs-on: ubuntu-latest
    needs: [build]
    env:
        FUNC_APP_NAME: blogimagegenerator

Since the deploy job will need the artifacts from the build job we’ll set it up as a dependency using the needs: [build], otherwise we’d deploy before the Functions were built, and that’s not going to work!

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
steps:
    - name: Download website
      uses: actions/download-artifact@v1
      with:
          name: functions
          path: ${{ env.OUTPUT_PATH }}

    - name: "Login via Azure CLI"
      uses: azure/login@v1
      with:
          creds: ${{ secrets.AZURE_CREDENTIALS }}

Using the actions/download-artifact@v1 we can get the output from the build job and then it’s time to log in to Azure using the credentials we’d previously generated (see my last blog post for information about that).

The last piece of the puzzle is to use the Azure/functions-action@v1 GitHub Action from the marketplace:

1
2
3
4
5
- name: "Run Azure Functions Action"
  uses: Azure/functions-action@v1
  with:
      app-name: ${{ env.FUNC_APP_NAME }}
      package: ${{ env.OUTPUT_PATH }}

This requires the name of the Function App that we’re deploying into to be provided as the app-name parameter (we’ve stored it in an environment variable named FUNC_APP_NAME).

And with that the workflow is complete, ready to deploy your Functions to Azure. You can find the full file on my GitHub along with a recent run if you’re curious on the output.

Conclusion

With only 53 lines we can create a GitHub Action that will deploy Azure Functions each time we push to a branch, which I think is pretty simple.

I’ve covered off how to do it with a .NET Function, but if you want to check out how to do it with other languages head on over to the Azure Function docs.