Skip to main content

Command Palette

Search for a command to run...

Day 78: Jenkins Conditional Pipeline | 100 Days of DevOps

Updated
โ€ข8 min read
R
Iโ€™m currently working in DevOps and documenting my learning journey along the way. From CI/CD pipelines to cloud and containers, Iโ€™m exploring how modern systems are built and deployed. This blog is where I share what I learn, break down concepts in simple terms, and track my progress. Learning one concept at a time, and trying to apply it practically.

Content

Today I worked on creating a Jenkins Conditional Pipeline for xFusionCorp Industries. The objective was to deploy different branches of a static website repository based on a Jenkins parameter. Instead of cloning the repository during each build, the repository was already available on the target application server, and Jenkins had to switch between branches and deploy the appropriate version of the website.


๐Ÿ”น What I Learned

  • Jenkins Conditional Pipelines
  • Jenkins Pipeline Parameters
  • Configuring Jenkins SSH Agents
  • Managing Jenkins Credentials
  • Using Existing Git Repositories on Remote Servers
  • Working with Branch-Based Deployments
  • Deploying Applications Based on Runtime Parameters
  • Understanding Git Branch Switching in CI/CD Pipelines
  • Automating Conditional Deployments

๐Ÿ”น Task Requirement

The development team at xFusionCorp Industries wanted Jenkins to deploy different versions of a static website depending on the branch selected by the user during build execution.

The repository already existed on App Server 1 under:

/var/www/html

The Jenkins Pipeline needed to:

  • Accept a parameter named BRANCH
  • Deploy the master branch when master is selected
  • Deploy the feature branch when feature is selected
  • Use a single stage named Deploy
  • Execute deployment on App Server 1 through a Jenkins Agent

Jenkins Access Details

Field Value
Username admin
Password Adm!n321

Gitea Access Details

Field Value
Username sarah
Password Sarah_pass123

Deployment Requirements

Requirement Value
Pipeline Job datacenter-webapp-job
Agent Name App Server 1
Agent Label stapp01
Repository web_app
Existing Repository Path /var/www/html
Agent Remote Root Directory /home/sarah/jenkins_agent
Pipeline Stage Deploy

๐Ÿ”น Steps I Followed

1. Verified Existing Repository on App Server

Connected to App Server 1:

ssh sarah@stapp01

Authenticated using:

Sarah_pass123

Moved to the repository directory:

cd /var/www/html

Verified repository contents:

ls -la

Output:

.git
feature.html
index.html

Checked available branches:

git branch

Output:

* feature
  master

This confirmed that the repository already existed and contained both required branches.


2. Installed Java on App Server

Verified Java installation:

java -version

Installed OpenJDK 17:

sudo yum install -y java-17-openjdk

This was required because Jenkins agents need Java to communicate with the Jenkins controller.


3. Created Jenkins Agent Directory

The task required the Jenkins Agent Remote Root Directory to be:

/home/sarah/jenkins_agent

Created the directory:

sudo mkdir -p /home/sarah/jenkins_agent

Assigned permissions:

sudo chmod 777 /home/sarah/jenkins_agent

Verified:

ls -la /home/sarah/jenkins_agent

Output:

drwxrwxrwx ...

4. Accessed Jenkins

Clicked the Jenkins button available in the lab environment.

Logged in using:

Username: admin
Password: Adm!n321

Successfully accessed the Jenkins Dashboard.


5. Installed Required Plugins

Navigated to:

Manage Jenkins
โ†’ Plugins

Installed:

  • SSH Build Agents
  • Pipeline
  • Git

After installation:

  • Restarted Jenkins
  • Refreshed the browser
  • Logged in again

6. Added Jenkins Credentials

Navigated to:

Manage Jenkins
โ†’ Credentials
โ†’ System
โ†’ Global Credentials
โ†’ Add Credentials

Configured:

Field Value
Kind Username with Password
Username sarah
Password Sarah_pass123
ID stapp01

Saved the credentials.


7. Configured Jenkins Agent

Navigated to:

Manage Jenkins
โ†’ Nodes
โ†’ New Node

Configured:

Field Value
Name App Server 1
Remote Root Directory /home/sarah/jenkins_agent
Labels stapp01
Launch Method Launch agents via SSH
Host stapp01
Credentials stapp01
Host Key Verification Non-verifying Verification Strategy

Saved the configuration.

Jenkins successfully connected to the remote server.

Agent status:

Connected

8. Created Jenkins Pipeline Job

