您的第一个尝试是使用声明性管道,第二个可以使用的是脚本化管道。您需要将步骤括在步骤声明中,并且不能
if用作声明式的顶层步骤,因此需要将其包装在
script步骤中。这是一个有效的声明性版本:
pipeline { agent any stages { stage('test') { steps { sh 'echo hello' } } stage('test1') { steps { sh 'echo $TEST' } } stage('test3') { steps { script { if (env.BRANCH_NAME == 'master') { echo 'I only execute on the master branch' } else { echo 'I execute elsewhere' } } } } }}您可以简化此过程,并可以通过使用“
when”来避免if语句(只要您不需要else)。请参阅https://jenkins.io/doc/book/pipeline/syntax/上的
“指令时” 。您还可以使用jenkins rest api验证jenkinsfiles。超级甜 在詹金斯中使用声明性管道玩得开心!



