小编典典

Jenkins Pipeline-在阶段之间传递工件URL

jenkins

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

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

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。

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

sh "deploy.sh > deploy.txt"

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

有什么更好的方法来使工件URL从发布进入管道的处理阶段?


阅读 708

收藏
2020-07-25

共1个答案

小编典典

publish阶段将值存储在变量中并在阶段读取该变量processing呢?考虑下面的詹金斯管道示例:

def artifactUrl = ''

pipeline {
    agent any

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

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

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

[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还有一个悬而未决的问题,但是如果您只能从publish命令中获取工件URL,那么在阶段之间传递它就不成问题。

2020-07-25