Jenkins流水线是持续集成和持续交付(CI/CD)的核心功能,它允许您将整个构建、测试和部署过程以代码的形式进行定义。本文将全面解析Jenkins流水线语法,帮助您掌握这一强大工具。
Jenkins支持两种离散的流水线语法:声明式流水线和脚本化流水线。声明式流水线提供了更简单、更有主见的语法,而脚本化流水线基于Groovy构建,提供了更大的灵活性和表达能力。
所有有效的声明式流水线必须包含在pipeline块中:
pipeline {
/* 声明式流水线内容 */
}agent部分指定流水线执行的位置:
pipeline {
agent any // 在任何可用代理上执行
// 或
agent none // 不指定全局代理
// 或
agent {
label 'my-defined-label' // 在指定标签的代理上执行
}
// 或
agent {
docker {
image 'maven:3-alpine'
label 'my-defined-label'
args '-v /tmp:/tmp'
}
}
}stages部分包含一个或多个stage指令,每个stage包含steps部分:
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building...'
sh 'mvn -B clean verify'
}
}
stage('Test') {
steps {
echo 'Testing...'
}
}
}
}post部分定义根据流水线或阶段完成情况运行的步骤:
pipeline {
agent any
stages {
stage('Example') {
steps {
echo 'Hello World'
}
}
}
post {
always {
echo 'I will always say Hello again!'
}
success {
echo 'Pipeline succeeded!'
}
failure {
echo 'Pipeline failed!'
}
}
}environment指令定义环境变量:
pipeline {
agent any
environment {
CC = 'clang'
AN_ACCESS_KEY = credentials('my-predefined-secret-text')
}
stages {
stage('Example') {
steps {
sh 'printenv'
}
}
}
}options指令配置流水线特定选项:
pipeline {
agent any
options {
timeout(time: 1, unit: 'HOURS')
retry(3)
timestamps()
}
stages {
stage('Example') {
steps {
echo 'Hello World'
}
}
}
}parameters指令定义用户提供的参数:
pipeline {
agent any
parameters {
string(name: 'PERSON', defaultValue: 'Mr Jenkins', description: 'Who should I say hello to?')
booleanParam(name: 'DEBUG_BUILD', defaultValue: true, description: 'Enable debug mode?')
}
stages {
stage('Example') {
steps {
echo "Hello ${params.PERSON}"
}
}
}
}triggers指令定义流水线自动化触发方式:
pipeline {
agent any
triggers {
cron('H */4 * * 1-5')
// 或
pollSCM('H */4 * * 1-5')
// 或
upstream(upstreamProjects: 'job1,job2', threshold: hudson.model.Result.SUCCESS)
}
stages {
stage('Example') {
steps {
echo 'Hello World'
}
}
}
}tools指令自动安装工具并添加到PATH:
pipeline {
agent any
tools {
maven 'apache-maven-3.0.1'
jdk 'jdk8'
}
stages {
stage('Example') {
steps {
sh 'mvn --version'
}
}
}
}input指令在阶段中暂停执行等待用户输入:
pipeline {
agent any
stages {
stage('Example') {
input {
message "Should we continue?"
ok "Yes, we should."
submitter "alice,bob"
parameters {
string(name: 'PERSON', defaultValue: 'Mr Jenkins', description: 'Who should I say hello to?')
}
}
steps {
echo "Hello, ${PERSON}, nice to meet you."
}
}
}
}when指令根据条件决定是否执行阶段:
pipeline {
agent any
stages {
stage('Example Build') {
steps {
echo 'Hello World'
}
}
stage('Example Deploy') {
when {
branch 'production'
environment name: 'DEPLOY_TO', value: 'production'
anyOf {
environment name: 'ENVIRONMENT', value: 'production'
environment name: 'ENVIRONMENT', value: 'staging'
}
}
steps {
echo 'Deploying'
}
}
}
}parallel指令允许阶段并行执行:
pipeline {
agent any
stages {
stage('Non-Parallel Stage') {
steps {
echo 'This stage will be executed first.'
}
}
stage('Parallel Stage') {
failFast true
parallel {
stage('Branch A') {
agent {
label "for-branch-a"
}
steps {
echo "On Branch A"
}
}
stage('Branch B') {
agent {
label "for-branch-b"
}
steps {
echo "On Branch B"
}
}
}
}
}
}script步骤在声明式流水线中执行脚本化代码:
pipeline {
agent any
stages {
stage('Example') {
steps {
echo 'Hello World'
script {
def browsers = ['chrome', 'firefox']
for (int i = 0; i < browsers.size(); ++i) {
echo "Testing the ${browsers[i]} browser"
}
}
}
}
}
}脚本化流水线基于Groovy构建,提供了更大的灵活性:
node {
stage('Example') {
if (env.BRANCH_NAME == 'master') {
echo 'I only execute on the master branch'
} else {
echo 'I execute elsewhere'
}
}
}脚本化流水线使用Groovy的流控制结构:
node {
stage('Example') {
try {
sh 'exit 1'
}
catch (exc) {
echo 'Something failed, I should sound the klaxons!'
throw
}
}
}特性 | 声明式流水线 | 脚本化流水线 |
|---|---|---|
语法 | 结构化、预定义 | 基于Groovy、灵活 |
学习曲线 | 平缓 | 较陡峭 |
灵活性 | 有限 | 极高 |
错误检查 | 早期 | 运行时 |
适用场景 | 简单到中等复杂度 | 高度复杂需求 |
Jenkins流水线提供了强大而灵活的方式来定义CI/CD流程。声明式流水线适合大多数场景,提供了简单直观的语法;而脚本化流水线则为有复杂需求的用户提供了极大的灵活性。掌握这两种语法,将帮助您构建高效、可靠的持续交付流水线。
无论您选择哪种语法,记住保持流水线代码简洁、可读和可维护是至关重要的。随着项目的发展,定期审查和优化流水线代码,确保它们能够高效地支持您的开发流程。