Skip to content

Step 3 - Trigger Deployment

In complex systems, it's often better to separate the "Build" logic from the "Deploy" logic. In this step, we push the image to a Development repository and then trigger a separate deployment job.

Jenkinsfile

Here is the Jenkinsfile for this step. Source code: 30-03-Jenkinsfile-docker-build-push-dev-repository-trigger-dev-deploy.

pipeline {
  agent any
  options {
    disableConcurrentBuilds()
    disableResume()
    buildDiscarder(logRotator(numToKeepStr: '10'))
    timeout(time: 1, unit: 'HOURS')
  }
  tools {
    maven 'maven-3.6.3' 
  }
  environment {
    DOCKER_REGISTRY = "vigneshsweekaran.jfrog.io"
    DOCKER_REPOSITORY = "docker-helloworld-dev-local"
    IMAGE_NAME = "hello-world-java"
    IMAGE_TAG = "1.${BUILD_NUMBER}"
    DOCKER_CREDENTIAL_ID = "jfrog-credential"
  }
  stages {
    stage ('Build') {
      steps {
        sh 'mvn clean package'
      }
    }
    stage ('Docker Build') {
      steps {
        script {
          docker.build("${DOCKER_REGISTRY}/${DOCKER_REPOSITORY}/${IMAGE_NAME}:${IMAGE_TAG}")
        }
      }
    }
    stage ('Docker Push') {
      steps {
        script {
          docker.withRegistry("https://${DOCKER_REGISTRY}", "${DOCKER_CREDENTIAL_ID}") {
            docker.image("${DOCKER_REGISTRY}/${DOCKER_REPOSITORY}/${IMAGE_NAME}:${IMAGE_TAG}").push() 
          }           
        }
      }
    }
    stage ('Trigger deployment') {
      steps {
        build wait: false, job: 'deploy',  parameters: [string(name: 'IMAGE_TAG', value: "${IMAGE_TAG}")]
      }
    }
  }
  post {
    always {
      deleteDir()
    }
  }
}

Detailed Explanation

Environment Block

We specifically target the docker-helloworld-dev-local repository here. This ensures that every build initially goes to the Development repository, preventing untested code from reaching QA or Prod.

Trigger Deployment Stage

  • build step: Use this to trigger another Jenkins job (in this case, named 'deploy').
  • wait: false: We do not wait for the deployment to finish. The build job succeeds as soon as the deployment job is triggered.
  • parameters: We pass the IMAGE_TAG (the version we just built) to the deploy job, ensuring it deploys exactly what we just built.

Important Tips

Tip

Passing parameters between jobs is crucial for maintaining artifact consistency. Never rely on "latest" when triggering downstream jobs.

Next Step: Deploy from Multiple Repos

Quick Quiz

Quick Quiz

#

Which step is used to trigger another Jenkins job?

#

What does wait: false do when triggering a downstream job?

#

Why might you separate Build and Deploy into different jobs?

📬 DevopsPilot Weekly — Learn DevOps, Cloud & Gen AI the simple way.
👉 Subscribe here