πŸš€ Understanding Upstream and Downstream Jobs in Jenkins

Ankit RajAnkit Raj
3 min read

Jenkins is a fantastic tool for automating your workflows. One of its coolest features is upstream and downstream jobs, which help manage dependencies between tasks. Let’s dive in to learn what they are and how to use themβ€”step by step, with emojis! πŸ› οΈ


πŸ€” What Are Upstream and Downstream Jobs?

  • πŸ”Ό Upstream Job: A job that triggers another job when it completes.

  • πŸ”½ Downstream Job: A job that is triggered by another job.

🧩 How Do They Work Together?

Imagine a pipeline with three tasks:

  1. Job A (πŸ”Ό Upstream): Builds the application.

  2. Job B (πŸ”½ Downstream): Tests the build.

Job A β†’ Job B

Simple, right? Let's set them up! πŸ’‘


πŸ› οΈ Setting Up Upstream and Downstream Jobs

1️⃣ Create the Upstream Job

  1. Go to your Jenkins Dashboard.

  2. Click New Item πŸ†•.

  3. Enter a name (e.g., A πŸ› οΈ) and select Pipeline.

  4. Add your configuration for the build (e.g., source code checkout, build steps).

  5. Save the job βœ….

pipeline{
    agent any
    stages{
        stage('Hello'){
            steps{
                echo "Hello from A"
            }
        }
        post{
            success{
                    build job: 'B'
            }
        }
    }
}

2️⃣ Create the Downstream Job

  1. Go back to your Jenkins Dashboard.

  2. Click New Item πŸ†• again.

  3. Enter a name (e.g., BπŸ§ͺ) and select Pipeline.

  4. Add your configuration for testing (e.g., run unit tests).

  5. Save the job βœ….

pipeline{
    agent any
    stages{
        stage('Hello'){
           steps{
                echo "Hello from B"
            }
        }
    }
}

πŸ”— Option 1: Post-Build Action in Upstream Job

  1. Open the Upstream Job (A πŸ› οΈ).

  2. Go to the Post-build Actions section.

  3. Choose Build other projects.

  4. Enter the downstream job name (e.g., B πŸ§ͺ).

  5. Select the trigger condition:

    • Only if build is stable (recommended 🟒).

    • Even if the build is unstable or fails πŸ”΄.

  6. Save the configuration βœ….

Now, B πŸ§ͺ will automatically trigger after AπŸ› οΈ finishes! πŸŽ‰

πŸ”— Option 2: Build Trigger in Downstream Job

  1. Open the Downstream Job (B πŸ§ͺ).

  2. Go to the Build Triggers section.

  3. Check Build after other projects are built.

  4. Enter the upstream job name (e.g., A πŸ› οΈ).

  5. Save the configuration βœ….

πŸ”„ This setup creates the same dependency but from the downstream job's perspective.


πŸ” Visualizing Job Dependencies

Jenkins makes it easy to see how jobs are connected:

  • Upstream Projects: Shows jobs that trigger the current job πŸ”Ό.

  • Downstream Projects: Shows jobs triggered by the current job πŸ”½.

2️⃣ Graphical View

  1. Install the Dependency Graph Viewer plugin πŸ“Š:

    • Go to Manage Jenkins > Manage Plugins > Available.

    • Search for and install Dependency Graph Viewer.

  2. View the graph under Project Relationships.


🌟 Best Practices for Upstream and Downstream Jobs

  1. Separate Responsibilities:

    • Use upstream jobs for building πŸ› οΈ.

    • Use downstream jobs for testing πŸ§ͺ and deploying πŸš€.

  2. Use Notifications:

    • Configure email or Slack alerts πŸ“§ to stay informed about job statuses.
  3. Handle Failures Gracefully:

    • Ensure downstream jobs only trigger if upstream jobs succeed.
  4. Parallel Execution:

    • Run multiple downstream jobs simultaneously to save time ⏱️.

✨ Real-World Use Cases

  1. Continuous Integration (CI):

    • Upstream Job: Compile and package the app πŸ“¦.

    • Downstream Job: Run unit and integration tests πŸ§ͺ.

  2. Continuous Deployment (CD):

    • Upstream Job: Test the app πŸ§ͺ.

    • Downstream Job: Deploy to staging and production πŸš€.


πŸŽ‰ Conclusion

By mastering upstream and downstream jobs, you can orchestrate complex workflows in Jenkins with ease. Whether you’re automating a CI/CD pipeline or managing dependencies, this feature ensures smooth operations.

Ready to level up your Jenkins game? Start linking your jobs today! πŸ”—βœ¨

1
Subscribe to my newsletter

Read articles from Ankit Raj directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ankit Raj
Ankit Raj