Jenkins Pipeline — a way to describe a CI/CD pipeline as code (Jenkinsfile).
Simple analogy: A Pipeline is like a recipe: step-by-step instructions with checks at each stage.
Declarative Pipeline (recommended):
1pipeline {2 agent any34 environment {5 APP_NAME = "myapp"6 DOCKER_REGISTRY = "ghcr.io"7 REGISTRY_CREDENTIALS = credentials('ghcr-token')8 }910 stages {11 stage('Checkout') {12 steps {13 checkout scm14 }15 }1617 stage('Test') {18 steps {19 sh 'npm ci'20 sh 'npm test'21 }22 }2324 stage('Build') {25 steps {26 sh "docker build -t ${DOCKER_REGISTRY}/${APP_NAME} ."27 }28 }2930 stage('Deploy to Staging') {31 when {32 branch 'develop'33 }34 steps {35 sh 'kubectl apply -f k8s/staging/'36 }37 }3839 stage('Deploy to Production') {40 when {41 branch 'main'42 }43 input {44 message "Deploy to production?"45 ok "Yes, deploy!"46 }47 steps {48 sh 'kubectl apply -f k8s/production/'49 }50 }51 }5253 post {54 failure {55 slackSend channel: '#alerts', message: "Build FAILED: ${env.JOB_NAME}"56 }57 success {58 slackSend channel: '#alerts', message: "Build SUCCESS: ${env.JOB_NAME}"59 }60 }61}
Scripted Pipeline (more flexible, but more complex):
1node {2 stage('Checkout') {3 checkout scm4 }5 stage('Build') {6 sh 'npm ci && npm run build'7 }8}
Key concepts: