问题
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>
Below would be my stages:
stage('publish'){
//push to Nexus
}
stage('processing'){
// get the artifact url from above step
}
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"
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.
Is there any better way to get the artifact url from publish to processing stage in pipeline?
回答1:
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
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.
回答2:
Based on @Szymon Stepniak comment implemented it the below way:
def out = sh(script: "/myscript.sh" , returnStdout: true )
Pattern p = Pattern.compile("INFO] Uploaded: .*bin.tar.gz")
Matcher m = p.matcher(out);
来源:https://stackoverflow.com/questions/48754463/jenkins-pipeline-pass-artifact-url-between-stages