Skip to main content

Command Palette

Search for a command to run...

Day 71: Jenkins Parameterized Jobs - Automated Package Installation 📦

Published
13 min read
Day 71: Jenkins Parameterized Jobs - Automated Package Installation 📦

Welcome back! 👋 Day 71 of the 100 Days Cloud DevOps Challenge, and today we're creating Jenkins parameterized jobs! This is automation evolution - building reusable jobs that accept runtime parameters for flexible execution. Let's automate! 🎯

🎯 The Mission - Create Parameterized Package Installation Job

📋 TASK TICKET #DEV-8071 - Automated Package Installation
Priority: HIGH | Type: Job Configuration
Server: Jenkins | Target: Storage Server

REQUIREMENT:
- Automate package installation across infrastructure
- Create reusable Jenkins job
- Accept package name as parameter
- Execute on storage server

IMPLEMENTATION:
1. Access Jenkins UI
   - Username: admin
   - Password: Adm!n321

2. Create Jenkins Job:
   - Job Name: install-packages
   - Type: Freestyle project
   - Parameter: PACKAGE (String)
   - Action: Install package on storage server

3. Configuration:
   - String parameter named PACKAGE
   - Execute installation command
   - Target: storage server (Stratos Datacenter)

SUCCESS CRITERIA:
- Job created successfully
- Parameter configuration working
- Can install packages on storage server
- Repeatable executions successful

This is infrastructure automation! 🔧

🤔 Why Parameterized Jobs?

Without Parameters:

❌ Separate job for each package
❌ Job duplication
❌ Hard to maintain
❌ Limited flexibility

Example:
- install-nginx (separate job)
- install-docker (separate job)
- install-git (separate job)
→ 100+ packages = 100+ jobs!

With Parameters:

✅ One reusable job
✅ Runtime customization
✅ Easy maintenance
✅ Flexible execution

Example:
- install-packages job
  → Run with PACKAGE=nginx
  → Run with PACKAGE=docker
  → Run with PACKAGE=git
→ 100+ packages = 1 job!

🛠️ Complete Implementation

Step 1: Access Jenkins UI

1. Click "Jenkins" button on top bar

2. Login page appears

3. Enter credentials:
   Username: admin
   Password: Adm!n321

4. Click "Sign in"

Logged in successfully!

Step 2: Create New Jenkins Job

From Jenkins Dashboard:

1. Click "New Item" (left sidebar)
   OR
   Click "Create a job" (if first job)

2. Enter job details:

New Item page:

┌─────────────────────────────────────────┐
│  Enter an item name                     │
├─────────────────────────────────────────┤
│  [install-packages]                     │
│                                         │
│  Select job type:                       │
│  ⦿ Freestyle project                    │
│  ○ Pipeline                             │
│  ○ Multi-configuration project         │
│  ○ Folder                               │
│  ○ Multibranch Pipeline                │
│                                         │
│  [OK]                                   │
└─────────────────────────────────────────┘

Enter:

Item name: install-packages
Select: Freestyle project

Click "OK"

Step 3: Configure Job - General Settings

Job Configuration page opens:

1. General section (top of page):
   - Description: (optional) "Automated package installation on storage server"
   - ☐ Discard old builds (optional)
   - ☐ GitHub project (leave unchecked)

Step 4: Configure String Parameter

In job configuration:

1. Find "This project is parameterized" checkbox

2. Check it: ☑ This project is parameterized

3. Click "Add Parameter" dropdown

4. Select "String Parameter"

5. Fill in parameter details:

String Parameter configuration:

┌─────────────────────────────────────────┐
│  String Parameter                       │
├─────────────────────────────────────────┤
│  Name:        [PACKAGE]                 │
│  Default:     [nginx]                   │
│               (optional)                │
│  Description: [Package name to install] │
│               (optional)                │
│  Trim:        ☑ Trim the string         │
└─────────────────────────────────────────┘

Enter exactly:

Name: PACKAGE
Default value: nginx (or leave blank)
Description: Package name to install on storage server
Trim the string: ☑ (checked)

Parameter configured!

Step 5: Configure Build Environment (Optional)

Build Environment section:

These are optional but useful:
- ☐ Delete workspace before build starts
- ☐ Use secret text(s) or file(s)
- ☐ Add timestamps to the Console Output ☑ (recommended)

Check "Add timestamps" for better logging

