本文介绍了Jenkins Copy Artifact解析复制的内部版本ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Jenkins 2.73.1中的多分支管道中使用复制Artifact插件要从其他两个管道中获得最后成功的工件,请参阅我的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
...

问题/目标::我想解析并保存到文件(要存档,然后由groovy加载并解析到另一个管道中)的前端和后端的内部版本号(以日志请参阅内部版本号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-阅读上一阶段日志我读到的是可以将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 Copy Artifact解析复制的内部版本ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 19:19