0
0
Jenkinsdevops~5 mins

Pipeline triggers and upstream/downstream in Jenkins - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes, one task needs to start only after another task finishes. Pipeline triggers help connect these tasks so they run in order automatically without manual work.
When you want a build to start automatically after a code test finishes successfully.
When you have a deployment task that should run only after the build task completes.
When you want to chain multiple jobs so they run one after another without manual intervention.
When you want to trigger a notification job after a deployment finishes.
When you want to split a big process into smaller jobs that run in sequence.
Config File - Jenkinsfile
Jenkinsfile
pipeline {
  agent any
  stages {
    stage('Build') {
      steps {
        echo 'Building the project'
      }
    }
    stage('Test') {
      steps {
        echo 'Running tests'
      }
    }
  }
  post {
    success {
      build job: 'Deploy', wait: false
    }
  }
}

This Jenkinsfile defines a simple pipeline with two stages: Build and Test.

After the pipeline succeeds, it triggers another job named 'Deploy' without waiting for it to finish.

This shows how to trigger a downstream job automatically after the current pipeline completes successfully.

Commands
This command manually triggers the 'Deploy' job in Jenkins to test if it runs correctly.
Terminal
jenkins-jobs build Deploy
Expected OutputExpected
Started build of job 'Deploy' #1\nWaiting for build to complete...\nBuild #1 SUCCESS\nFinished: SUCCESS
This command starts the main pipeline job 'BuildAndTest' which will run Build and Test stages, then trigger 'Deploy' automatically on success.
Terminal
jenkins-jobs build BuildAndTest
Expected OutputExpected
Started build of job 'BuildAndTest' #1\n[Pipeline] Start of Pipeline\n[Pipeline] echo\nBuilding the project\n[Pipeline] echo\nRunning tests\n[Pipeline] build\nTriggering job: Deploy\n[Pipeline] End of Pipeline\nFinished: SUCCESS
This command checks the build history of the 'Deploy' job to confirm it was triggered by the upstream pipeline.
Terminal
jenkins-jobs list-builds Deploy
Expected OutputExpected
Builds for job 'Deploy':\n#1 SUCCESS - triggered by BuildAndTest #1
Key Concept

If you remember nothing else from this pattern, remember: downstream jobs can be triggered automatically after upstream jobs finish to create smooth, connected pipelines.

Common Mistakes
Not setting the downstream job name correctly in the trigger step.
The pipeline will fail to trigger the downstream job, breaking the chain.
Use the exact job name in the build step, for example: build job: 'Deploy', wait: false
Forgetting to handle job success or failure before triggering downstream jobs.
Downstream jobs may run even if the upstream job failed, causing wasted work or errors.
Use post conditions like 'post { success { ... } }' to trigger downstream jobs only on success.
Triggering downstream jobs synchronously without 'wait: false' when not needed.
This causes the upstream job to wait and possibly slow down the pipeline unnecessarily.
Add 'wait: false' to trigger downstream jobs asynchronously unless you need to wait for their result.
Summary
Use the 'build' step in Jenkins pipelines to trigger downstream jobs automatically.
Place triggers inside 'post' blocks to control when downstream jobs run based on success or failure.
Verify downstream jobs run by checking their build history after triggering the upstream pipeline.