【问题标题】:How to use conditions in Jenkins multibranch declarative pipeline "options" block?如何在 Jenkins 多分支声明性管道“选项”块中使用条件?
【发布时间】:2022-08-05 21:51:31
【问题描述】:

我想配置 Office365 选项以基于分支运行,因为每个分支的 URL 不同

options {   office365ConnectorWebhooks([[notifyBackToNormal: true, notifyFailure: true, notifySuccess: true, notifyUnstable: true, url: \'\']]) }

标签: jenkins jenkins-pipeline jenkins-declarative-pipeline


【解决方案1】:

我建议在管道 {} 块之前创建简单变量并有条件地设置其值。

def webHookUrl

if ("${env.BRANCH_NAME}".startsWith("PR-")) {
  webHookUrl = "https://your-first-url"
} else {
  webHookUrl = "https://your-second-url"
}

pipeline {
  agent {
    label 'my_node'
  }
  options {
    office365ConnectorWebhooks([[startNotification: true,
                                 notifySuccess: true,
                                 notifyAborted: true,
                                 notifyNotBuilt: false,
                                 notifyUnstable: true,
                                 notifyFailure: true,
                                 notifyBackToNormal: true,
                                 notifyRepeatedFailure: true,
                                 url: "$webHookUrl",
                                 name: "Webhook Name"
                                ]])
  }

  stages {.....}

此外,如果您想获得完全控制,可以通过像这样的一些包装类来实现(声明式风格):

class WebhooksOptions {
  // default values
  Boolean startNotification = true
  Boolean notifySuccess = true
  Boolean notifyAborted = true
  Boolean notifyNotBuilt = true
  Boolean notifyUnstable = true
  Boolean notifyFailure = true
  Boolean notifyBackToNormal = true
  Boolean notifyRepeatedFailure = true
  String url
  String name = "Some name"

  WebhooksOptions(String url) {
    this.url = url
  }
}

def webHookOptions

if ("${env.BRANCH_NAME}".startsWith("PR-")) {
  webHookOptions = new WebhooksOptions("https://your-first-url")
} else {
  webHookOptions = new WebhooksOptions("https://your-second-url")
  // you can customize notification rules as well
  webHookOptions.startNotification = false
}

pipeline {
  agent {
    label 'my_node'
  }
  options {

    office365ConnectorWebhooks([[startNotification: webHookOptions.startNotification,
                                 notifySuccess: webHookOptions.notifySuccess,
                                 notifyAborted: webHookOptions.notifyAborted,
                                 notifyNotBuilt: webHookOptions.notifyNotBuilt,
                                 notifyUnstable: webHookOptions.notifyUnstable,
                                 notifyFailure: webHookOptions.notifyFailure,
                                 notifyBackToNormal: webHookOptions.notifyBackToNormal,
                                 notifyRepeatedFailure: webHookOptions.notifyRepeatedFailure,
                                 url: webHookOptions.url,
                                 name: webHookOptions.name
                                ]])
}
stages {.....}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-21
    • 1970-01-01
    • 1970-01-01
    • 2022-01-16
    • 1970-01-01
    • 2021-01-11
    • 1970-01-01
    相关资源
    最近更新 更多