How to Deploy Azure Functions using GitHub Actions
Deploying changes to your Azure Functions manually can be a big time sink. Azure Functions supports using modern software deployment practices such as continuous integration and continuous deployment with the CICD tool GitHub Actions to automate deployments of your code.
In this tutorial, we will create an Azure Function App in Azure and create a GitHub Actions workflow YAML file to deploy changes to our Azure Functions using both the act tool locally and GitHub Actions within GitHub.
This content is available in video form on the Cloud Engineer Skills YouTube channel.
Setup CICD for Azure Functions with GitHub Actions
Create the Resource Group, Storage Account, and Function App
We will start by creating the following Azure Resources:
- Azure Resource Group
- Azure Storage Account
- Azure Function App
Creating the Resource Group
1
az group create -g cloudengineerskills-af-rg -l eastus
Creating the Storage Account
1
az storage account create -n cesstorageaccount2323 -g cloudengineerskills-af-rg -l eastus --sku Standard_LRS
Creating the Function App
1
az functionapp create -g cloudengineerskills-af-rg --consumption-plan-location eastus -n cloudengineerskillsfuncapp -s cesstorageaccount2323 --runtime node --os-type Linux --functions-version 4
Initialize a Function Project and Create a Function
Next we will initialize an Azure Function Project locallly and add an Azure Function inside it using Azure Function Core Tools.
To learn more about this process follow this previous tutorial.
Intializing a Node.js Azure Function Project
Now we will initialize the Azure Function Project and we will use Node.js as the programming language for the Azure Functions.
1
func init
- Select
node
- Select
javascript
Adding a Node.js Azure Function with a HTTP Trigger
Next we will add a HTTP Trigger Node.js Azure Function to the Azure Function Project locally.
1
func new
- Select
HTTP Trigger
- Provide a
Function name
and hit enter
Create the Function App Publish Profile
We will need a Function App Publish profile to deploy Azure Functions to our Azure Function App within Azure.
This can be located in the Azure Function, go the Function App you have previously created, in the Overview
screen select Get publish profile
and download the file.
Add a Secret for the Publish Profile
The Publish Profile allows you to deploy changes to your Azure Function App. This will need to be set up as a secret to use in a GitHub Actions workflow.
Make the following local changes to set up your publish profile as a secret.
- Open the downloaded publish profile in a text editor
- Create
main.secrets
file inside the directory.github/workflows
- Add secret
AZURE_FUNCTIONAPP_PUBLISH_PROFILE
tomain.secrets
- Add
*.secrets
to.gitignore
file because this secret contains sensitive information and should never be present in your code repository
Sample .github/workflows/main.secrets
1
AZURE_FUNCTIONAPP_PUBLISH_PROFILE=<YOUR_PUBLISH_PROFILE_HERE>
Sample .github/workflows/.gitignore
1
*.secrets
Create GitHub Actions Workflow File
The GitHub Actions workflow describes the steps it will automatically run to deploy your application. This can include steps for building, testing and deploying your application.
You can also provide environment variables, and specify on what event do you want your GitHub action to run.
We will run the GitHub action on push
, and define the following environment variables:
AZURE_FUNCTIONAPP_NAME
: this is the name of the Azure Function App resourceAZURE_FUNCTIONAPP_PACKAGE_PATH
: to provide the relative path to the base of the functions projectNODE_VERSION
: to specify we want to use Node.js version 20
Steps are run inside jobs, you can have multiple jobs, and each job can have one or more steps.
We will have one a single job called build-and-deploy
containing 5 steps.
Steps are run on an operating system, we will use ubuntu-latest
in our workflow, and each step will have some action that it invokes.
We will make use of the following actions within the steps of our workflow:
- The run action
- actions/checkout@v3
- actions/setup-node@v3
- Azure/functions-action@v1
Create the main.yml
YAML file in the .github/workflows
directory using this code
You will need to alter the AZURE_FUNCTIONAPP_NAME
environment variable if your Azure Function App is named something else.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
name: Deploy Azure Functions Node.js project to Azure Function App
on: [push]
env:
AZURE_FUNCTIONAPP_NAME: "cloudengineerskillsfuncapp"
AZURE_FUNCTIONAPP_PACKAGE_PATH: "."
NODE_VERSION: "20.x"
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: "Checkout GitHub Action"
uses: actions/checkout@v3
- name: Install Azure CLI (required for self hosted runners like act)
run: curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash
- name: Setup Node $ Environment
uses: actions/setup-node@v3
with:
node-version: $
- name: "Resolve Project Dependencies Using Npm"
shell: bash
run: |
pushd './$'
npm install
npm run build --if-present
npm run test --if-present
popd
- name: "Run Azure Functions Action"
uses: Azure/functions-action@v1
id: fa
with:
app-name: $
package: $
publish-profile: $
Test Deployment Locally using Act
Act allows you to run your GitHub Action workflow locally, this avoids the risk of cluttering your GitHub repository GitHub Actions with failed builds.
You can install Act by following the Act user guide.
Act requires the Docker Engine to be running before it can be used, make sure to start it before running the following command to deploy your Azure Functions locally into Azure.
1
act --secret-file .github/workflows/main.secrets
Deploy using GitHub Actions
Make sure you have created your GitHub repo first by pushing your code into a GitHub repository.
In your GitHub repository settings you will need to add a secret named AZURE_FUNCTIONAPP_PUBLISH_PROFILE
.
Follow the instructions below to add the secret.
- In your GitHub repository on github.com select
Settings
- Select
Secrets and variables
, selectActions
- Select
New repository secret
- Name it
AZURE_FUNCTIONAPP_PUBLISH_PROFILE
, and copy over the Publish Profile from your.github/workflow/main.secrets
file locally - Select
Add Secret
From there you can push a change to your repository and verify GitHub Actions runs correctly and has updated your Azure Function in your Azure Function App.