Jenkins Pipeline-在阶段之间传递工件URL [英] Jenkins Pipeline - Pass Artifact URL between stages

查看:626
本文介绍了Jenkins Pipeline-在阶段之间传递工件URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的用例是构建一个Java作业,将tar.gz.zip部署到nexus,并将该工件URL传递到进一步的阶段.

My usecase is to build a Java job , deploy a tar.gz.zip to nexus and pass on this artifact URL to further stages.

我在执行其他验证的脚本中使用以下命令:

I am using the below command in a script that does other validation:

mvn -B clean deploy -U package deploy:deploy -DskipNexusStagingDeployMojo=true -DaltDeploymentRepository=dev-snapshots::default::https://<myrepo.com>

下面是我的阶段:

stage('publish'){    
  //push to Nexus  
 }
stage('processing'){    
 // get the artifact url from above step
}

我不想使用"archiveartifacts",因为我已经存储了工件,所以我不想对其进行存档.我只需要URL.

I don't want to use "archiveartifacts" as I am already storing the artifacts, I do not want to archive it. I just need the URL.

到目前为止,我所能做的就是将部署的全部输出都输出到文本文件中.

So far , all I could manage was to get the entire output of the deploy to a text file.

sh "deploy.sh > deploy.txt"

但是,新文件(deploy.txt)不适用于管道,因为在主服务器上执行了Groovy,而在从属服务器上创建了文件.而且readfile('deploy.txt')不需要任何正则表达式.因此,我无法解析文件以获取工件URL.

However ,new File(deploy.txt) does not work with Pipelines as groovy gets executed on master and file is created on slaves. And readfile('deploy.txt') does not take any regex. So I am not able to parse the file to get the artifact url.

是否有更好的方法将工件url从发布发布到管道的处理阶段?

Is there any better way to get the artifact url from publish to processing stage in pipeline?

推荐答案

publish阶段将值存储在变量中并在processing阶段读取该变量怎么办?考虑以下Jenkins管道示例:

What about storing value in a variable during publish stage and reading this variable during processing stage? Consider following Jenkins pipeline example:

def artifactUrl = ''

pipeline {
    agent any

    stages {
        stage('Build') { 
            steps {
                script {
                   artifactUrl = 'test'
                }
            }
        }

        stage('Publish') {
            steps {
                echo "Current artifactUrl is '${artifactUrl}'"
            }
        }
    }
}

当我运行它时,我得到以下输出:

When I run it I get following output:

[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/test-pipeline
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Build)
[Pipeline] script
[Pipeline] {
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Publish)
[Pipeline] echo
Current artifactUrl is 'test'
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

当然还有一个悬而未决的问题,如何获取工件URL,但是如果只能从发布命令中获取工件URL,那么在阶段之间传递它应该不是问题.

Of course there is an open question how you get your artifact URL, but if you only can get it from your publish command then passing it between stages shouldn't be a problem.

这篇关于Jenkins Pipeline-在阶段之间传递工件URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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