Contact Us

GitHub has opened the doors for meaningful collaboration across the wider software development community. As a GitHub user, you can truly make great use of its diverse ecosystem and leverage the wonderful features that it has to offer, including ‘GitHub Actions’, which is a powerful workflow engine that enables developers to automate repetitive tasks. In this blog, we dive deeper into the ways of configuring an automated GitHub Actions deployment pipeline.

 

The High-level Approach

There are a few steps involved in this configuration process., they are as follows:

1. First, we create a new ‘Azure AD App Registration’. This will enable GitHub Action deployment pipeline to authenticate with Azure (using App Registration credentials).

2. Then, we create a new ‘Secret’ in GitHub repo containing the ‘Azure AD App Registration’ credentials.

3. Then, create an ‘Azure Function App ARM’ template which will create the ‘Function App’ resource in Azure.

4. And finally, create a ‘GitHub Actions Workflow’ file to execute the ARM template and subsequently deploy the Azure Function code.

 

Create the Azure Function

We begin by creating a basic HTTP Azure Function (C#) that we’ll be deploying to Azure.

Create the Azure AD App Registration

The GitHub Actions workflow will first need to authenticate with Azure before it can begin deploying our code. Therefore, we must generate a new ‘Azure AD App Registration’ and then fetch the credentials for it.

So, let us head over to the Azure Portal and run the following command in the ‘Azure Bash Cloud Shell’ to register a new Azure AD application. Once completed, your new App Registration will be visible under AZURE ACTIVE DIRECTORY > APP REGISTRATIONS.

Azure active directory

azure function deployment

At this point, we will receive a JSON response containing the secret credentials. Make sure to retain this securely as you won’t be able to retrieve this later. Furthermore, this will be essential for the next step.

azure devops deployment

Create GitHub Repo Secret

Now let us store these credentials in the GitHub repo. We do this by heading over to our GitHub repo (i.e., the same repo where your Azure Function code is stored) and creating a ‘Secret’ containing the JSON response from the previous step.

create github repo secret


Generate Azure Function App ARM Template

Now, we generate an ARM template through the Azure Portal. This template will create an empty ‘Azure Function App’ resource in Azure.

In the Azure Portal, we create a new Function App resource. At the ‘Review + create’ screen, click ‘Download a template for automation’. This option will auto-generate the ARM template (including the Template and Parameters file) which needs to be stored in the GitHub code repository. It will be referenced from our GitHub Actions workflow file later.

github actions workflow

microsoft azure functions

git hub actions 101

MicrosoftTeams-image (132)


GitHub Actions 101

GitHub Actions workflows are defined using YAML files to define triggers, jobs and associated steps that need to be performed. These YAML files must be stored in GitHub repo alongside code in the following directory.

.github/workflows

Here’s a sample of what a YAML file looks like:

MicrosoftTeams-image (133)


Create the GitHub Actions Workflow File

Now we need to create the Github Actions workflow file. We will create a GitHub Actions *.yml workflow file that will perform the following high-level steps:

1. Create the empty Azure Function App resource using our ARM template that we created before

2. Build and package the C# Azure Function code

3. Deploy your code to the Azure Function App

 

github actions workflow file

This first section of the script specifies the following:

  • Defines the triggers that will cause this workflow to fire. This workflow will execute when someone pushes a new commit to the ‘master’ branch of this repository.
  • Then define various the environment variables. Note here that we would need to update the following:
    • ‘AZURE_RESOURCE_GROUP_NAME’: Specifies the ‘Resource Group’ where the Function App will reside.
    • ‘AZURE_FUNCTIONAPP_NAME’: Specifies the name of the ‘Function App Resource’ that will be created. This must match the ARM template.
    • ‘AZURE_FUNCTIONAPP_PACKAGE_PATH’: Path to the ‘Azure Function’ code.
  • And finally, since GitHub supports numerous command line shells, we will use the ‘bash shell’ as the default choice, unless otherwise specified within a given circumstance.

MicrosoftTeams-image (135)

In the next section, we create a job named ‘deploy-az-infrastructure' which will execute the ARM template to create the ‘Azure Function App’ resource.

MicrosoftTeams-image (136)

The ‘build-az-function' job will then build the code, create a zip package, and finally store the zip artefact for later use. This job will only be executed after the ‘deploy-az-infrastructure' job has finished running.

zip package to deploy-az-infrastructure

MicrosoftTeams-image (138)

The ‘deploy-az-function' workflow job does the following

1. First, we fetch the ‘Publishing Profile’ for the ‘Function App’ created earlier. This will be required later for the code deployment.

2. Then we fetch the ‘zip artefact’ that we had created earlier which contains the published code.

3. And finally, we deploy the code to the Azure Function App.

We have now successfully created our GitHub Actions workflow which will automatically deploy our Azure Function. The workflow will trigger when we push a new commit to our GitHub code repository.

github code repository

Once the GitHub Actions deployment workflow finishes running, Azure Function will soon be accessible.

git hub actions deployment

Need Help?

Conclusion

Following the process described above, it is now easier than ever before to build out an extensive CI/CD pipeline using GitHub actions. Maintaining our Azure Functions in a Git repository brings the fore, the benefits of version control, source code management, and of course the automated build and deployment of our functions into Azure though CI/CD.

Reach out to our expert team for more information and best practices to implement this for your business.

Publications
& Thought Leadership

Publication
Azure AD Setup for Sitecore
Enable single sign-on option in Sitecore by integrating it with Azure Active Directory (AD). Smoothen user and group management for your Sitecore website with tips shared by our Sitecore developers.
Azure AD Setup for Sitecore image
Publication
Next.js on Azure Functions: Serverless Architecture for Faster Web Development
Unlock the power of Next.js on Azure Functions! Seamlessly deploy scalable Next.js applications with serverless efficiency. Explore the future of web development.
Smart Sites 1
Webinar
Personalisation Using Azure ML And Sitecore
In this webinar, we will be using xDB data as well as the items content to personalize and Azure Machine Learning Studio – GUI based integrated development environment for performing different machine learning operations.
Personalisation Using Azure ML And Sitecore image