Jenkins流水线语法终极指南:从入门到精通
个人名片
🎓作者简介:java领域优质创作者
🌐个人主页:码农阿豪
📞工作室:新空间代码工作室(提供各种软件服务)
💌个人邮箱:[2435024119@qq.com]
📱个人微信:15279484656
🌐个人导航网站:www.forff.top
💡座右铭:总有人要赢。为什么不能是我呢?
- 专栏导航:
码农阿豪系列专栏导航
面试专栏:收集了java相关高频面试题,面试实战总结🍻🎉🖥️
Spring5系列专栏:整理了Spring5重要知识点与实战演练,有案例可直接使用🚀🔧💻
Redis专栏:Redis从零到一学习分享,经验总结,案例实战💐📝💡
全栈系列专栏:海纳百川有容乃大,可能你想要的东西里面都有🤸🌱🚀
目录
Jenkins流水线语法终极指南:从入门到精通
Jenkins流水线是持续集成和持续交付(CI/CD)的核心功能,它允许您将整个构建、测试和部署过程以代码的形式进行定义。本文将全面解析Jenkins流水线语法,帮助您掌握这一强大工具。
一、流水线语法概述
Jenkins支持两种离散的流水线语法:声明式流水线和脚本化流水线。声明式流水线提供了更简单、更有主见的语法,而脚本化流水线基于Groovy构建,提供了更大的灵活性和表达能力。
二、声明式流水线
1. 基本结构
所有有效的声明式流水线必须包含在pipeline块中:
pipeline {
/* 声明式流水线内容 */
}
2. 代理 (agent)
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'
}
}
}
3. 阶段 (stages) 和步骤 (steps)
stages部分包含一个或多个stage指令,每个stage包含steps部分:
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building...'
sh 'mvn -B clean verify'
}
}
stage('Test') {
steps {
echo 'Testing...'
}
}
}
}
4. 后处理 (post)
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!'
}
}
}
5. 环境变量 (environment)
environment指令定义环境变量:
pipeline {
agent any
environment {
CC = 'clang'
AN_ACCESS_KEY = credentials('my-predefined-secret-text')
}
stages {
stage('Example') {
steps {
sh 'printenv'
}
}
}
}
6. 选项 (options)
options指令配置流水线特定选项:
pipeline {
agent any
options {
timeout(time: 1, unit: 'HOURS')
retry(3)
timestamps()
}
stages {
stage('Example') {
steps {
echo 'Hello World'
}
}
}
}
7. 参数 (parameters)
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}"
}
}
}
}
8. 触发器 (triggers)
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'
}
}
}
}
9. 工具 (tools)
tools指令自动安装工具并添加到PATH:
pipeline {
agent any
tools {
maven 'apache-maven-3.0.1'
jdk 'jdk8'
}
stages {
stage('Example') {
steps {
sh 'mvn --version'
}
}
}
}
10. 输入 (input)
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."
}
}
}
}
11. 条件执行 (when)
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'
}
}
}
}
12. 并行执行 (parallel)
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"
}
}
}
}
}
}
13. 脚本 (script)
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、灵活 |
| 学习曲线 | 平缓 | 较陡峭 |
| 灵活性 | 有限 | 极高 |
| 错误检查 | 早期 | 运行时 |
| 适用场景 | 简单到中等复杂度 | 高度复杂需求 |
五、最佳实践
- 优先使用声明式流水线:对于大多数用例,声明式流水线提供了更简单、更易维护的语法
- 使用共享库:将复杂逻辑提取到共享库中,保持Jenkinsfile简洁
- 适当使用脚本步骤:在声明式流水线中,使用script步骤处理复杂逻辑
- 利用并行执行:合理使用并行阶段提高流水线执行效率
- 实现健壮的错误处理:使用post部分和try/catch块处理失败情况
六、总结
Jenkins流水线提供了强大而灵活的方式来定义CI/CD流程。声明式流水线适合大多数场景,提供了简单直观的语法;而脚本化流水线则为有复杂需求的用户提供了极大的灵活性。掌握这两种语法,将帮助您构建高效、可靠的持续交付流水线。
无论您选择哪种语法,记住保持流水线代码简洁、可读和可维护是至关重要的。随着项目的发展,定期审查和优化流水线代码,确保它们能够高效地支持您的开发流程。


浙公网安备 33010602011771号