DEV Community

Aaron Powell for Microsoft Azure

Posted on • Originally published at aaron-powell.com on

Deploying Azure Functions With GitHub Actions

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:

name: Build and Deploy
env:
  OUTPUT_PATH: ${{ github.workspace }}/.output
  DOTNET_VERSION: "3.1.100"

on:
  push:
    branches:
      - master

jobs:
Enter fullscreen mode Exit fullscreen mode

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:

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 }}
Enter fullscreen mode Exit fullscreen mode

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:

- name: Package functions
  uses: actions/upload-artifact@v1
  with:
    name: functions
    path: ${{ env.OUTPUT_PATH }}
Enter fullscreen mode Exit fullscreen mode

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

The Deployment Job

deploy:
  runs-on: ubuntu-latest
  needs: [build]
  env:
    FUNC_APP_NAME: blogimagegenerator
Enter fullscreen mode Exit fullscreen mode

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!

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 }}
Enter fullscreen mode Exit fullscreen mode

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:

- name: "Run Azure Functions Action"
  uses: Azure/functions-action@v1
  with:
    app-name: ${{ env.FUNC_APP_NAME }}
    package: ${{ env.OUTPUT_PATH }}
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (0)