常用插件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 Plugins: Git plugin Git Parameter Pipeline Extended Choice Parameter Active Choice Parameter Publish Over SSH Maven Integration Pipeline Maven Integration SonarQube Scanner for Jenkins LDAP Plugin Role-based Authorization Authorize Project Image Tag Parameter Workspace Cleanup Plugin Kubernetes Kubernetes Continuous Deploy
插件加速设置 1 2 cd ~/jenkins_home/updates sed -i 's#https://updates.jenkins.io/download#https://mirrors.tuna.tsinghua.edu.cn/jenkins#g' default.json sed -i 's#http://www.google.com#https://www.baidu.com#g' default.json
pipeline语法 官网: https://www.jenkins.io/doc/book/pipeline/syntax/
1 2 3 4 5 6 7 8 9 10 11 12 13 agent post stages steps script environment options parameters triggers stage tools input when
pipeline语法结构 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 properties([ ... ]) pipeline { agent any tools { ... // 工具调用 , 引用全局环境变量中配置的工具 , 名称对应 } options { ... // 基础配置 , 如保留构建数量 , 天数 } environment { ... // 环境变量 , shell中通过$符引用 , 外部通过env引用 } parameters { ... // 参数化构建 } stages { stage('some stages1') { steps { '' ' } } stage(' some stages2') { steps { script { if ( ... ) { ... } } } } stage('some stages2') { when { anyOf { allOf { environment name: '...' , value: '...' environment name: '...' , value: '...' } allOf { environment name: '...' , value: '...' environment name: '...' , value: '...' } } } steps { script { def ... = '...' ... } } } } }
常用语法 https://www.jenkins.io/doc/book/pipeline/syntax/#input
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 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." } } } }
post
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 pipeline { agent any stages { stage('Example') { steps { echo 'Hello World' } } } post { always { echo 'I will always say Hello again!' } } }
条件参数详解:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 always post无论流水线或阶段运行的完成状态如何,都运行该部分中的步骤。 changed 仅post当当前管道或阶段的运行与之前的运行具有不同的完成状态时才运行步骤。 fixed 仅post当当前管道或阶段的运行成功并且前一次运行失败或不稳定时才运行中的步骤。 regression 仅post当当前管道或阶段的运行状态为失败、不稳定或中止并且上次运行成功时才运行中的步骤。 aborted 仅post当当前管道或阶段的运行具有“中止”状态时才运行步骤,这通常是由于管道被手动中止。这通常在 Web UI 中用灰色表示。 failure 仅post当当前管道或阶段的运行具有“失败”状态时才运行这些步骤,通常在 Web UI 中用红色表示。 success 仅post当当前流水线或阶段的运行具有“成功”状态(通常在 Web UI 中用蓝色或绿色表示)时才运行这些步骤。 unstable 仅post当当前流水线或阶段的运行处于“不稳定”状态时才运行这些步骤,这通常是由测试失败、代码违规等引起的。这通常在 Web UI 中用黄色表示。 unsuccessful 仅post当当前流水线或阶段的运行没有“成功”状态时才运行步骤。这通常在 Web UI 中表示,具体取决于前面提到的状态。 cleanup 在评估post所有其他条件后运行此条件中 的步骤post,无论管道或阶段的状态如何。
environment https://www.jenkins.io/doc/book/pipeline/syntax/#environment
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 pipeline { agent any environment { CC = 'clang' } stages { stage('Example') { environment { AN_ACCESS_KEY = credentials('my-predefined-secret-text') } steps { sh 'printenv' } } } }
https://www.jenkins.io/doc/book/pipeline/syntax/#tools
工具名称要与全局变量中定义的工具名称对应
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 pipeline { agent any tools { maven 'MAVEN3' jdk 'JDK1.8' } stages { stage('Example') { steps { sh 'java -version' sh 'mvn -version' } } } }
parameters https://www.jenkins.io/doc/book/pipeline/syntax/#parameters
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 pipeline { agent any parameters { string(name: 'PERSON' , defaultValue: 'Mr Jenkins' , description: 'Who should I say hello to?' ) text(name: 'BIOGRAPHY' , defaultValue: '' , description: 'Enter some information about the person' ) booleanParam(name: 'TOGGLE' , defaultValue: true , description: 'Toggle this value' ) choice(name: 'CHOICE' , choices: ['One' , 'Two' , 'Three' ], description: 'Pick something' ) password(name: 'PASSWORD' , defaultValue: 'SECRET' , description: 'Enter a password' ) } stages { stage('Example') { steps { echo "Hello ${params.PERSON}" echo "Biography: ${params.BIOGRAPHY}" echo "Toggle: ${params.TOGGLE}" echo "Choice: ${params.CHOICE}" echo "Password: ${params.PASSWORD}" } } } }
stages/steps https://www.jenkins.io/doc/book/pipeline/syntax/#stages
1 2 3 4 5 6 7 8 9 10 pipeline { agent any stages { stage('Example') { steps { echo 'Hello World' } } } }
script https://www.jenkins.io/doc/book/pipeline/syntax/#script
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 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" } } } } } }
when条件判断 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 pipeline { agent any stages { stage('Example Build') { steps { echo 'Hello World' } } stage('Example Deploy') { when { allOf { branch 'production' environment name: 'DEPLOY_TO' , value: 'production' } } steps { echo 'Deploying' } } } } pipeline { agent any stages { stage('Example Build') { steps { echo 'Hello World' } } stage('Example Deploy') { when { branch 'production' anyOf { environment name: 'DEPLOY_TO' , value: 'production' environment name: 'DEPLOY_TO' , value: 'staging' } } steps { echo 'Deploying' } } } }
if/else 1 2 3 4 5 6 7 8 9 node { stage('Example') { if (env.BRANCH_NAME == 'master') { echo 'I only execute on the master branch' } else { echo 'I execute elsewhere' } } }
由于jenkinsfile的特殊性, 普通的shell命令的状态返回值必须以文件的方式落地, 也就是说stdout得内容重定向到文件, 在jenkinsfile的sh语法中, 需要开启returnSstatus: true属性
格式效果如下
1 2 result = sh returnStdout: true , script: "<shell command>" result = sh(script: "<shell command>" , returnStdout: true )
try/catch 1 2 3 4 5 6 7 8 9 10 11 node { stage('Example') { try { sh 'exit 1' } catch (exc) { echo 'Something failed, I should sound the klaxons!' throw } } }
并行语法 Parallel 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 pipeline { agent any stages { stage('Non-Parallel Stage') { steps { echo 'This stage will be executed first.' } } stage('Parallel Stage') { when { branch 'master' } 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" } } stage('Branch C') { agent { label "for-branch-c" } stages { stage('Nested 1 ') { steps { echo "In stage Nested 1 within Branch C" } } stage(' Nested 2 ') { steps { echo "In stage Nested 2 within Branch C" } } } } } } } }
分布式 四种方式
依赖插件: SSH Build Agents
https://www.jenkins.io/doc/book/scaling/architecting-for-scale/