Jenkins 流水线语法 04 agent/post

网友投稿 274 2022-09-13

Jenkins 流水线语法 04 agent/post

agent 构建节点

参数:

any: 运行在任一可用节点。none:当pipeline全局指定agent为none,则根据每个stage中定义的agent运行(stage必须指定)。label:在指定的标签的节点运行。(标签=分组)node:支持自定义流水线的工作目录。

## 一pipeline { agent any}## 二pipeline { agent { label "label Name" }}## 三 自定义节点,加上node有一些高级的特性,比如自定义工作目录pipeline { agent { node { label "labelName", customWorkspace "/opt/agent/workspace" } }}Running on build-01 in /data/cicd/jenkinsagent/workspace/devops-maven-service

pipeline { agent {label "build"} stages { stage('Hello') { steps { script{ echo "The branch name is ${branchName}" #这里变量是参数构建 sh "echo the job name is ${JOB_NAME}" } } } }}Started by user adminRunning in Durability level: MAX_SURVIVABILITY[Pipeline] Start of Pipeline[Pipeline] nodeRunning on build-01 in /data/cicd/jenkinsagent/workspace/pipeline-test[Pipeline] {[Pipeline] stage (hide)[Pipeline] { (Hello)[Pipeline] script[Pipeline] {[Pipeline] echoThe branch name is master[Pipeline] sh+ echo the job name is pipeline-testthe job name is pipeline-test[Pipeline] }[Pipeline] // script[Pipeline] }[Pipeline] // stage[Pipeline] }[Pipeline] // node[Pipeline] End of PipelineFinished: SUCCESS

post 构建后操作

定义: 根据流水线的最终状态匹配后做一些操作。

状态:

always: 不管什么状态总是执行success: 仅流水线成功后执行failure: 仅流水线失败后执行aborted: 仅流水线被取消后执行unstable:不稳定状态,单侧失败等等

post就是构建后的操作

post就是根据流水线的状态去做一些事情,always是不管怎么样总是要做的一些事情

注意post可以写到stage里面,那么就是针对于某个阶段了,如果写在pipeline里面那么就是全局的,是针对整个流水线的状态,里面script不一定要写,但是运行了脚本方式的语法最好带上。

什么时候在stage里面写post?比如构建maven项目在单元测试之后,会产生一个报告,也就是单元测试成功之后可以将这个报告展示在jenkins上面,失败了就可以不让她展示了

在post always里面可以清理项目的空间,比如删除workspace的空间,在always里面的sript里面加入shell清理worksapce空间

pipeline { agent any stages { stage('Hello') { steps { script{ echo "The branch name is ${branchName}" sh "echo the job name is ${JOB_NAME}" } } post{ always{ script{ echo "stage always.........." } } } } }post { always{ script{ println("流水线结束后,经常做的事情") echo "The job status is ${currentBuild.currentResult}" } } success{ script{ println("流水线成功后,要做的事情") } } failure{ script{ println("流水线失败后,要做的事情") } } aborted{ script{ println("流水线取消后,要做的事情") } } }}Started by user adminRunning in Durability level: MAX_SURVIVABILITY[Pipeline] Start of Pipeline[Pipeline] nodeRunning on Jenkins in /var/jenkins_home/workspace/pipeline-test[Pipeline] {[Pipeline] stage[Pipeline] { (Hello)[Pipeline] script[Pipeline] {[Pipeline] echoThe branch name is master[Pipeline] sh+ echo the job name is pipeline-testthe job name is pipeline-test[Pipeline] }[Pipeline] // scriptPost stage[Pipeline] script[Pipeline] {[Pipeline] echostage always..........[Pipeline] }[Pipeline] // script[Pipeline] }[Pipeline] // stage[Pipeline] stage[Pipeline] { (Declarative: Post Actions)[Pipeline] script[Pipeline] {[Pipeline] echo流水线结束后,经常做的事情[Pipeline] echoThe job status is SUCCESS[Pipeline] }[Pipeline] // script[Pipeline] script[Pipeline] {[Pipeline] echo流水线成功后,要做的事情[Pipeline] }[Pipeline] // script[Pipeline] }[Pipeline] // stage[Pipeline] }[Pipeline] // node[Pipeline] End of PipelineFinished: SUCCESS

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Kubernetes API Server 准入控制插件 / 资源限制 ResourceQuota
下一篇:全媒派:一个月能写3万字:自媒体时代的医生,不只是会看病!
相关文章

 发表评论

暂时没有评论,来抢沙发吧~