Friday, January 1, 2021

Build CI/CD Pipeline For Azure Container Instances



Introduction

 
In my recent two articles, I developed a REST API with Azure Functions using SQL database and containerized Azure Functions Apps using Docker Desktop. In this article, I want to do an automatic build and release process. If any developer wants to make a change in the code, then my pipelines will run automatically based on the branch name trigger. I already have pushed this application source code to Azure Repo. This is a public repo and anyone can access and clone the code. I will use the Azure Container Registry service for docker image hosting and I will use Azure Container Instances service for container hosting. Azure Container Instances is a managed and serverless container hosting service provided by Azure. Azure Container Instances can start containers in Azure in a few seconds, without the need to provision and manage VMs or any other resources.

Problem and Solution

 
The first problem I want to solve is whenever I change or update my Azure function code. Every single time I need to create a new docker image of my Azure function app and push it to the Azure container registry or docker hub and then update my Azure container instances service for the latest docker image. This is a huge pain for me to do this whole process every time, therefore I want to automate my whole process. Whenever I will update the code and push it to the Azure repo then based on branch trigger a build pipeline will fire up and build the code in release mode. After building the code, then a new docker image will be created and pushed to the Azure container registry. Next, a release pipeline will fire up and update the docker image in the Azure container instances.
 
Prerequisites
 
You are required to have basic knowledge of Azure Container Registry and Azure Container Instances services and beginner-level knowledge of Azure DevOps.
 
Agenda
  • Create Azure Resource Group
  • Create Azure Container Registry
  • Grab Azure Container Registry Access Keys  
  • Create a Build and Push Docker Image Pipeline
  • Create a Release Pipeline
  • Define Variables in Release Pipeline
Let's get started:
 
Log in to the Azure portal or log in through Azure CLI. I will show you these both ways. Open your command prompt and use the following command in order to login to Azure. Make sure you already have installed the Azure CLI on your local.
  1. az login  
After logging in, you can see your all active subscriptions in the output and you need to set the subscription for this current context to do so use the following command. I have multiple subscriptions and I will use my Azure pass sponsorship for this context.
 
Build CI/CD Pipeline For Azure Container Instances 
  1. az account set --subscription "Azure Pass - Sponsorship"  
Step 1 - Create an Azure Resource Group
 
 As you know, we already logged in using CLI. Now we will create a resource group for our docker images and container instances. We will keep our all resources in this resource group that we are creating. Use the following command in order to create the resource group.
  1. az group create --name "azurefunction-rg" --location "centralus"  
Build CI/CD Pipeline For Azure Container Instances 
 
Build CI/CD Pipeline For Azure Container Instances
 
  1. az group list --output table   
Step 2 - Create Azure Container Registry
 
We have created a resource group. Now for every new resource, we will add to this resource group. Use the following command to create the Azure container registry for hosting docker images. This is a paid service, but you can also use a free hosting service like Docker Hub.
 
(Azure Container Registry Name : azurefuncAcr)
  1. az acr create --resource-group azurefunction-rg --name azurefuncAcr --sku Basic 
Build CI/CD Pipeline For Azure Container Instances
 
Build CI/CD Pipeline For Azure Container Instances 
 
As shown above, you can see that we have successfully created the Azure container registry. Now we need to enable the Azure container registry admin user permission. Open the resource group from the Azure portal and open the container registry and then click on the Access Keys tab and enable Admin user toggle.
 
Build CI/CD Pipeline For Azure Container Instances 
 
Build CI/CD Pipeline For Azure Container Instances 
 
Build CI/CD Pipeline For Azure Container Instances 
 
Step 3 - Grab Azure Container Registry Access Keys
 
After enabling the Admin user permission, you will see the username and two auto-generated passwords. Copy the following required credentials (Registry Name, Login Server, Username, Anyone Password).
 
Build CI/CD Pipeline For Azure Container Instances 
 
Step 4 - Create a Build and Push Docker Image Pipeline
 
Login to your DevOps account and create a new project with the name Azure Function App. 
 
Build CI/CD Pipeline For Azure Container Instances 
Open this newly created project and add source code to this project repo. You can also use the existing source code that I mention on top of this article. I already have the source code in this project.
 
Build CI/CD Pipeline For Azure Container Instances 
 
Next, click on the Pipeline tab and then choose the pipelines option from the drop-down because first, we will create a build pipeline for our application. This pipeline will build the source code and then create the docker image and finally will push to the Azure container registry that we created in the last step. Click on create pipeline and choose the source code option. As I mentioned earlier that, I am using Azure repos for my source code. I will select the Azure repo git option.
 
Build CI/CD Pipeline For Azure Container Instances 
 
Select your repository and then branch. In my case, I will choose Azure Function App.
 
Build CI/CD Pipeline For Azure Container Instances 
 
Next, select Docker (build and push image to azure container registry) option from the Configure your pipeline page.
 
Build CI/CD Pipeline For Azure Container Instances 
 
Next, select your active subscription from pop up and hit continue. Enter your Azure account credentials and click sign in.
 
Build CI/CD Pipeline For Azure Container Instances 
 
