Jenkins Credentials: Secrets, SSH Keys, Tokens, Scopes, Folders and Using Them Safely in Jobs

CI CD Pipelines with Jenkins 30 min min read Updated: Nov 17, 2026 Intermediate
Jenkins Credentials: Secrets, SSH Keys, Tokens, Scopes, Folders and Using Them Safely in Jobs
Intermediate Topic 2 of 3

Never Put Secrets in the Job or Jenkinsfile

Pipelines need passwords for Git, registries, cloud accounts and servers. If you write them into the Jenkinsfile, anyone who can read the repository can see them, and they stay in the Git history forever. Jenkins keeps secrets in an encrypted credentials store and hands them to a build only when needed.

Where to Manage Credentials

*Manage Jenkins, Credentials, System, Global credentials, Add Credentials*. Each credential has a Kind, a Scope, an ID and a description.

KindHoldsTypical use
Username with passwordTwo valuesDocker registry, Nexus, Git over HTTPS
Secret textOne stringAPI tokens, Slack webhook, cloud keys
SSH Username with private keyA user and a private keyGit over SSH, deploying to servers, SSH agents
Secret fileAn uploaded filekubeconfig, service account JSON, .env file
CertificatePKCS#12 keystoreCode signing, client certificates

The ID is the name you use in pipelines (for example docker-hub). Choose clear IDs and never reuse an ID for a different purpose.

Scopes and Where They Live

Scope or storeWho can use it
GlobalAny job on the controller, and agents (the usual choice)
SystemOnly Jenkins itself, such as agent connections or email. Pipelines cannot use these
Folder credentialsOnly jobs inside that folder. Best way to separate teams
User credentialsOnly for that user's own use
Use folders for isolation: Put each team's jobs in a folder and keep their credentials in that folder store. Then one team's pipeline cannot use another team's production password.

Use Credentials in a Pipeline

groovy pipeline { agent any stages { stage('Login and push') { steps { withCredentials([usernamePassword(credentialsId: 'docker-hub', usernameVariable: 'DH_USER', passwordVariable: 'DH_PASS')]) { sh ''' echo "$DH_PASS" | docker login -u "$DH_USER" --password-stdin docker push myorg/app:${BUILD_NUMBER} ''' } } } stage('Call API') { steps { withCredentials([string(credentialsId: 'api-token', variable: 'TOKEN')]) { sh 'curl -fsS -H "Authorization: Bearer $TOKEN" https://api.example.com/deploy' } } } stage('Kubeconfig') { steps { withCredentials([file(credentialsId: 'kubeconfig-prod', variable: 'KUBECONFIG')]) { sh 'kubectl get nodes' } } } stage('SSH deploy') { steps { sshagent(credentials: ['deploy-ssh-key']) { sh 'ssh -o StrictHostKeyChecking=accept-new deploy@app1 "sudo systemctl restart app"' } } } } }
BindingUse
usernamePassword(...)Two variables for user and password
string(...)Secret text into one variable
file(...)Writes the secret file to a temp path and sets a variable with that path
sshUserPrivateKey(...)Key file path plus username variable
sshagent([...])Loads the key into an SSH agent (SSH Agent plugin)

Using Credentials in the environment Block

groovy pipeline { agent any environment { DOCKER_CREDS = credentials('docker-hub') // creates DOCKER_CREDS, DOCKER_CREDS_USR and DOCKER_CREDS_PSW API_TOKEN = credentials('api-token') } stages { stage('Login') { steps { sh 'echo "$DOCKER_CREDS_PSW" | docker login -u "$DOCKER_CREDS_USR" --password-stdin' } } } }

Masking and Leak Prevention

  • Jenkins masks the value of bound secrets in the console output, replacing them with ****
  • Masking is not magic: it only matches the exact text. Base64, URL-encoded or split values can slip through
  • Single quotes in Groovy (sh 'echo $TOKEN') let the shell read the variable. Double quotes (sh "echo $TOKEN") let Groovy insert the secret into the command text, which can leak it in logs or process lists. Jenkins warns about this
  • Do not use set -x or echo on secrets, and never put them in command-line arguments when a safer option exists (--password-stdin)
  • Never write secrets to artifacts or workspace files that are archived
groovy // BAD: Groovy interpolation puts the secret into the command line text sh "curl -u admin:${PASS} https://example.com" // GOOD: the shell reads the environment variable sh 'curl -u "admin:$PASS" https://example.com'

Git Checkout With Credentials

groovy checkout([$class: 'GitSCM', branches: [[name: '*/main']], userRemoteConfigs: [[url: 'git@github.com:myorg/app.git', credentialsId: 'github-ssh']] ]) // or the short form git url: 'https://github.com/myorg/app.git', branch: 'main', credentialsId: 'github-token'

For GitHub use a fine-grained personal access token or a deploy key with only the access the job needs, not a personal account password.

Rotate and Audit

  1. Create the new secret value in the target system (for example a new token)
  2. Edit the Jenkins credential and update the value (the ID stays the same, so pipelines need no change)
  3. Run a test build
  4. Revoke the old value
  5. Check *Manage Jenkins, Credentials* for unused or old items and remove them
  • Use the Audit Trail plugin or logs to see who changed credentials
  • Prefer short-lived credentials from a vault or cloud identity (OIDC) over long-lived passwords
  • Limit who has the Credentials, Create/Update/ManageDomains permissions

Practice Tasks

  1. Create secret text and username-password credentials and print their masked output with withCredentials
  2. Create a folder with its own credential and show that a job outside the folder cannot use it
  3. Use credentials() in an environment block and check _USR and _PSW variables
  4. Demonstrate the single versus double quote warning
  5. Rotate a token without changing any pipeline

Conclusion

Store secrets as Jenkins credentials with clear IDs and the narrowest scope, and bind them with withCredentials or credentials(). Use folder credentials to separate teams, single quotes to avoid Groovy interpolation of secrets, and rotate and audit regularly.

Quick Summary: kinds; scopes (global/system/folder); withCredentials bindings; environment credentials(); masking limits; single vs double quotes; git creds; rotation.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators