Jenkins Pipeline — Declarative Syntax Guide
Advertisement
Jenkins Pipeline — Declarative Syntax Guide
Jenkins declarative pipelines provide structured CI/CD automation.
Introduction
Jenkins pipelines define build processes as code, enabling version control and reproducibility.
Basic Pipeline
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building...'
sh 'npm install'
}
}
stage('Test') {
steps {
echo 'Testing...'
sh 'npm test'
}
}
stage('Deploy') {
steps {
echo 'Deploying...'
sh 'npm run deploy'
}
}
}
}
Agents and Options
pipeline {
agent {
docker {
image 'node:18'
}
}
options {
timeout(time: 1, unit: 'HOURS')
buildDiscarder(logRotator(numToKeepStr: '10'))
}
stages {
stage('Build') {
steps {
sh 'npm install && npm run build'
}
}
}
}
Post Actions
pipeline {
agent any
stages {
stage('Test') {
steps {
sh 'npm test'
}
}
}
post {
always {
junit 'test-results.xml'
}
failure {
emailext subject: 'Build failed', body: 'Build failed!', to: 'team@example.com'
}
}
}
FAQ
Q: What's the difference between declarative and scripted pipelines? A: Declarative is structured and simpler; scripted is more flexible using Groovy.
Q: Can I store pipelines in Git? A: Yes, use Jenkinsfile in your repository root for pipeline-as-code.
Advertisement