1. Variabel Lingkungan Bawaan (Built-in Env Vars)
Jenkins menyediakan variabel sistem bawaan yang dapat langsung diakses di dalam pipeline:
${env.BUILD_NUMBER}: Nomor urut build saat ini (misal: 42).${env.BRANCH_NAME}: Nama cabang Git yang sedang diproses.${env.JOB_NAME}: Nama job Jenkins.${env.GIT_COMMIT}: Hash SHA commit Git yang memicu build.
2. Parameterized Pipeline & Credentials Binding
Jenkinsfile Advanced Example
pipeline {
agent any
parameters {
choice(name: 'DEPLOY_ENV', choices: ['staging', 'production'], description: 'Pilih Lingkungan Target Deployment')
booleanParam(name: 'RUN_INTEGRATION_TESTS', defaultValue: true, description: 'Jalankan Integration Test?')
}
environment {
DOCKER_REGISTRY = 'registry.hub.docker.com/myuser'
DB_SECRET = credentials('postgres-production-cred')
}
stages {
stage('Build & Deploy') {
steps {
echo "Deploying branch ${BRANCH_NAME} build #${BUILD_NUMBER} to ${params.DEPLOY_ENV}..."
withCredentials([usernamePassword(credentialsId: 'docker-hub-auth', usernameVariable: 'USER', passwordVariable: 'PASS')]) {
sh "docker login -u ${USER} -p ${PASS}"
}
}
}
}
}