Jenkins Parameterized Builds & Workspaces

Samson OladipoSamson Oladipo
5 min read

A. Jenkins Parameterized Builds :

A new DevOps Engineer has joined the team and he will be assigned some Jenkins-related tasks. Before that, the team wanted to test a simple parameterized job to understand the basic functionality of parameterized builds. He is given a simple parameterized job to build in Jenkins. Please find more details below:

  • Create a parameterized job which should be named as parameterized-job

  • Add a string parameter named Stage; its default value should be Build.

  • Add a choice parameter named env; its choices should be Development, Staging and Production.

  • Configure the job to execute a shell command, which should echo both parameter values (you are passing in the job).

  • Build the Jenkins job at least once with choice parameter value Production to make sure it passes.

Execution

Login to Jenkins:

  • Open your web browser and go to the Jenkins URL.

  • Use the provided username admin and password to log in.

Create Parameterized Job:

  • Click on New Item on the Jenkins dashboard.

  • Enter the item name as parameterized-job.

  • Select Freestyle project and click OK.

Add String Parameter:

  • In the job configuration, scroll down to the "Build Environment" section.

  • Check the box for This build is parameterized.

  • Click on "Add Parameter" and choose "String Parameter".

  • Set the name to Stage and provide a default value of Build.

Add Choice Parameter:

  • Click on "Add Parameter" again and choose "Choice Parameter".

  • Set the name to env.

  • Enter the choices as Development, Staging, and Production.

Configure Shell Command:

  • Scroll down to the "Build" section.

  • Click on "Add build step" and choose "Execute shell".

In the command box, enter the following script:

echo "Stage: ${Stage}"

echo "Environment: ${env}"

Save and Build:

  • Click on "Save" to save the job configuration.

  • Now, click on "Build Now" to build the job.

  • Choose Production as the value for the env parameter during the build.

Verify the Output:

  • After the build is complete, go to the build console output.

  • Verify that the script has echoed the values of the Stage and env parameters correctly.

B. Jenkins Workspaces :

Some developers are working on a common repository where they are testing some features for an application. They have three branches (excluding the master branch) in this repository where they are adding changes related to these different features. They want to test these changes on the Datacentre app servers so they need a Jenkins job using which they can deploy these different branches as per requirements. Configure a Jenkins job accordingly.

There is a Git repository named web_app on Gitea where developers are pushing their changes. It has three branches version1, version2 and version3 (excluding the master branch). You need not to make any changes in the repository.

  • Create a Jenkins job named app-job.

  • Configure this job to have a choice parameter named Branch with choices as given below:

    version1

    version2

    version3

  • Configure the job to fetch changes from above mentioned Git repository and make sure it fetches the changes from the respective branch which you are passing as a choice in the choice parameter while building the job. For example, (if you choose version1 then it must fetch and deploy the changes from branch version1).

  • Configure this job to use a custom workspace rather than a default workspace and a custom workspace directory should be created under /var/lib/jenkins (for example /var/lib/jenkins/version1) location rather than under any sub-directory etc. The job should use a workspace as per the value you will pass for the Branch parameter while building the job. For example, if you choose version1 while building the job then it should create a workspace directory called version1 and should fetch Git repository etc within that directory only.

  • Configure the job to deploy code (fetched from Git repository) on the storage server (in the datacentre) under the /var/www/html directory. Since it is a shared volume.

Execution

Click Manage Jenkins > Manage Plugins and click Available tab.

  • Search for Git & Publish over SSH plugin (Optional Gitea, SSH Agent), click "Download now and install after restart"

  • Click the checkbox Restart Jenkins when installation is complete.

After Jenkins restarts, set credentials for the Git user

  • Manage Jenkins > Manage Credentials, click Global under Stores scoped to Jenkins and Add Credentials.

Configure Publish Over SSH

  • Manage Jenkins > Configure System under Publish over SSH > SSH Servers click Add

Create & Configure Jenkins Job

  • Open your Jenkins dashboard

  • Click on "New Item" in the top left

  • Enter the name as "app-job" and select "Freestyle project"

  • Click "OK" to create the job

Configure the Choice Parameter

  • On the job configuration page, find the "General" section

  • Check the box for "This project is parameterized"

  • Click on the "Add Parameter" dropdown and select "Choice Parameter"

  • Set the name to "Branch"

  • Provide the choices:

    version1

    version2

    version3

  • Click "Save" to save the configuration

Configure Custom Workspace based on Choice Parameter

  • On the job configuration page, find the "Source Code Management" section

  • Select the version control system to Git

  • Provide the Gitea repository URL.

  • Select the credentials you created for the Git user

In the "Branches to build" or "Branch specifier" field, use the choice parameter you created.

${Branch}

On the job configuration page, find the "Advanced" section under the Choice Parameter.

  • Check the box for "Use custom workspace"

  • Specify the custom workspace directory using the following syntax

    /var/lib/jenkins/${Branch}

Under "Build Environment" still on the job configuration page, Configure SSH transfer to the Storage server

10
Subscribe to my newsletter

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

Written by

Samson Oladipo
Samson Oladipo

DevOps enthusiast sharing real-world insights. ๐Ÿš€ #Automation #CI/CD #InfrastructureAsCode