Jenkins: Credentials & Agents
Use Jenkins credentials without leaking secrets. Control execution using labels/agents and archive artifacts between stages.
This tutorial extends Jenkins pipeline knowledge with two enterprise essentials:
- credentials (secure secret management)
- agents/nodes (running the right stage in the right place)
1) Jenkins credentials: avoid hardcoding
Jenkins stores secrets in its credentials system.
You reference them by credentialsId.
Common credential types:
- Secret text (token)
- Username/password
- SSH keys
- Files (e.g., kubeconfig)
Example: secret text token
withCredentials([
string(credentialsId: 'API_TOKEN', variable: 'API_TOKEN')
]) {
sh '''
echo "Calling external API"
curl -sS -H "Authorization: Bearer $API_TOKEN" https://api.example.com
'''
}
Rules of thumb:
- do not print the token
- avoid
set -x - keep logs clean
2) Agents: choose where stages run
Declarative: label-based
pipeline {
agent none
stages {
stage('Build') {
agent { label 'linux-x64' }
steps {
sh 'npm ci'
sh 'npm run build'
}
}
stage('Test') {
agent { label 'linux-x64' }
steps {
sh 'npm test'
}
}
}
}
Declarative: Docker agent
pipeline {
agent {
docker {
image 'node:20-alpine'
args '-u node'
}
}
stages {
stage('Build') {
steps {
sh 'npm ci'
sh 'npm run build'
}
}
}
}
3) Artifacts: keep outputs for later
Archive artifacts so you can download them from the build page.
stage('Build') {
steps {
sh 'npm ci'
sh 'npm run build'
archiveArtifacts artifacts: 'dist/**', fingerprint: true
}
}
4) Practical pipeline example (theory + code)
A typical CI flow:
- checkout
- install deps
- build
- test
- archive artifacts
pipeline {
agent any
stages {
stage('Checkout') {
steps { checkout scm }
}
stage('Install') {
steps { sh 'npm ci' }
}
stage('Build') {
steps { sh 'npm run build' }
}
stage('Test') {
steps { sh 'npm test' }
}
stage('Archive') {
steps { archiveArtifacts artifacts: 'dist/**', fingerprint: true }
}
}
}
Next steps
Next tutorials:
- Monitoring & Observability fundamentals
- Terraform for reproducible environments
- Ansible for idempotent configuration
Frequently Asked Questions
Why are my secrets empty in Jenkins?
Often because the credential ID is wrong, or you’re using the wrong credentials type (string/file/username/password). Also check that the job has access to the credential.
When should I use agents vs running everything on one node?
Use agents to run stages on the correct environment (OS/tools) and to scale parallel builds. Use one node when simplicity matters and resources are constrained.