Next, select your registry name from the dropdown and enter the image name that you want to use. Dockerfile option leaves it with a default value. Finally, click on validate and configure.
 
Build CI/CD Pipeline For Azure Container Instances 
 
Next, you will see an azure-pipelines-1.yml file that is a predefined template for building and pushing images to the Azure container registry. Just update the branch trigger name main to master or any branch name that you want to use for this build pipeline. Now we are good to go and we can save this pipeline as-is. Click on save and run. 
  1. # Docker  
  2. # Build and push an image to Azure Container Registry  
  3. # https://docs.microsoft.com/azure/devops/pipelines/languages/docker  
  4.   
  5. trigger:  
  6. - master  
  7.   
  8. resources:  
  9. - repo: self  
  10.   
  11. variables:  
  12.   # Container registry service connection established during pipeline creation  
  13.   dockerRegistryServiceConnection: 'd8b817ba-6640-4efd-908f-f9f0fa26ce16'  
  14.   imageRepository: 'funcappimage'  
  15.   containerRegistry: 'azurefuncacr.azurecr.io'  
  16.   dockerfilePath: '$(Build.SourcesDirectory)/DockerApp/Dockerfile'  
  17.   tag: '$(Build.BuildId)'  
  18.     
  19.   # Agent VM image name  
  20.   vmImageName: 'ubuntu-latest'  
  21.   
  22. stages:  
  23. - stage: Build  
  24.   displayName: Build and push stage  
  25.   jobs:    
  26.   - job: Build  
  27.     displayName: Build  
  28.     pool:  
  29.       vmImage: $(vmImageName)  
  30.     steps:  
  31.     - task: Docker@2  
  32.       displayName: Build and push an image to container registry  
  33.       inputs:  
  34.         command: buildAndPush  
  35.         repository: $(imageRepository)  
  36.         dockerfile: $(dockerfilePath)  
  37.         containerRegistry: $(dockerRegistryServiceConnection)  
  38.         tags: |  
  39.           $(tag)  
Build CI/CD Pipeline For Azure Container Instances
 
Build CI/CD Pipeline For Azure Container Instances 
 
Here, you can see in the Azure portal that a newly created docker image is added with its build number ID.
 
Build CI/CD Pipeline For Azure Container Instances 
 
Step 5 - Create a Release Pipeline
 
We have successfully created the docker image and pushed it to the Azure container registry. Next, click on releases and a select new pipeline. Deploying to the Azure container instance template is not available, therefore, we will use an empty template job.
 
Build CI/CD Pipeline For Azure Container Instances 
 
Build CI/CD Pipeline For Azure Container Instances 
 
Add a new task in the release pipeline by clicking on the plus icon. Search Azure CLI and hit add. Next, click on the Azure CLI task and add some required information.
 
Build CI/CD Pipeline For Azure Container Instances 
 
Select Azure Resource Manager connection and select Script Type to batch and select Script Location to the inline script. Add the following command in the Inline Script textbox.
 
Build CI/CD Pipeline For Azure Container Instances
  1. az container create -g $(resourceGrop) --name funcapp --image $(acrLoginServer)/azurefunctionapp:$(Build.BuildId) --cpu 1 --memory 1 --registry-login-server $(acrLoginServer) --registry-username $(acrName) --registry-password $(acrPassword) --dns-name-label funcapp --ports 80 -e SqlConnectionString="Server=tcp:development.database.windows.net,1433;Initial Catalog=Learning;Persist Security Info=False;User ID=ahsan;Password=masK;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"
Step 6 - Define Variables in Release Pipeline
 
Add the following variable in the variable tab.
 
Note
Please change your Azure container instance app name and also update the connection string for your database if you have it in your app. Otherwise, you can remove the environment variable that you can see with the -e tag.
 
Build CI/CD Pipeline For Azure Container Instances
 
Step 6 - Add Artifacts
 
Click on pipeline and then select the Add an artifact button. Choose an Azure container and select service connection, resource group, azure container registry, repository, and version to the latest version, then click on add. 
 
Build CI/CD Pipeline For Azure Container Instances 
 
Add a continuous deployment trigger by clicking on the event type button and enable the option then save the release pipeline. Now our release pipeline is also ready to run.
 
Build CI/CD Pipeline For Azure Container Instances 
 

Final Words

 
We have successfully created a build and release pipeline for our Azure function app. Add any change to your source code and push to repo, then you will see build pipeline will run and build the code and create the docker image and push to the Azure container registry. Then release pipeline will deploy your container to Azure container instances.
 
Build CI/CD Pipeline For Azure Container Instances 
 
Build CI/CD Pipeline For Azure Container Instances 
 
Yahoo, Azure container instance up and running in Azure portal. So, this was the process of building a CI/CD pipeline for Azure container instances.
 
Build CI/CD Pipeline For Azure Container Instances

Ahsan Siddique

Author & Editor

Experienced Microsoft Certified Azure Developer and Trainer with a demonstrated history of working in the computer software industry. Skilled in .NET, .NET Core, C#, Xamarin, Azure, Kubernetes, Docker Containers, Microservices, CI/CD, and Universal Windows Platform with Strong engineering professional.

0 Comments:

Post a Comment