Jenkins流水线语法


pipeline {
agent { docker 'maven:3.3.3' } #代理,
stages { #阶段。
stage('build') { #每一个阶段
steps { #每一个步骤
sh 'mvn --version'
}
}
stage('test') {
steps {
sh 'echo success'
}
}
}
}
#每一个流水线工程最终都在
/var/jenkins_home/workspace/流水线工程名

#agent { docker 'maven:3.3.3' }
整个流水线工程
docker run -t -d -u 0:0 -w /var/jenkins_home/workspace/helloworld --volumes-from cd270036b30eada48bb123338864c1451ee8c7d30997a3b856c6d0174b1be2e2 -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** maven:3.3.3 cat
整个流水线的文件夹是在maven容器内运行的

 

#1、多步骤。steps{ sh ``` 多命令 ``` }
#2、agent:jenkins接下来的流水线运行在哪个环境
https://www.jenkins.io/doc/book/pipeline/syntax/#agent
agent any:任意环境(立刻就能运行,不挑环境),
agent none:顶级整个流水线环境,每个阶段stage,要定义自己的agent环境
agent { label 'my-defined-label' }
agent { node { label 'labelName' } } ;和agent { label 'labelName' }一个意思,
agent {
docker {
image 'maven:3-alpine'
label 'my-defined-label'
args '-v /tmp:/tmp'
}
} #指定当前流水线或者stage运行在使用docker下载来的这个环境下,用完就删了。如果这些环境有些数据需要永久保存我们就应该挂载出来。

#3、环境变量,jenkins整个流水线过程中,我可以把经常要用的一些值,抽取为环境变量,在下面方便引用。
environment {
DISABLE_AUTH = 'true'
DB_ENGINE = 'sqlite'
}

#4、后置处理
post {
always {
junit 'build/reports/**/*.xml'
}
aborted: {
sh 'echo 666'
}
}
always:完成

changed:阶段发生变化执行

fixed:

regression
Only run the steps in post if the current Pipeline’s or stage’s run’s status is failure, unstable, or aborted and the previous run was successful.

aborted:手工停止

failure
Only run the steps in post if the current Pipeline’s or stage’s run has a "failed" status, typically denoted by red in the web UI.

success
Only run the steps in post if the current Pipeline’s or stage’s run has a "success" status, typically denoted by blue or green in the web UI.

unstable
Only run the steps in post if the current Pipeline’s or stage’s run has an "unstable" status, usually caused by test failures, code violations, etc. This is typically denoted by yellow in the web UI.

unsuccessful
Only run the steps in post if the current Pipeline’s or stage’s run has not a "success" status. This is typically denoted in the web UI depending on the status previously mentioned.

cleanup
Run the steps in this post condition after every other post condition has been evaluated, regardless of the Pipeline or stage’s status.

示例:构建失败的时候邮件通知
post {
failure {
mail to: 'team@example.com',
subject: "Failed Pipeline: ${currentBuild.fullDisplayName}",
body: "Something is wrong with ${env.BUILD_URL}"
}
}
#5、人工确认
input "Does the staging environment look ok?"
#6、总结:Pipeline语法
pipeline {
agent any
stages {
stage('第一阶段') {
//
steps {
sh 'echo 111'
emailext body: '哈哈', subject: '测试', to: '22222@qq.com'
}
}
}
}
#所有不会写的语法,全部参照 流水线工程里面的 语法生成器。
片段生成器--》可以生产step里面不会写的内容
Declarative Directive Generator--》生成哪些指令不会用
post {
always {
// One or more steps need to be included within each condition's block.
}
aborted {
// One or more steps need to be included within each condition's block.
}
success {
// One or more steps need to be included within each condition's block.
}
failure {
// One or more steps need to be included within each condition's block.
}
}

 

agent {
docker {
image 'maven:3-alpine' #docker下载来的maven作为接下来的环境,容器用完就没了
args '-v /root/.m2:/root/.m2' #mvn从网上下载jar包。下载来的东西都挂载到linux的/root/.m2
}
}
#默认使用docker的maven环境下载很慢。如何下载快的问题
#1、改配置文件。进入linux的/root/.m2。修改settings-docker.xml
#2、指定maven的配置。
2.1:先给项目里面放maven-setting.xml文件,配置好mirror,profile等。
2.2:jenkins流水线,mvn -gs maven-setting.xml

 

参考语法生成器


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM