Deploy a Spring Boot application on Azure Container Instances (ACI) using Azure DevOps CI/CD pipelines

Deploying a Spring Boot application to Azure Container Instances (ACI) using Azure DevOps CI/CD pipelines involves several steps. 

Here is the general architecture diagram that we will be deploying,


The diagram illustrates the process of deploying an application using Azure services. 

  • Developers write the application code and push it to Azure Repos, which is a source code repository in Azure DevOps.
  • Azure Repos manages the source control system (e.g., Git).
  • Azure Pipelines are set up to automate the build and deployment process.
  • The pipeline is triggered whenever there is a code change in the Azure Repo.
  • The pipeline builds the code, runs tests, and creates a container image if needed.
  • Once the build is successful, Azure Pipelines push the containerized application (Docker image) to the Azure Container Registry (ACR).
  • ACR is a private registry used to store and manage container images securely.
  • Azure Pipelines deploy the container image from ACR to Azure Container Instances (ACI).
  • ACI is a platform to run containerized applications in the Azure cloud.
  • The containerized application is hosted in the Azure Container Instance and becomes available to end users over the internet or a private network.
  • Feedback is collected from users to improve the application.
  • Developers analyze the feedback, implement updates or fixes, and push the changes back to the Azure Repo, restarting the cycle.

This workflow represents a continuous integration/continuous deployment (CI/CD) pipeline for containerized applications in Azure.

Here's a detailed guide:


Prerequisites

  1. Azure Account:

    • Active Azure subscription.
    • Azure CLI installed and logged in.
  2. Azure DevOps Account:

    • Active Azure DevOps organization with access to a project.
  3. Spring Boot Application:

    • A working Spring Boot application.
  4. Docker:

    • Docker installed locally for creating and managing images.

Steps to Deploy

Step 1: Prepare Your Spring Boot Application

  1. Ensure your Spring Boot app has a Dockerfile:

    FROM openjdk:17-jdk-slim
    ARG JAR_FILE=target/*.jar
    COPY ${JAR_FILE} app.jar
    ENTRYPOINT ["java", "-jar", "/app.jar"]
  2. Package your application:

    mvn clean package
  3. Test the application locally using Docker:

    docker build -t springboot-app .
    docker run -p 8080:8080 springboot-app

Step 2: Set Up Azure Resources

  1. Create a Resource Group:

    az group create --name myResourceGroup --location eastus
  2. Create an ACR (Azure Container Registry):

    az acr create --resource-group myResourceGroup --name myACR --sku Basic
  3. Enable Admin Access for ACR:

    az acr update --name myACR --admin-enabled true
  4. Retrieve ACR Login Server:

    az acr login --name myACR

Step 3: Configure Azure DevOps Repository

  1. Commit your Spring Boot application with the Dockerfile into an Azure Repos repository.

  2. Navigate to Azure DevOps > Repos and ensure your code is accessible.


CI/CD Pipeline Setup in Azure DevOps

Step 4: Create a CI Pipeline

  1. Navigate to Pipelines > New Pipeline > Azure Repos Git:

    • Select your repository.
  2. Create a YAML CI Pipeline:

    trigger:
      - main
    
    pool:
      vmImage: 'ubuntu-latest'
    
    variables:
      ACR_NAME: 'myACR'
    
    steps:
      - task: Maven@3
        inputs:
          mavenPomFile: 'pom.xml'
          goals: 'clean package'
    
      - task: Docker@2
        displayName: 'Build and Push Docker Image'
        inputs:
          command: 'buildAndPush'
          repository: '$(ACR_NAME).azurecr.io/springboot-app'
          dockerfile: '**/Dockerfile'
          containerRegistry: '$(ACR_NAME)'
          tags: '$(Build.BuildId)'
  3. Save and Run:

    • Verify that your Docker image is built and pushed to the ACR.

Step 5: Create a CD Pipeline

  1. Navigate to Pipelines > Releases > New Pipeline.

  2. Add an Artifact:

    • Select the build pipeline as the artifact source.
  3. Add a Stage:

    • Name the stage (e.g., Deploy to ACI).
  4. Configure Deployment Task:

    • Select Azure CLI task.
    • Add the following script to deploy the image to ACI:
      az container create \
        --resource-group myResourceGroup \
        --name springboot-container \
        --image myACR.azurecr.io/springboot-app:$(Build.BuildId) \
        --cpu 1 \
        --memory 1 \
        --registry-login-server myACR.azurecr.io \
        --registry-username <ACR_USERNAME> \
        --registry-password <ACR_PASSWORD> \
        --ports 8080
  5. Enable Continuous Deployment:

    • Go to Triggers and enable the Continuous deployment trigger.
  6. Save and Deploy:

    • Run the pipeline to deploy your Spring Boot app to ACI.

Validation

  1. Check your ACI container:

    az container show --resource-group myResourceGroup --name springboot-container --query ipAddress
    
  2. Open the public IP in a browser:

    • Verify the application is running.

Enhancements

  • Secrets Management: Use Azure Key Vault to securely store sensitive data like ACR credentials.

  • Monitoring: Enable Azure Monitor and Application Insights for logging and monitoring.

  • Scaling: Consider using Azure Kubernetes Service (AKS) for better scalability and management.

🌟 Master Microsoft Azure with Microsoft Azure in Action! 🌟

Dive into the world of cloud computing and supercharge your skills! This practical guide is packed with step-by-step tutorials, real-world use cases, and the latest Azure features to help you build, deploy, and manage scalable cloud apps like a pro. 🚀

🔥 Exclusive Deal Alert! Unlock amazing savings of 34% today! 🎉 Don’t miss this chance to learn Azure while saving big.

👇 Click now to claim your discount and start your Azure journey! 👇
👉 Grab Your 34% Discount Now!

Hurry—this offer won't last forever! ⏳

Comments

Popular posts from this blog

Spring Boot OpenAI Integration: Step-by-Step Guide

Orchestration-Based Saga Architecture and Spring Boot Microservices Implementation Guide

Spring Boot 3 + Angular 15 + Material - Full Stack CRUD Application Example