Continuous Integration and Continuous Deployment (CI/CD) in Azure with GitHub Actions
Continuous Integration (CI) and Continuous Deployment (CD) are integral practices in modern software development, enabling teams to automate the building, testing, and deployment of their applications. GitHub Actions is a powerful workflow automation tool integrated with GitHub, that can be seamlessly combined with Azure, Microsoft’s cloud computing platform, to create a robust CI/CD pipeline. This integration streamlines the development process, improving collaboration, code quality, and the speed of delivering new features and bug fixes to end-users.
In this environment, any code changes are automatically validated, tested, and deployed with minimal manual intervention, reducing the risk of human errors and the time spent on repetitive tasks. GitHub Actions and Azure together provide a powerful and flexible solution for implementing CI/CD, allowing teams to focus on code development and innovation while ensuring the reliability and efficiency of their software delivery pipeline.
First, let us clarify some basic concepts to understand the CI/CD process
- GitHub Actions
- GitHub Actions Workflows
- GitHub Actions Runners
What are GitHub Actions?
GitHub Actions is a powerful and integrated automation tool provided by GitHub to streamline and automate various aspects of your software development workflows. With GitHub Actions, you can define custom workflows using YAML files to automatically perform tasks like building, testing, deploying, and more, in response to events such as code changes, pull requests, or scheduled triggers. These automated workflows can help you save time, improve code quality, and enhance collaboration by simplifying and optimizing your development processes, all within the familiar GitHub environment.
What are GitHub Actions Workflows?
A GitHub Actions workflow is a set of one or more automated steps that can be triggered in response to various events, such as code pushes, pull requests, or scheduled tasks. Workflows are defined in YAML files and can include a wide range of actions and commands to build, test, deploy, and more. You can create custom workflows to meet the specific needs of your project, and they are executed in a self-contained environment called a runner. Workflows have to be created under the .github/workflows folder and you can have one or multiple YAML files.
name are the names of the workflow.
on are the actions that trigger the workflow and
jobs are a collection of tasks. Each job runs in its own environment. These environments are also called runners. We will explain in more detail in the next point.
name: CI CD with GitHub Actions and Azure
on:
push:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
What are GitHub Actions Runners?
GitHub Actions runners are the execution environments for your workflows. They can be either GitHub-hosted runners or self-hosted runners. GitHub-hosted runners are provided and maintained by GitHub, and they come with pre-installed software and various configuration options. Self-hosted runners, on the other hand, are machines or virtual machines that you set up and maintain in your own infrastructure. You can choose the runner that best suits your requirements and customize it to execute your workflows efficiently.
Other alternatives for GitHub Actions are Jenkins or Circle CI and for the Azure side, we could use AWS or Google Cloud
Practical Example
For this article, we will describe the steps to implement a basic Continuous Integration (CI) and Continuous Deployment (CD) pipeline using GitHub Actions and Azure to set up a Web API Net Core Application.
1. Create Web Application with Visual Studio or Visual Code
Create a ASP.NET Core Web App in this case using Visual Studio 2022. However, we can use also Visual Code. We will run it locally to be sure it is working as expected.
2. Set Up the GitHub Repository and Push the Web Application
Create a GitHub repository for your project and run these commands in your command console
Open your Terminal Application and Run these commands to push your new web application to GitHub
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin git@github.com:sandropucp/CI-CD-GitHub-Actions.git
git push -u origin master
3. Create a Web Application in Azure.
For this, we can use different ways. Let's do it using Terraform to have a more reusable and consistent way to do it.
resource "azurerm_resource_group" "web_group_01" {
name = "WebAppGroup01"
location = "eastus"
}
resource "azurerm_service_plan" "service_plan_01" {
name = "ServicePlan01"
resource_group_name = azurerm_resource_group.web_group_01.name
location = azurerm_resource_group.web_group_01.location
os_type = "Windows"
sku_name = "F1"
}
resource "azurerm_windows_web_app" "web_app_01" {
name = "WebApp0134783"
resource_group_name = azurerm_resource_group.web_group_01.name
location = azurerm_service_plan.service_plan_01.location
service_plan_id = azurerm_service_plan.service_plan_01.id
site_config {
always_on = false
application_stack {
current_stack = "dotnet"
dotnet_version = "v6.0"
}
}
}
terraform init
terraform fmt
terraform validate
terraform plan
terraform apply
terraform destroy
4. Download Publish Profile from Web Slot and Create Secret in GitHub
In Azure go to the Slot Web Application
Download Publish profile
Open Publish Profile in Notepad. Copy Content in the clipboard
Go to GitHub to create a Secret and the Profile content
5. Define Workflow and Commit Changes to Start workflow
- Inside your repository, create a
.github/workflows
directory. - Create a YAML file for your workflow (e.g.,
ci-cd.yml
) to define the CI/CD pipeline.
# This workflow will build a .NET project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-net
name: 'Deploy to Azure Web App'
on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]
env:
AZURE_WEBAPP_PACKAGE_PATH: '.'
AZURE_WEBAPP_NAME: WebApp0134783
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 6.0.x
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build --configuration release
- name: Publish
run: dotnet publish - Release -o '${{env.AZURE_WEBAPP_PACKAGE_PATH}}/myapp'
- name: Deploy
uses: Azure/webapps-deploy@v3.0.0
with:
app-name: '${{env.AZURE_WEBAPP_NAME}}'
slot-name: WebAppSlot01
publish-profile: '${{secrets.AZURE_WEBAPP_PUBLISH_PROFILE}}'
package: '${{env.AZURE_WEBAPP_PACKAGE_PATH}}/myapp'
6. Check Slot Page
In Azure go to the new Slot page to check the deployment was successful
7. Swapping Deployment Slot to Production Web App
Go to Azure
Go to the Web Application root
Click Swap
Check Web App Production