简体   繁体   English

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

[英]How do I trigger a scan for a jenkins multibranch pipeline?

I have a jenkins job ( A ) that creates and pushes a new git branch with the pattern releases/major.minor .我有一个 jenkins 作业( A ),它创建并推送一个新的 git 分支,其模式为releases/major.minor I have jenkins multibranch pipeline ( B ) that builds for all branches named releases/* .我有 jenkins 多分支管道( B ),它为所有名为releases/*的分支构建。 Immediately after A completes, I want to trigger B on the newly-created branch, but jenkins won't run B/major.minor until there's a new scan.A完成后,我想立即在新创建的分支上触发B ,但是 jenkins 不会运行B/major.minor ,直到有新的扫描。

How do I trigger a scan?如何触发扫描?

You can scan multibranch projects with the build step .您可以使用build步骤扫描多分支项目。 However, you must include wait: false or else you'll get the following error:但是,您必须包含wait: false否则您将收到以下错误:

ERROR: Waiting for non-job items is not supported

Unfortunately, that means if you want to run the multibranch pipeline on the associated branch, you need to manually check for the existence of the job.不幸的是,这意味着如果您想在关联的分支上运行多分支管道,您需要手动检查作业是否存在。

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"
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM