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.
| Kind | Holds | Typical use |
|---|---|---|
| Username with password | Two values | Docker registry, Nexus, Git over HTTPS |
| Secret text | One string | API tokens, Slack webhook, cloud keys |
| SSH Username with private key | A user and a private key | Git over SSH, deploying to servers, SSH agents |
| Secret file | An uploaded file | kubeconfig, service account JSON, .env file |
| Certificate | PKCS#12 keystore | Code 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 store | Who can use it |
|---|---|
| Global | Any job on the controller, and agents (the usual choice) |
| System | Only Jenkins itself, such as agent connections or email. Pipelines cannot use these |
| Folder credentials | Only jobs inside that folder. Best way to separate teams |
| User credentials | Only for that user's own use |
Use Credentials in a Pipeline
| Binding | Use |
|---|---|
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
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 -xorechoon 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
Git Checkout With Credentials
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
- Create the new secret value in the target system (for example a new token)
- Edit the Jenkins credential and update the value (the ID stays the same, so pipelines need no change)
- Run a test build
- Revoke the old value
- 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
- Create secret text and username-password credentials and print their masked output with
withCredentials - Create a folder with its own credential and show that a job outside the folder cannot use it
- Use
credentials()in anenvironmentblock and check_USRand_PSWvariables - Demonstrate the single versus double quote warning
- 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.

