2017-04-24 80 views
0

如何在我的Jenkinsfile中指定類似以下內容的東西?如何在jenkinsfile中指定分支NOT(分支名稱)?

當分支不是X

我知道如何指定像分支的具體任務:

stage('Master Branch Tasks') { 
     when { 
      branch "master" 
     } 
     steps { 
      sh '''#!/bin/bash -l 
      Do some stuff here 
      ''' 
     } 
} 

但是我想指定分支時沒有掌握或分期像一個舞臺以下:

stage('Example') { 
    if (env.BRANCH_NAME != 'master' && env.BRANCH_NAME != 'staging') { 
     echo 'This is not master or staging' 
    } else { 
     echo 'things and stuff' 
    } 
} 

但是上面並沒有與下面的錯誤工作和失敗:

WorkflowScript: 62: Not a valid stage section definition: "if 

WorkflowScript: 62: Nothing to execute within stage "Example" 

爲我的失敗嘗試注來源:https://jenkins.io/doc/book/pipeline/syntax/#flow-control

回答

2

從您的文章的鏈接顯示了腳本語法管道的例子。您的代碼使用聲明式管道語法。要在聲明中使用腳本管道,可以使用腳本步驟。

stage('Example') { 
    steps { 
     script { 
      if (env.BRANCH_NAME != 'master' && env.BRANCH_NAME != 'staging') { 
       echo 'This is not master or staging' 
      } else { 
       echo 'things and stuff' 
      } 
     } 
    } 
} 
3

有了這個issue解決了,你現在可以這樣做:

stage('Example (Not master)') { 
    when { 
     not { 
      branch 'master' 
     } 
    } 
    steps { 
    sh 'do-non-master.sh' 
    } 
}