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:
|
|
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:
|
|
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:
|
|
Tada 🎉! You have an artifact for the Functions, ready to be deployed.
The Deployment Job
|
|
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!
|
|
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:
|
|
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.