Step 6: Configure Build Steps

Scroll to "Build" section:

1. Click "Add build step" dropdown

2. Select "Execute shell"

3. Command text area appears

4. Enter installation script:

Execute shell command:

#!/bin/bash

# Package installation script for storage server

echo "========================================="
echo "Package Installation Job"
echo "========================================="
echo "Target Server: Storage Server (ststor01)"
echo "Package: $PACKAGE"
echo "Timestamp: $(date)"
echo "========================================="

# SSH to storage server and install package
ssh -o StrictHostKeyChecking=no natasha@ststor01 "sudo yum install -y $PACKAGE"

# Check installation status
if [ $? -eq 0 ]; then
    echo "========================================="
    echo "SUCCESS: Package $PACKAGE installed successfully!"
    echo "========================================="
else
    echo "========================================="
    echo "ERROR: Failed to install package $PACKAGE"
    echo "========================================="
    exit 1
fi

Important notes:

Server details:
- Storage server hostname: ststor01
- User: natasha
- Password: Bl@kW (stored in SSH keys)
- Package manager: yum

The $PACKAGE variable comes from the parameter

Alternative simpler version:

#!/bin/bash
echo "Installing package: $PACKAGE"
ssh natasha@ststor01 "sudo yum install -y $PACKAGE"

Step 7: Configure Post-Build Actions (Optional)

Post-build Actions section (optional):

Useful options:
1. Click "Add post-build action"

2. Select options like:
   - Email notification
   - Archive artifacts
   - Publish build status

For this task, can skip or add:
- "Editable Email Notification" (if plugin installed)

Step 8: Save Job Configuration

1. Scroll to bottom of page

2. Click "Save" button

3. Redirected to job page

Job created successfully!

Step 9: Configure SSH Credentials (If Needed)

If SSH keys not configured, need to setup:

1. Go to "Manage Jenkins"

2. Click "Manage Credentials"

3. Click "(global)" domain

4. Click "Add Credentials"

5. Configure:
   Kind: SSH Username with private key
   Username: natasha
   Private Key: Enter directly (paste key)
   ID: storage-server-ssh
   Description: Storage server SSH key

6. Click "Create"

Alternative: Use password authentication (less secure):
- Change SSH command to: sshpass -p 'Bl@kW' ssh natasha@ststor01 ...
- Need sshpass installed on Jenkins server

Step 10: Test Job Execution

From job page:

1. Click "Build with Parameters" (left sidebar)

2. Parameter form appears:

Build with Parameters page:

┌─────────────────────────────────────────┐
│  Build with Parameters                  │
├─────────────────────────────────────────┤
│  PACKAGE                                │
│  [nginx]                                │
│                                         │
│  [Build]                                │
└─────────────────────────────────────────┘

Test installations:

Test 1: Install nginx

PACKAGE: nginx
Click "Build"

Test 2: Install git

PACKAGE: git
Click "Build"

Test 3: Install wget

PACKAGE: wget
Click "Build"

Step 11: Monitor Build Execution

After clicking Build:

1. Build starts in "Build History" (left sidebar)

2. Click build number (e.g., #1)

3. Click "Console Output" to see logs

4. Watch installation progress

Console Output example:

Started by user admin
Running as SYSTEM
Building in workspace /var/lib/jenkins/workspace/install-packages
[install-packages] $ /bin/bash /tmp/jenkins12345.sh
=========================================
Package Installation Job
=========================================
Target Server: Storage Server (ststor01)
Package: nginx
Timestamp: Wed Jan 15 10:30:00 UTC 2026
=========================================

Connecting to ststor01...
Loaded plugins: fastestmirror, ovl
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package nginx.x86_64 1:1.20.1-10.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved
================================================================================
 Package       Arch           Version                   Repository        Size
================================================================================
Installing:
 nginx         x86_64         1:1.20.1-10.el7           epel             587 k

Transaction Summary
================================================================================
Install  1 Package

Total download size: 587 k
Installed size: 1.6 M
Downloading packages:
nginx-1.20.1-10.el7.x86_64.rpm                           | 587 kB  00:00:01
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : 1:nginx-1.20.1-10.el7.x86_64                                 1/1
  Verifying  : 1:nginx-1.20.1-10.el7.x86_64                                 1/1

Installed:
  nginx.x86_64 1:1.20.1-10.el7

Complete!
=========================================
SUCCESS: Package nginx installed successfully!
=========================================
Finished: SUCCESS

Build successful!

Step 12: Verify Repeated Executions

Test job repeatability:

1. Run build multiple times with same package:
   - Build #1: PACKAGE=nginx → SUCCESS
   - Build #2: PACKAGE=nginx → SUCCESS (already installed)
   - Build #3: PACKAGE=git → SUCCESS
   - Build #4: PACKAGE=git → SUCCESS (already installed)

2. Verify all builds complete successfully

3. Check Console Output shows:
   - "Package already installed" OR
   - "Nothing to do" OR
   - "Complete!" message

Repeated executions should not fail!

Step 13: Verify on Storage Server

# SSH to storage server from Jenkins or jump host
ssh natasha@ststor01
# Password: Bl@kW

# Check installed packages
rpm -qa | grep nginx
rpm -qa | grep git
rpm -qa | grep wget

# Verify package functionality
nginx -v
git --version
wget --version

# Exit
exit

Expected output:

nginx version: nginx/1.20.1
git version 2.18.4
GNU Wget 1.14 built on linux-gnu

Packages installed successfully!

Step 14: Complete Verification Script

# Create comprehensive verification script
cat > verify-jenkins-job.sh << 'EOF'
#!/bin/bash
echo "=== Jenkins Parameterized Job Verification ==="
echo ""
echo "1. Job Exists Check:"
if sudo ls /var/lib/jenkins/jobs/install-packages >/dev/null 2>&1; then
    echo "   ✓ Job 'install-packages' exists"
else
    echo "   ✗ Job 'install-packages' not found"
    exit 1
fi
echo ""
echo "2. Job Configuration:"
CONFIG="/var/lib/jenkins/jobs/install-packages/config.xml"
if sudo grep -q "ParametersDefinitionProperty" "$CONFIG"; then
    echo "   ✓ Job is parameterized"
fi
if sudo grep -q "<name>PACKAGE</name>" "$CONFIG"; then
    echo "   ✓ PACKAGE parameter configured"
fi
echo ""
echo "3. Build History:"
BUILD_COUNT=$(sudo ls -1d /var/lib/jenkins/jobs/install-packages/builds/[0-9]* 2>/dev/null | wc -l)
echo "   Total builds: $BUILD_COUNT"
if [ $BUILD_COUNT -gt 0 ]; then
    echo "   ✓ Job has been executed"
fi
echo ""
echo "4. Last Build Status:"
if [ -f /var/lib/jenkins/jobs/install-packages/builds/lastSuccessfulBuild ]; then
    LAST_BUILD=$(sudo readlink /var/lib/jenkins/jobs/install-packages/builds/lastSuccessfulBuild)
    echo "   Last successful build: #$LAST_BUILD"
    echo "   ✓ Job has successful builds"
fi
echo ""
echo "5. Job Type:"
if sudo grep -q "project" "$CONFIG"; then
    echo "   ✓ Freestyle project"
fi
echo ""
echo "6. Build Steps:"
if sudo grep -q "Execute shell" "$CONFIG" || sudo grep -q "hudson.tasks.Shell" "$CONFIG"; then
    echo "   ✓ Shell execution configured"
fi
echo ""
echo "7. Parameter Details:"
PARAM_NAME=$(sudo grep -A 1 "<name>PACKAGE</name>" "$CONFIG" | head -2)
echo "   Parameter configured: PACKAGE"
echo ""
echo "8. SSH Access to Storage Server:"
if ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no natasha@ststor01 "echo 'SSH working'" 2>/dev/null | grep -q "SSH working"; then
    echo "   ✓ SSH connection to storage server working"
else
    echo "   ⚠ SSH connection to storage server may need configuration"
fi
echo ""
echo "✓ VERIFICATION COMPLETE"
EOF

chmod +x verify-jenkins-job.sh
sudo ./verify-jenkins-job.sh

Expected output:

=== Jenkins Parameterized Job Verification ===

1. Job Exists Check:
   ✓ Job 'install-packages' exists

2. Job Configuration:
   ✓ Job is parameterized
   ✓ PACKAGE parameter configured

3. Build History:
   Total builds: 5
   ✓ Job has been executed

4. Last Build Status:
   Last successful build: #5
   ✓ Job has successful builds

5. Job Type:
   ✓ Freestyle project

6. Build Steps:
   ✓ Shell execution configured

7. Parameter Details:
   Parameter configured: PACKAGE

8. SSH Access to Storage Server:
   ✓ SSH connection to storage server working

✓ VERIFICATION COMPLETE

All verifications passed! 🎊

🔍 Understanding Parameterized Jobs

Parameter Types

1. String Parameter (Our choice):

Use: Single text value
Example: PACKAGE=nginx

Configuration:
Name: PACKAGE
Default: (optional)
Description: Package name to install

2. Choice Parameter:

Use: Select from predefined options
Example: ENVIRONMENT (dev, staging, prod)

Configuration:
Name: ENVIRONMENT
Choices:
  dev
  staging
  production

3. Boolean Parameter:

Use: True/false flag
Example: DEPLOY (true/false)

Configuration:
Name: DEPLOY
Default: false

4. File Parameter:

Use: Upload file to job
Example: CONFIG_FILE

Configuration:
Name: CONFIG_FILE

5. Password Parameter:

Use: Sensitive text (masked)
Example: API_KEY

Configuration:
Name: API_KEY (shown as ***)

6. Multi-line String:

Use: Large text blocks
Example: DEPLOY_NOTES

Configuration:
Name: DEPLOY_NOTES

Parameter Usage in Scripts

Accessing parameters:

#!/bin/bash

# String parameter
echo "Package: $PACKAGE"

# Using in commands
yum install -y $PACKAGE

# Conditional logic
if [ "$PACKAGE" == "nginx" ]; then
    echo "Installing web server"
fi

# Variable substitution
ssh user@server "sudo yum install -y $PACKAGE"

Environment variables:

Jenkins automatically creates environment variables:
- Parameter PACKAGE → $PACKAGE
- Parameter VERSION → $VERSION
- Parameter ENVIRONMENT → $ENVIRONMENT

Available in all build steps

Build Triggers with Parameters

Manual trigger:

User clicks "Build with Parameters"
→ Form appears
→ User enters values
→ Build executes

Scheduled trigger:

Configure cron schedule:
H 2 * * * (2 AM daily)

With parameters:
Use default values
OR
Use parameterized trigger plugin

Remote trigger:

Trigger via API:
curl -X POST http://jenkins/job/install-packages/buildWithParameters \
  --user admin:token \
  --data PACKAGE=nginx

Upstream trigger:

Job A completes → Triggers Job B with parameters

Configure:
"Trigger parameterized build on other projects"
Pass parameters from Job A to Job B

Job Configuration Best Practices

Parameter naming:

✓ UPPERCASE convention
✓ Descriptive names
✓ Consistent naming

Examples:
✓ PACKAGE
✓ VERSION
✓ ENVIRONMENT
✗ pkg (too short)
✗ package_name (lowercase)

Default values:

✓ Provide sensible defaults
✓ Make job easier to use
✓ Prevent empty parameter errors

Example:
PACKAGE = nginx (default)
ENVIRONMENT = dev (default)

Validation:

#!/bin/bash

# Validate parameter not empty
if [ -z "$PACKAGE" ]; then
    echo "ERROR: PACKAGE parameter is required"
    exit 1
fi

# Validate allowed values
ALLOWED="nginx httpd mysql git"
if ! echo "$ALLOWED" | grep -qw "$PACKAGE"; then
    echo "ERROR: Invalid package. Allowed: $ALLOWED"
    exit 1
fi

# Continue with installation
echo "Installing $PACKAGE..."

💡 Key Takeaways

Parameterized jobs enable reusability
String parameters accept runtime input
$PARAMETER syntax accesses values
Build with Parameters triggers execution
Default values improve usability
SSH execution enables remote automation
Repeatable builds ensure reliability
One job replaces many similar jobs

🎓 Quick Interview Questions

Q: What's the benefit of parameterized jobs over multiple jobs? A: Maintainability: One job instead of many duplicates. Flexibility: Same logic, different inputs. Scalability: Add new use cases without new jobs. DRY principle: Don't repeat yourself. Easier to update and test.

Q: How do you access parameter values in build scripts? A: Environment variables automatically created. String parameter PACKAGE becomes $PACKAGE in shell scripts. Access same way as any environment variable: echo $PACKAGE, use in commands: yum install $PACKAGE.

Q: Can parameters have default values? A: Yes. Set default value in parameter configuration. Used when: user doesn't provide value, job triggered automatically, API call omits parameter. Best practice: provide sensible defaults for optional parameters.

Q: What happens if required parameter is empty? A: Build proceeds with empty value, potentially causing errors. Solution: Validate in script (if [ -z "$PARAM" ]), use required parameter plugin, or provide default value. Always validate critical parameters.

Q: How do you trigger parameterized job from another job? A: Use "Trigger parameterized build on other projects" post-build action. Specify downstream job and parameter values. Can pass: predefined parameters, current build parameters, or parameters from files/properties.

Q: Can you use parameters in job name or description? A: In description: Yes, Jenkins will substitute values. In job name: No, job name is static. Alternative: Use display name plugin or include parameter in build description using Groovy postbuild plugin.

Q: What's difference between build parameter and environment variable? A: Build parameters: Defined in job configuration, user-provided at build time, customizable per build. Environment variables: Jenkins/system-defined, global or job-level, same for all builds unless overridden. Parameters become environment variables during build.

Q: How do you secure sensitive parameters? A: Use "Password Parameter" type (masked in logs/UI), store in Credentials plugin (better), use Credentials Binding plugin to inject securely, avoid logging sensitive values, use HashiCorp Vault integration for production.

Q: Can you modify parameter values during build? A: No, build parameters are immutable during build. Workaround: Create new variable in script (NEW_VAR=$PARAM-modified), use environment injector plugin to override, or set for downstream jobs.

Q: How do you validate parameter values before build starts? A: Options: (1) Use "Validating String Parameter" plugin for regex validation, (2) Use "Active Choices" plugin for dynamic validation, (3) Validate in first build step and exit if invalid, (4) Use Pipeline with input validation step.

Advanced Questions:

Q: How do parameterized builds affect build history and permalinks? A: Each parameter combination is separate build. Build history shows parameter values. Permalinks (lastSuccessfulBuild) point to most recent successful build regardless of parameters. Can't filter by parameter in basic Jenkins.

Q: What's the performance impact of many parameters? A: Minimal during build. UI impact: Many parameters make form long/complex. Solution: Use Active Choices plugin for conditional parameters, group related parameters, use Pipeline with organized input steps.

Q: How do you implement conditional parameters (show parameter B only if A selected)? A: Use "Active Choices" plugin: Reactive parameters show/hide based on other parameters. Groovy scripts define dependencies. Alternative: Use Pipeline with complex input validation.

Q: Can you pass objects or complex data structures as parameters? A: Not directly. Workarounds: (1) JSON string parameter, parse in script, (2) File parameter for complex configs, (3) Multiple simple parameters, (4) Use Pipeline with complex objects in Groovy.

Q: How do you implement parameter inheritance in multi-level job chains? A: Use "Parameterized Trigger" plugin: Pass current parameters to downstream, add/override specific parameters, copy artifacts with parameters. Pipeline: Pass parameters through stages explicitly.

🎉 Final Thoughts

You've successfully created a parameterized Jenkins job! This is automation efficiency.

What you accomplished: ✅ Created parameterized job
✅ Configured string parameter (PACKAGE)
✅ Implemented shell execution
✅ Automated remote package installation
✅ Tested multiple packages
✅ Verified repeatable execution
✅ Automated infrastructure tasks

Job configuration:

Job: install-packages
Type: Freestyle project
Parameter: PACKAGE (String)
Action: SSH to storage server → Install package
Target: ststor01 (natasha user)
Repeatable: ✓ Yes

Real-world impact:

  • One job replaces hundreds of package-specific jobs

  • Self-service for team members (no manual SSH)

  • Audit trail (who installed what, when)

  • Standardization (consistent installation process)

  • Scalability (add packages without new jobs)

Skills acquired:

  • Parameterized job creation

  • String parameter configuration

  • Shell script execution

  • SSH remote automation

  • Build testing

  • Job reusability patterns

Next steps:

  • Add more parameter types

  • Implement validation

  • Add email notifications

  • Create job templates

  • Build pipeline chains

This is automation excellence! 💪


Day: 71/100
Challenge: KodeKloud Cloud DevOps
Date: January 15, 2026
Topic: Jenkins Parameterized Jobs

What parameters do you use in your Jenkins jobs? Share your automation patterns! 📦