Jenkins复制工件解析复制的构建ID [英] Jenkins Copy Artifact parse copied build id

查看:17
本文介绍了Jenkins复制工件解析复制的构建ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Jenkins 2.73.1 中使用多分支管道 复制Artifact Plugin 要从其他两个管道获取最后成功的工件,请参阅我的 Jenkinsfile:

I'm using in Jenkins 2.73.1, in a multibranch pipeline Copy Artifact Plugin to get the last successful artifacts from two other pipelines, see my Jenkinsfile:

def branchname = "${BRANCH_NAME}".replace("/", "%2F")
pipeline {
  agent {
    label 'windows'
  }
  stages {
    stage('get artifacts') {
      steps {
        script {
          parallel('get-backend': {
            step([$class: 'CopyArtifact', projectName: "backend/${branchname}", target: 'input/backend'])
          },
          'get-frontend': {
            step([$class: 'CopyArtifact', projectName: "frontend/${branchname}", target: 'input/frontend'])
          })
        }
      }
    }
  }
}

在构建日志中我看到例如:

In the build log I see e.g.:

...
[Pipeline] script
[Pipeline] {
[Pipeline] parallel
[Pipeline] [get-backend] { (Branch: get-backend)
[Pipeline] [get-frontend] { (Branch: get-frontend)
[Pipeline] [get-backend] step
[Pipeline] [get-frontend] step
[get-frontend] Copied 344 artifacts from "frontend » foo/bar" build number 17
[Pipeline] [get-frontend] }
[get-backend] Copied 2'287 artifacts from "backend » foo/bar" build number 3
[Pipeline] [get-backend] }
[Pipeline] // parallel
[Pipeline] }
[Pipeline] // script
...

问题/目标:我想将前端和后端的内部版本号(在日志见内部版本号 17"和内部版本号 3").

Question / Goal: I'd like to parse and save to a file (to be archived, then loaded and parsed by groovy in a different pipeline) the build number of frontend and backend (in the log see "build number 17" and "build number 3").

在问题 Jenkins Pipeline - Reading previous stage log 我读到它是可以将 sh 脚本的标准输出重定向到 groovy 变量中,如下所示:

In the question Jenkins Pipeline - Reading previous stage log I read it's possible to redirect the standard output of an sh script into a groovy variable, like this:

def out = sh script: 'command', returnStdout: true

但是在我的管道中我需要使用 windows,在这种情况下,步骤也不是命令,而是一个步骤"(包含类CopyArtifact",对于 Jenkins 来说还是新的,我只是让它工作谷歌搜索并发现一些使用这种语法的例子).

But in my pipeline I need to use windows, also the step in this case is not a command but it's a "step" (containing class "CopyArtifact", still new to Jenkins, I just made it work googling around and found some examples using this syntax).

我怎样才能实现我的目标?在这种情况下我应该解析整个日志还是有更优雅的解决方案?请在您的答案中提供一些代码,以便我直接测试.

How can I achieve my goal? Should I parse the entire log in this case or is there a more elegant solution? Please provide some code in your answer, so I can test it directly.

推荐答案

您可以将内部版本号写入上游作业中的文件并将其包含在存档中.然后你向前复制那个工件.您可以使用管道实用程序步骤插件从文件中收集信息,但如果您保持文件简单(即只写内部版本号),那么类似这样的内容将为您提供内部版本号:

You could write the build number to a file in the upstream jobs and include that in the archive. Then you copy that artifact forward. You could use the pipeline utility steps plugin to gather information from the file, but if you keep the file simple (i.e. write nothing but the build number), then something like this would get you the build number:

def backendBuildId = sh script: "cat backendBuild.txt", returnStdout: true

或在 Windows 上:

or on Windows:

def backendBuildId = bat script: "type backendBuild.txt", returnStdout: true

这篇关于Jenkins复制工件解析复制的构建ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