Navigated to:

New Item

Entered:

datacenter-webapp-job

Selected:

Pipeline

Clicked:

OK

9. Added Build Parameter

Enabled:

This project is parameterized

Added:

String Parameter

Configured:

Field Value
Name BRANCH
Default Value master

This parameter would determine which branch gets deployed.


10. Added Pipeline Script

Under:

Pipeline
โ†’ Definition
โ†’ Pipeline Script

Added:

pipeline {
    agent { label 'stapp01' }

    parameters {
        string(
            name: 'BRANCH',
            defaultValue: 'master'
        )
    }

    stages {
        stage('Deploy') {
            steps {
                sh """
                cd /var/www/html

                if [ "${params.BRANCH}" = "master" ]; then
                    git checkout master
                    git pull origin master

                elif [ "${params.BRANCH}" = "feature" ]; then
                    git checkout feature
                    git pull origin feature

                else
                    echo "Invalid branch"
                    exit 1
                fi
                """
            }
        }
    }
}

11. Understanding the Pipeline

Agent Section

agent { label 'stapp01' }

Ensures the pipeline runs only on the configured Jenkins agent.


Parameter Section

parameters {
    string(name: 'BRANCH')
}

Allows users to choose which branch to deploy.


Deploy Stage

stage('Deploy')

The task explicitly required a single stage named:

Deploy

(case-sensitive)


Branch-Based Deployment

If the parameter is:

master

Pipeline executes:

git checkout master
git pull origin master

If the parameter is:

feature

Pipeline executes:

git checkout feature
git pull origin feature

This provides conditional deployment based on user input.


12. Saved the Job

Clicked:

Apply

Then:

Save

Pipeline creation completed successfully.


13. Tested Master Branch Deployment

Opened:

datacenter-webapp-job
โ†’ Build with Parameters

Selected:

BRANCH = master

Build completed successfully.

Verified on App Server:

cd /var/www/html

git branch

Output:

  feature
* master

14. Tested Feature Branch Deployment

Opened:

datacenter-webapp-job
โ†’ Build with Parameters

Selected:

BRANCH = feature

Build completed successfully.

Verified:

cd /var/www/html

git branch

Output:

* feature
  master

This confirmed conditional deployment was functioning correctly.


15. Verified Application

Clicked the:

App

button available in the lab environment.

Verified the application loaded successfully from:

/var/www/html

and not from a subdirectory.


๐Ÿ”น Simple Explanation of Jenkins Components Used

Jenkins Agent

A Jenkins Agent is a remote machine that executes build and deployment tasks.

In this task:

App Server 1

acted as the Jenkins Agent.


Agent Labels

Labels help Jenkins determine where jobs should run.

Example:

agent {
    label 'stapp01'
}

This ensures deployment occurs only on App Server 1.


Pipeline Parameters

Pipeline Parameters allow user input during build execution.

Example:

BRANCH = master

or

BRANCH = feature

This makes deployments dynamic and reusable.


Declarative Pipeline

The Jenkinsfile follows Declarative Pipeline syntax:

pipeline {
    agent
    parameters
    stages
}

This structure improves readability and maintainability.


Git Branch Switching

Instead of cloning a repository every time, the pipeline operates directly on the existing repository:

git checkout master

or

git checkout feature

This is faster and aligns with the repository layout provided by the task.


Continuous Deployment

The deployment process is fully automated:

  1. User selects a branch
  2. Jenkins connects to the agent
  3. Pipeline switches repository branch
  4. Latest code becomes available through Apache
  5. Application is immediately updated

๐Ÿ”น My Understanding

This task helped me understand how Jenkins can perform conditional deployments using parameters. Instead of maintaining separate jobs for different branches, a single pipeline can dynamically deploy multiple versions of an application based on user input. It also reinforced the importance of understanding whether a repository already exists on the target server before deciding between cloning code or operating directly on an existing Git repository.


๐Ÿ”น What I Found Interesting

I found it interesting that the entire deployment logic could be controlled through a single Jenkins parameter. By simply changing the value of BRANCH, Jenkins was able to deploy different application versions without requiring multiple jobs. This demonstrated how flexible and powerful Jenkins Pipelines can be when combined with Git branch management and remote agents.

๐Ÿ“Œ Full notes: GitHub link

100_days_of_devops

Part 1 of 50

Documenting my 100 Days of DevOps journey with hands-on practice from KodeKloud, along with notes, commands, and mini projects