Deploy a Spring Boot Application on Azure App Service Using GitHub Actions CI/CD

Follow this step-by-step guide to deploy your Spring Boot application to Azure App Service using GitHub Actions for seamless CI/CD.

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



Step 1: Prerequisites

  1. Set Up Azure Resources:

    • Azure App Service: Create an App Service instance for your application.
    • Azure Container Registry (ACR): Create a registry for storing your application’s container image.
  2. Prepare Your GitHub Repository:

    • Ensure your Spring Boot application source code is in a GitHub repository.
    • Include a Dockerfile in your project for containerization (see Step 2).
  3. Install Azure CLI:

    • Download and install Azure CLI.
    • Log in to Azure:
      az login
  4. Enable Admin User in ACR:

    • Go to the Azure portal > ACR > Settings > Access Keys.
    • Enable Admin user and note down the username and password.

Step 2: Add a Dockerfile

  1. Navigate to the root of your Spring Boot application project.
  2. Create a Dockerfile with the following content:
    FROM eclipse-temurin:17-jdk-alpine AS build
    WORKDIR /app
    COPY . .
    RUN ./mvnw package -DskipTests
    
    FROM eclipse-temurin:17-jre-alpine
    WORKDIR /app
    COPY --from=build /app/target/*.jar app.jar
    ENTRYPOINT ["java", "-jar", "app.jar"]
    Replace eclipse-temurin:17 with your required JDK version if different. Adjust mvnw commands based on your build process.

Step 3: Push Your Code to GitHub

  1. Add and commit your changes:
    git add .
    git commit -m "Add Dockerfile for containerization"
    git push origin main

Step 4: Configure Azure Container Registry

  1. Log in to ACR:

    az acr login --name <ACR_NAME>
  2. Get ACR Login Server:

    az acr show --name <ACR_NAME> --query "loginServer" --output tsv

    Note down the loginServer (e.g., myregistry.azurecr.io).


Step 5: Create a GitHub Actions Workflow

  1. Create a .github/workflows/ci-cd.yml file in your GitHub repository.
  2. Add the following content:
    name: CI/CD Pipeline for Spring Boot
    
    on:
      push:
        branches:
          - main
    
    jobs:
      build:
        runs-on: ubuntu-latest
        steps:
          - name: Checkout code
            uses: actions/checkout@v3
    
          - name: Log in to Azure Container Registry
            uses: azure/docker-login@v1
            with:
              login-server: <ACR_LOGIN_SERVER>
              username: ${{ secrets.AZURE_ACR_USERNAME }}
              password: ${{ secrets.AZURE_ACR_PASSWORD }}
    
          - name: Build and Push Docker Image
            run: |
              docker build -t <ACR_LOGIN_SERVER>/<IMAGE_NAME>:${{ github.sha }} .
              docker push <ACR_LOGIN_SERVER>/<IMAGE_NAME>:${{ github.sha }}
    
      deploy-dev:
        needs: build
        runs-on: ubuntu-latest
        steps:
          - name: Deploy to Azure App Service (DEV)
            uses: azure/webapps-deploy@v2
            with:
              app-name: <DEV_APP_SERVICE_NAME>
              slot-name: Production
              images: |
                <ACR_LOGIN_SERVER>/<IMAGE_NAME>:${{ github.sha }}
    
      deploy-qa:
        needs: deploy-dev
        runs-on: ubuntu-latest
        if: success()
        steps:
          - name: Deploy to Azure App Service (QA)
            uses: azure/webapps-deploy@v2
            with:
              app-name: <QA_APP_SERVICE_NAME>
              slot-name: Production
              images: |
                <ACR_LOGIN_SERVER>/<IMAGE_NAME>:${{ github.sha }}
    
      deploy-prod:
        needs: deploy-qa
        runs-on: ubuntu-latest
        if: success()
        steps:
          - name: Deploy to Azure App Service (PROD)
            uses: azure/webapps-deploy@v2
            with:
              app-name: <PROD_APP_SERVICE_NAME>
              slot-name: Production
              images: |
                <ACR_LOGIN_SERVER>/<IMAGE_NAME>:${{ github.sha }}
    Replace placeholders (<ACR_LOGIN_SERVER>, <IMAGE_NAME>, <DEV_APP_SERVICE_NAME>, etc.) with actual values.

Step 6: Configure GitHub Secrets

  1. In your GitHub repository, go to Settings > Secrets and Variables > Actions.
  2. Add the following secrets:
    • AZURE_ACR_USERNAME: Your ACR username.
    • AZURE_ACR_PASSWORD: Your ACR password.
    • AZURE_APP_SERVICE_PUBLISH_PROFILE_DEV: Publish profile for the DEV App Service.
    • AZURE_APP_SERVICE_PUBLISH_PROFILE_QA: Publish profile for the QA App Service.
    • AZURE_APP_SERVICE_PUBLISH_PROFILE_PROD: Publish profile for the PROD App Service.

Step 7: Test the CI/CD Pipeline

  1. Push a code change to the main branch.
  2. Monitor the Actions tab in GitHub for pipeline execution.
  3. Validate deployment in DEV, QA, and finally in PROD.

Step 8: Scale and Monitor the Application

  1. Monitor Logs:

    • Use the Azure portal or az webapp log commands to check application logs.
  2. Scale Resources:

    • Scale your App Service Plan based on traffic requirements.

🎉 You’re Done! With this setup, your Spring Boot application is seamlessly deployed to Azure App Service with GitHub Actions handling the CI/CD workflow.

🌟 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! ⏳

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete