Learn › Jenkins

Jenkins Parameters and Credentials: A Practical Deep Dive

Make Jenkins pipelines reusable with string, choice, boolean and password parameters, and handle secrets safely with withCredentials and masking.

Intermediate 11 min read
By Gopinath Karthikeyan

A pipeline that only works for one server isn’t automation – it’s a fancy shortcut. Parameters make one pipeline serve many situations; credentials let it log in without ever exposing a password. This guide builds on Jenkins Part 2.

Parameter types

pipeline {
    agent any
    parameters {
        string(name: 'TARGET', defaultValue: 'lab', description: 'Server name or inventory group')
        choice(name: 'ACTION', choices: ['report', 'restart', 'cleanup'], description: 'What to do')
        booleanParam(name: 'DRY_RUN', defaultValue: true, description: 'Preview only')
        text(name: 'SERVER_LIST', defaultValue: '', description: 'One server per line (optional)')
    }
    stages {
        stage('Show inputs') {
            steps {
                echo "Target: ${params.TARGET}, action: ${params.ACTION}, dry run: ${params.DRY_RUN}"
            }
        }
    }
}

Tips:

  • Make the safe option the default (DRY_RUN = true, ACTION = report).
  • Use choice instead of free text whenever the valid values are known – it prevents typos.
  • Remember the first run after adding parameters only registers them; the Build with Parameters button appears afterwards.

Validate before doing anything

stage('Validate') {
    steps {
        script {
            if (!(params.TARGET ==~ /^[A-Za-z0-9._-]+$/)) {
                error("TARGET contains invalid characters")
            }
            if (params.ACTION != 'report' && params.TARGET == 'all') {
                error("Changes against 'all' are not allowed – choose a group")
            }
        }
    }
}

error() stops the build with a clear message. Checking input also protects against command injection when parameters end up in shell commands.

Credential types

Add credentials under Manage Jenkins → Credentials. The common kinds:

Kind Use for In the pipeline
Username with password Service accounts, share access usernamePassword(...)
Secret text API tokens string(...)
Secret file kubeconfig, certificates file(...)
SSH username with private key Linux / OpenSSH logins, Ansible sshUserPrivateKey(...)

Using credentials safely

stage('Restart on target') {
    when { expression { !params.DRY_RUN } }
    steps {
        withCredentials([usernamePassword(credentialsId: 'svc-automation',
                                          usernameVariable: 'SVC_USER',
                                          passwordVariable: 'SVC_PASS')]) {
            pwsh '''
                $sec  = ConvertTo-SecureString $env:SVC_PASS -AsPlainText -Force
                $cred = [pscredential]::new($env:SVC_USER, $sec)
                Invoke-Command -ComputerName $env:TARGET -Credential $cred -ScriptBlock { Restart-Service Spooler }
            '''
        }
    }
}

The rules:

  • Secrets exist only inside withCredentials { }.
  • Use single-quoted ''' shell blocks and read secrets as environment variables ($env:SVC_PASS, $SVC_PASS). Double-quoted Groovy strings ("${SVC_PASS}") put the secret into the command text, which Jenkins warns about.
  • Jenkins masks known secret values in the log with ****, but don’t rely on it – never echo a secret.
  • Parameters are available as environment variables too ($env:TARGET), which keeps them out of Groovy string building.

(pwsh needs the PowerShell plugin; on a Linux agent with PowerShell 7 you can also use sh 'pwsh -File script.ps1'.)

Password parameters?

Jenkins has a password parameter type, but the value is typed by whoever starts the build. Prefer stored credentials for service accounts. Use a password parameter only for things that genuinely change each run, and never print it.

Scope and folders

Credentials can be stored in a folder instead of globally, so only that team’s jobs can use them. Combine folders with role-based permissions to keep production credentials away from lab jobs.

FAQ

Can I see a stored password in Jenkins later? Not through the UI – that’s the point. Admins with script console access could extract it, so restrict that access tightly.

What’s next

Next: Jenkins agents, including Windows agents.

Practise this topic

Test yourself with free questions on this topic – instant answers and explanations.

Start practice questions

JenkinsCredentialsParametersSecurityIntermediate

Gopinath Karthikeyan

Gopinath Karthikeyan – Windows Site Reliability Engineer with about 12 years in enterprise Windows Server infrastructure, and founder of WinOps Academy. YouTube · LinkedIn

More in Jenkins