How to Deploy a Spring Boot Application on Azure App Service with Jenkins CI/CD - Step-by-Step Guide

Explanation:

  1. Jenkins: Represents the Jenkins CI/CD pipeline, including steps like building the application with Maven and deploying it to Azure.
  2. GitHub: Represents the GitHub repository that contains the Spring Boot source code.
  3. Azure App Service: Represents the Azure service hosting the Spring Boot application after it's deployed from Jenkins.


To deploy a Spring Boot application on Azure App Service using Jenkins for CI/CD (Continuous Integration and Continuous Deployment), follow this end-to-end example:

Prerequisites:

  1. Azure Account: You need an active Azure account. If you don’t have one, you can create it here.
  2. Jenkins Server: A Jenkins instance up and running (either locally or in the cloud).
  3. Spring Boot Application: A Spring Boot project ready for deployment.
  4. Azure CLI: Installed on your Jenkins server (or use Azure DevOps pipelines for automated setup).

Step 1: Set up Azure App Service

  1. Create an App Service Plan:

    • Go to the Azure Portal.
    • Search for App Services and click Create.
    • Fill in the required details like subscription, resource group, and region.
    • Select the appropriate App Service Plan for your Spring Boot app.
  2. Create a Web App:

    • In the App Services section, click Create.
    • Provide a name for your web app, choose the runtime stack (Java 11 or higher), and select your App Service Plan.
    • Once the app is created, you’ll get a URL to access your app (e.g., https://your-app-name.azurewebsites.net).
  3. Enable Deployment from GitHub or Jenkins:

    • Go to the Deployment Center in the Azure portal for your web app.
    • Select Jenkins or GitHub as the source, and provide necessary configurations (you may need to configure Jenkins integration later).

Step 2: Set up Jenkins

  1. Install Jenkins Plugins:

    • Ensure that Jenkins has the required plugins:
      • Azure CLI Plugin: For deploying to Azure.
      • Git Plugin: For pulling your Spring Boot code from a Git repository.
      • Maven/Gradle Plugin: Depending on your project build tool.
      • Pipeline Plugin: For CI/CD pipeline setup.
  2. Configure Azure CLI on Jenkins:

    • Install Azure CLI on your Jenkins server (if not already installed).
    • On Jenkins, configure the Azure CLI plugin:
      • Go to Manage JenkinsConfigure System.
      • Under Azure CLI, provide the credentials (Client ID, Secret, Tenant ID) for your Azure account (these can be obtained by registering an application in Azure Active Directory).
  3. Configure Jenkins Credentials:

    • Go to Manage JenkinsManage CredentialsGlobal Credentials and add your Azure Service Principal credentials for authentication.
    • If you use GitHub, add GitHub credentials as well.

Step 3: Configure Your Jenkins Pipeline (Jenkinsfile)

  1. Create a Jenkinsfile: This file defines your CI/CD pipeline for Jenkins. It should be in the root of your Spring Boot project.

    Here's an example Jenkinsfile for a Spring Boot application:

    pipeline {
        agent any
    
        environment {
            AZURE_SUBSCRIPTION_ID = 'your-azure-subscription-id'
            AZURE_TENANT_ID = 'your-azure-tenant-id'
            AZURE_CLIENT_ID = 'your-azure-client-id'
            AZURE_CLIENT_SECRET = 'your-azure-client-secret'
            APP_NAME = 'your-app-name'
            RESOURCE_GROUP = 'your-resource-group'
        }
    
        stages {
            stage('Checkout') {
                steps {
                    // Checkout the code from GitHub or any repository
                    git 'https://github.com/your-repo/your-spring-boot-app.git'
                }
            }
    
            stage('Build') {
                steps {
                    script {
                        // Build the Spring Boot app with Maven or Gradle
                        sh 'mvn clean package -DskipTests'
                    }
                }
            }
    
            stage('Deploy to Azure') {
                steps {
                    script {
                        // Login to Azure
                        sh '''
                        az login --service-principal -u $AZURE_CLIENT_ID -p $AZURE_CLIENT_SECRET --tenant $AZURE_TENANT_ID
                        az account set --subscription $AZURE_SUBSCRIPTION_ID
                        '''
                        
                        // Deploy the built jar file to Azure App Service
                        sh '''
                        az webapp deploy --resource-group $RESOURCE_GROUP --name $APP_NAME --src-path target/your-app.jar --type jar
                        '''
                    }
                }
            }
    
            stage('Clean Up') {
                steps {
                    // Clean up the workspace (optional)
                    cleanWs()
                }
            }
        }
    
        post {
            success {
                echo 'Deployment to Azure App Service was successful!'
            }
            failure {
                echo 'Deployment failed!'
            }
        }
    }
    • This Jenkinsfile defines:
      • Checkout: Pulls your Spring Boot code from the Git repository.
      • Build: Builds the project using Maven (you can replace mvn with gradle if you use Gradle).
      • Deploy to Azure: Deploys the generated JAR file to Azure App Service.
      • Clean Up: Cleans the workspace after the job.
  2. Configure the Jenkins Pipeline Job:

    • In Jenkins, create a new Pipeline job.
    • In the Pipeline Script section, select Pipeline from SCM and point it to your GitHub repository where the Jenkinsfile is located.

Step 4: Trigger the Pipeline

  1. Manual Trigger:
    • Once the Jenkins job is set up, you can trigger it manually by clicking Build Now in Jenkins.
  2. Automatic Trigger:
    • To automate the process, you can configure Jenkins to trigger on GitHub push events (e.g., when code is pushed to the main branch). This can be done by setting up GitHub Webhooks in the Jenkins job configuration.

Step 5: Monitor the Deployment

  • After Jenkins runs the pipeline, check the Azure App Service for the deployed application.
  • You can monitor logs in the Azure portal under App Service Logs or use the Azure CLI for more details.

Optional: Using Azure DevOps

If you'd like to integrate Azure DevOps for CI/CD, you can use Azure Pipelines instead of Jenkins. Azure Pipelines supports Spring Boot apps natively and integrates seamlessly with Azure App Services.


Summary:

This end-to-end guide shows how to set up Jenkins CI/CD for a Spring Boot application, deploying it to Azure App Service. The pipeline:

  1. Checks out the code from GitHub.
  2. Builds the project using Maven.
  3. Deploys the JAR file to Azure App Service.
  4. Cleans up the workspace and provides deployment status.

Unlock Your Microservices Mastery for Only $9!

Get your copy now for just $9! and start building resilient and scalable microservices with the help of Microservices with Spring Boot 3 and Spring Cloud.

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