Azure Spring Apps Custom Container Image CICD deployment using Azure DevOps

Published Jun 13 2022 11:50 PM 1,134 Views
Microsoft

Since 2022 May, Azure Spring Cloud started to allow deploying an application with a custom container image (Preview).

Deploying an application with a custom container supports most features as when deploying a JAR application. Other Java and non-Java applications can also be deployed with the container image.

 

This blog aims to show an example for how to use Azure DevOps pipeline to make CICD deployment using custom container image.

Hanli_Ren_0-1654852056116.png

 

Prerequisites

Step 1: Create a Git repo for your project in Azure DevOps

 

1.  Go to https://dev.azure.com 

2.  After signing in, click on 'Create Project' in the top right of the screen. If there is no organization already created then this will need to be done first.

3.  Create a Git repository in your Azure DevOps Project.

Hanli_Ren_0-1654852747218.png

Get the repository URL

Hanli_Ren_1-1654852860360.png

 

Step 2: Push Application code and Dockerfile to the git repository

 

1.  Git clone the new created empty repository to your local machine

 

git clone https://xxx@dev.azure.com/xxx/xxxx/_git/xxx​

 

 

2.  Add your application code and Dockerfile

    In this example I build a simple Python Flask application, and use a Dockerfile to pack it into a custom docker container.

    There are 3 files need to be committed to the Git repo.

 

requirements.txt

 

Flask

 

App.py

 

from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello from Azure Spring Cloud Custom Container Image!"
	

 

Dockerfile

 

FROM python:3.8-slim-buster

WORKDIR /flaskdemo

COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt

COPY . .

EXPOSE 1025

CMD [ "python3", "-m" , "flask", "run", "--host=0.0.0.0", "--port=1025"]

 

 

3.  Push your code to DevOps git repository

 

git add .
git commit -m "initial commit"
git push 

 

 

Hanli_Ren_0-1654854266499.png

 

Step 3: Create a new Build Pipeline to build and push Docker image

1.  Create a new pipeline use the classic editor

Hanli_Ren_0-1654854683323.png

 

2. Select source repository

Hanli_Ren_1-1654854952508.png

 

3. Select Docker container template

Hanli_Ren_2-1654854988215.png

 

4.  Fill in your Azure Container Registry information in the "Build an image" Task.
Since we added our Dockerfile in our git repository, according to "**/Dockerfile" setting, the pipeline can find our Dockerfile and use it to build the docker image.

Hanli_Ren_3-1654855061150.png

 

5.  Fill in your Azure Container Registry information in the "Push an image" Task.

Hanli_Ren_4-1654855150807.png

 

6. Enable continuous integration in Triggers

Hanli_Ren_0-1654856495391.png

 

Step 4: Create New Release Pipeline

1.  Set the Artifacts, select your project name, Source (the build pipeline we just created in Step 3)

Hanli_Ren_0-1654855304886.png

We can enable Continuous deployment trigger, so when there is any code change being pushed to our git repository, the release task will be triggered to run.

Hanli_Ren_1-1654855352602.png

 

2. Add a new Stage using "Empty job" template, named it "Deploy to App"

Hanli_Ren_2-1654855411600.png

 

3. Create a Task in this "Deploy to App" stage

Hanli_Ren_3-1654855496707.png

 

4. Select "ubuntu-latest" as the job Agent

Hanli_Ren_4-1654855548124.png

 

5.  Add an Azure CLI task in this Agent job

Hanli_Ren_5-1654855613828.png

 

6. Select your Azure subscription, and fill in the following Shell Inline script

 

az config set extension.use_dynamic_install=yes_without_prompt
az extension add --name spring-cloud

az spring-cloud app deploy \
   --resource-group <your resource group name> \
   --name <App name> \
   --container-image $(Build.Repository.Name):$(Build.BuildId) \
   --service <your Azure Spring Cloud service name> \
   --container-registry <Azure Container Registry url> \
   --registry-password <Azure Container Registry password> \
   --registry-username <Azure Container Registry username>

 

 

Hanli_Ren_6-1654855719670.png

 

Step 5: Trigger CICD docker image build and App deployment.

Since we enabled continuous integration in Step 3 & continuous deployment in Step 4, when we commit and push any code change in the git repository, the pipeline will be automatically triggered.

 

1. Push some changes in the git repo, then we can go to Pipeline -> Runs to check the build log

Hanli_Ren_7-1654855921513.png

 

2. We can see detailed log for each build step

Hanli_Ren_8-1654855969370.png

 

Hanli_Ren_9-1654855987190.png

 

3. We can go to Pipeline -> Releases to check the release log

Hanli_Ren_10-1654856069419.png

"Azure CLI" task log records all the Azure CLI commands and the response details.

Hanli_Ren_11-1654856125970.png

Hanli_Ren_12-1654856164380.png

 

4. After the new app deployment being successfully boot up, in Azure Spring Apps portal, we should be able to see Succeeded Provisioning state.

Hanli_Ren_13-1654856231822.png

In the App -> Configuration portal, we should see the new custom docker image being set.

Hanli_Ren_14-1654856275011.png

We can check the application being successfully brought up and responding incoming requests. 

Hanli_Ren_15-1654856319701.png

Co-Authors
Version history
Last update:
‎Jun 10 2022 03:25 AM
Updated by: