DEV Community

Janak Shrestha
Janak Shrestha

Posted on

Day 72: Jenkins Parameterized Builds

Some new requirements have come up to install and configure some packages on the Nautilus infrastructure under Stratos Datacenter. The Nautilus DevOps team installed and configured a new Jenkins server so they wanted to create a Jenkins job to automate this task. Find below more details and complete the task accordingly:

  1. Access the Jenkins UI by clicking on the Jenkins button in the top bar. Log in using the credentials: username admin and password Adm!n321.

  2. Create a new Jenkins job named install-packages and configure it with the following specifications:

  • Add a string parameter named PACKAGE.
  • Configure the job to install a package specified in the $PACKAGE parameter on the storage server (Stratos Datacenter).
  • Build the job at least once (e.g. with parameter PACKAGE=vim-enhanced) so the package is installed on the Storage server and can be verified.

Note:

  1. Ensure to install any required plugins and restart the Jenkins service if necessary. Opt for Restart Jenkins when installation is complete and no jobs are running on the plugin installation/update page. Refresh the UI page if needed after restarting the service.
  2. Verify that the Jenkins job runs successfully on repeated executions to ensure reliability.
  3. Capture screenshots of your configuration for documentation and review purposes. Alternatively, use screen recording software like loom.com for comprehensive documentation and sharing.

Step-by-Step Solution

Step 1: Access Jenkins UI

  1. Click the Jenkins button on the top bar
  2. Login with:
    • Username: admin
    • Password: Adm!n321

Step 2: Install Required Plugin (Publish Over SSH)

  1. Navigate to Manage JenkinsPlugins
  2. Click on Available plugins
  3. Search for Publish Over SSH
  4. Check the box and click Install without restart or Download now and install after restart
  5. Optionally select Restart Jenkins when installation is complete and no jobs are running

Step 3: Configure SSH Credentials for Storage Server

Option A: Using Publish Over SSH Plugin (Recommended)

  1. Go to Manage JenkinsConfigure System
  2. Scroll to Publish over SSH section
  3. Click Add next to SSH Servers
  4. Configure:
    • Name: storage-server
    • Hostname: ststor01.stratos.xfusioncorp.com
    • Username: natasha
    • Remote Directory: /tmp
  5. Under Advanced, check Use password authentication
  6. Enter the password: Bl@kW
  7. Click Test Configuration to verify
  8. Click Save

Option B: Configure Passwordless SSH (Alternative)

On the Jenkins server:

# Generate SSH key
ssh-keygen -t rsa -b 4096 -N ""

# Copy public key to storage server
ssh-copy-id natasha@ststor01
Enter fullscreen mode Exit fullscreen mode

On the Storage server:

# Add sudoers entry for passwordless sudo
sudo visudo
# Add:
natasha ALL=(ALL) NOPASSWD: ALL
Enter fullscreen mode Exit fullscreen mode

Step 4: Create the Jenkins Job

  1. From the Jenkins dashboard, click New Item
  2. Enter job name: install-packages
  3. Select Freestyle project
  4. Click OK

Step 5: Configure Job Parameters

  1. Under General, check This project is parameterized
  2. Click Add ParameterString Parameter
  3. Configure:
    • Name: PACKAGE
    • Default Value: vim-enhanced (or leave blank)
    • Description: Enter the package name to install on the storage server
  4. Click Save

Step 6: Configure Build Step (Using Publish Over SSH)

  1. Scroll to Build section
  2. Click Add build stepSend files or execute commands over SSH
  3. Configure:
    • Name: Select storage-server
    • Exec command:
   sudo yum install -y $PACKAGE
Enter fullscreen mode Exit fullscreen mode
  1. Click Save

Step 6 (Alternative): Configure Build Step (Using Shell Script)

If not using the plugin, add a shell build step:

# Install sshpass if not present
which sshpass || sudo apt-get install -y sshpass

# Run the installation
sshpass -p 'Bl@kW' ssh -o StrictHostKeyChecking=no natasha@ststor01 "echo 'Bl@kW' | sudo -S yum install -y $PACKAGE"
Enter fullscreen mode Exit fullscreen mode

Step 7: Build the Job with Parameters

  1. From the job page, click Build with Parameters
  2. Enter:
    • PACKAGE: vim-enhanced
  3. Click Build

Step 8: Verify the Build

  1. Check the Console Output to ensure the installation succeeded
  2. Verify on the Storage server:
# SSH to storage server
ssh natasha@ststor01

# Verify package is installed
rpm -q vim-enhanced
Enter fullscreen mode Exit fullscreen mode

Complete Job Configuration Summary

Setting Value
Job Name install-packages
Job Type Freestyle project
Parameter Name PACKAGE
Parameter Type String Parameter
SSH Server storage-server
Build Step Send files or execute commands over SSH
Exec Command sudo yum install -y $PACKAGE

Expected Console Output

Started by user admin
Running as SYSTEM
Building in workspace /var/lib/jenkins/workspace/install-packages
[SSH] script:
sudo yum install -y vim-enhanced

[SSH] executing...
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package vim-enhanced.x86_64 2:8.0.1763-19.el8 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
Installing:
 vim-enhanced        x86_64        2:8.0.1763-19.el8        base        1.5 M
Installed:
  vim-enhanced.x86_64 2:8.0.1763-19.el8

Complete!
[SSH] completed
[SSH] exit-status: 0
Finished: SUCCESS
Enter fullscreen mode Exit fullscreen mode

Verification Commands

# On the Storage server - verify package installation
rpm -q vim-enhanced

# Expected output:
# vim-enhanced-8.0.1763-19.el8.x86_64
Enter fullscreen mode Exit fullscreen mode

Troubleshooting

Issue 1: SSH Connection Failed

Solution: Verify credentials and ensure the storage server is reachable:

ssh natasha@ststor01
Enter fullscreen mode Exit fullscreen mode

Issue 2: sudo Requires Password

Error: sudo: a terminal is required to read the password

Solution: Use -S flag to read password from stdin:

echo 'Bl@kW' | sudo -S yum install -y $PACKAGE
Enter fullscreen mode Exit fullscreen mode

Or add passwordless sudo on the storage server:

sudo visudo
# Add:
natasha ALL=(ALL) NOPASSWD: ALL
Enter fullscreen mode Exit fullscreen mode

Issue 3: Package Already Installed

Solution: The -y flag handles this gracefully. The job will succeed with a message indicating the package is already installed.

Issue 4: Plugin Not Available

Solution: Ensure plugins are installed and Jenkins is restarted:

  1. Go to Manage JenkinsPlugins
  2. Install Publish Over SSH
  3. Restart Jenkins

Top comments (0)