Why Pipeline as Code?
A Jenkinsfile in Git is reviewable, versioned, and reproducible — unlike UI-only freestyle jobs.
Minimal Jenkinsfile
pipeline {
agent any
environment {
APP_NAME = 'my-app'
NODE_VERSION = '20'
}
stages {
stage('Checkout') {
steps {
checkout scm
}
}
stage('Install') {
steps {
sh 'npm ci'
}
}
stage('Test') {
steps {
sh 'npm test -- --ci'
}
}
stage('Build') {
steps {
sh 'npm run build'
archiveArtifacts artifacts: 'dist/**', fingerprint: true
}
}
}
post {
success { echo 'Pipeline succeeded!' }
failure {
echo "FAILED: ${env.JOB_NAME} #${env.BUILD_NUMBER}"
}
always {
cleanWs()
}
}
}Create the job
- New Item → Pipeline
- Pipeline → Definition: Pipeline script from SCM
- Point at your repo; Script Path:
Jenkinsfile
Or paste the script directly for a quick lab.
Use the Pipeline Syntax helper in Jenkins to generate snippets for checkout, credentials, and more.