git 如何触发对jenkins多分支管道的扫描?

9avjhtql  于 2022-11-20  发布在  Git
关注(0)|答案(1)|浏览(164)

我有一个jenkins作业(A),它创建并推送一个新的git分支,模式为releases/major.minor。我有一个jenkins多分支管道(B),它为所有名为releases/*的分支构建。在A完成后,我想立即在新创建的分支上触发B,但jenkins不会运行B/major.minor,直到有一个新的扫描。
如何触发扫描?

rkue9o1l

rkue9o1l1#

您可以使用build step扫描多分支项目。

build(
  job: '/my-multibranch-project-name',
  wait: false,
)

您必须包含wait: false,否则将出现以下错误:

ERROR: Waiting for non-job items is not supported

不幸的是,这意味着如果要在关联的分支上运行多分支管道,则需要手动检查该作业是否存在。

def ensureMultibranchJobExists(Map args) {
  def branch = args['branch']?.replaceAll('/', '%252F')
  def rootJob = args['rootJob']

  if (branch == null) {
    throw new NullPointerException('branch is required')
  }
  if (rootJob == null) {
    throw new NullPointerException('rootJob is required')
  }

  // env.JENKINS_URL ends with a slash.
  env.ENSURE_MULTIBRANCH_JOB_EXISTS_URL = "${env.JENKINS_URL}job/$rootJob/job/$branch/"
  print "Ensuring multibranch job exists: ${env.ENSURE_MULTIBRANCH_JOB_EXISTS_URL}"

  def lastHttpStatusCode = null
  for (int i=0; i < 12; i++) {
    lastHttpStatusCode = sh(
      returnStdout: true,
      script: '''
#!/bin/bash
set -euo pipefail

curl \
  --output /dev/null \
  --silent \
  --user devtools:<MY_TOKEN> \
  --write-out '%{http_code}' \
  "${ENSURE_MULTIBRANCH_JOB_EXISTS_URL}" \
;
      '''.trim(),
    )
    if (lastHttpStatusCode == '200') {
      break
    } else {
      print "Last status code: $lastHttpStatusCode"
    }

    sleep(
      time: 10,
      unit: 'SECONDS',
    )
  }

  if (lastHttpStatusCode != '200') {
    error "${env.ENSURE_MULTIBRANCH_JOB_EXISTS_URL} failed. Expected 200 status code, but received: $lastHttpStatusCode"
  }
}

相关问题