Declarative jenkins pipeline, retrieve artifact

十年热恋 提交于 2020-01-14 06:08:09

问题


I'm currently trying to trigger an existing job and take in the generated artifacts in the current job's workspace.

The following works fine:

pipeline {
    agent {
        label 'builder-rpm'
    }

    options {
        timestamps()
        ansiColor('xterm')
    }

    stages {
        stage('Build Other pipeline') {
            steps {
                build job: 'Components/components-rpm-build', parameters: [
                    string(name: 'Component', value: 'foo'),
                ]
                copyArtifacts(
                    filter: 'results/*.rpm',
                    fingerprintArtifacts: true,
                    flatten: true,
                    projectName: 'Components/components-rpm-build',
                    selector: lastSuccessful(),
                    target: "${env.WORKSPACE}/dist"
                )
            }
        }
    }
}

The problem of it is that lastSuccessful() will take indeed the last successful build, meaning that if some other user manages to run a parallel build faster than me, I'm going to take in their artifacts instead of mine.

According to this page there should be a way to use a specific job, with specific:

def built = build('downstream');
copyArtifacts(projectName: 'downstream',
              selector: specific("${downstream.number}"));

There's however no explanation nor example on how to use it in a declarative pipeline.

Any hint?

Thanks in advance for any help.


回答1:


I found some decent solution: switching to script simply does the job.

pipeline {
    agent {
        label 'builder-rpm'
    }

    options {
        timestamps()
        ansiColor('xterm')
    }

    stages {
        stage('Build Other pipeline') {
            steps {
                script {
                    def built = build(
                        job:'Components/components-rpm-build',
                        parameters:[
                            string(name:'Component', value:'ew-filesystem'),
                            string(name:'RepoServer', value:'')
                        ]
                    )
                    copyArtifacts(
                        projectName: 'Components/components-rpm-build',
                        selector: specific("${built.number}"),
                        target: "${env.WORKSPACE}/dist",
                        filter: "results/*"
                    )
                }
            }
        }
    }
}


来源:https://stackoverflow.com/questions/49030754/declarative-jenkins-pipeline-retrieve-artifact

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!