Azure Pipeline for Java build - test and deploy to staging

 # Trigger the pipeline on changes to the main branch

trigger:

  branches:

    include:

      - main


# Define the VM image to use for the pipeline

pool:

  vmImage: 'ubuntu-latest'


# Pipeline steps

stages:

  - stage: Build

    jobs:

      - job: Build

        steps:

          # Checkout the code from the repository

          - checkout: self


          # Set up Maven

          - task: UseMaven@1

            inputs:

              mavenVersionOption: 'Default'

              mavenPomFile: 'pom.xml'

              goals: 'clean compile'


          # Run tests

          - task: Maven@3

            inputs:

              mavenPomFile: 'pom.xml'

              goals: 'test'


          # Package the application

          - task: Maven@3

            inputs:

              mavenPomFile: 'pom.xml'

              goals: 'package'


          # Publish the build artifact (JAR file)

          - publish: $(System.DefaultWorkingDirectory)/target/*.jar

            artifact: drop


  - stage: DeployToStaging

    dependsOn: Build

    jobs:

      - job: Deploy

        steps:

          # Download the artifact from the previous stage

          - download: current

            artifact: drop


          # Deploy the application to Azure App Service (Staging)

          - task: AzureWebApp@1

            inputs:

              azureSubscription: '<Your-Azure-Subscription>'

              appName: '<Your-App-Service-Name>'

              package: $(Pipeline.Workspace)/drop/*.jar

              slotName: 'staging'  # Deploy to the staging slot


Comments