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.
- az login
- az account set --subscription "Azure Pass - Sponsorship"
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.
- az group create --name "azurefunction-rg" --location "centralus"
- 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)
- az acr create --resource-group azurefunction-rg --name azurefuncAcr --sku Basic
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.
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).
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.
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.
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.
Select your repository and then branch. In my case, I will choose Azure Function App.
Next, select Docker (build and push image to azure container registry) option from the Configure your pipeline page.
Next, select your active subscription from pop up and hit continue. Enter your Azure account credentials and click sign in.
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.
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.
- # Docker
- # Build and push an image to Azure Container Registry
- # https://docs.microsoft.com/azure/devops/pipelines/languages/docker
- trigger:
- - master
- resources:
- - repo: self
- variables:
- # Container registry service connection established during pipeline creation
- dockerRegistryServiceConnection: 'd8b817ba-6640-4efd-908f-f9f0fa26ce16'
- imageRepository: 'funcappimage'
- containerRegistry: 'azurefuncacr.azurecr.io'
- dockerfilePath: '$(Build.SourcesDirectory)/DockerApp/Dockerfile'
- tag: '$(Build.BuildId)'
- # Agent VM image name
- vmImageName: 'ubuntu-latest'
- stages:
- - stage: Build
- displayName: Build and push stage
- jobs:
- - job: Build
- displayName: Build
- pool:
- vmImage: $(vmImageName)
- steps:
- - task: Docker@2
- displayName: Build and push an image to container registry
- inputs:
- command: buildAndPush
- repository: $(imageRepository)
- dockerfile: $(dockerfilePath)
- containerRegistry: $(dockerRegistryServiceConnection)
- tags: |
- $(tag)
Here, you can see in the Azure portal that a newly created docker image is added with its build number ID.
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.
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.
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.
- 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;"
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.
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.
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.
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.
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.
0 Comments:
Post a Comment