getting variables from a downstream job

30 views
Skip to first unread message

Alexander Drobst

unread,
Mar 6, 2017, 6:01:34 PM3/6/17
to Jenkins Users

We have a build and deploy process for every artifact(it is not a maven build, and not a maven arifact). If build or deploy for a single artifact fails it should not stop the whole process.


We have a separate jobs for build and deploy. Build jobs can work in parallel on a slaves, deploy not.


The results of a build job should be used by deploy job. I need somehow to get a workspace location of a build and set it as a parameter of a deploy job, that deploy job can use ear files from build job and deploy them on a server.


The place where I need to put a workspace is marked with a question mark.


def branches = [:]
def artifactsToDeploy = []
node{
    workspace = pwd()
    echo "Workspace:${workspace}"

    //read artifact names from file
    def appFile=readFile(workspace+"@script/artifacts.txt")
    def artifactNames = appFile.tokenize()

    //prepare parallel jobs
    for (int i=0 ; i < artifactNames.size ; i++) {
        def artifactName=artifactNames[i]
        branches[artifactName]={

            //start build job
            def buildResult = build job: 'build-artifact', parameters: [[$class: 'StringParameterValue', name: 'ARTIFACT', value:artifactName],
            [$class: 'StringParameterValue', name: 'SVN_TAG', value:SVN_TAG]]

            //need to read workspace from a build job, that was running on a slave
            artifactsToDeploy[artifactsToDeploy.size]=[artifact:artifactName,workspace:?????]
        }
    }

    echo 'pipeline begin'
    stage('build'){
        parallel branches
    }

    stage('deploy'){

        //read artifacts from a list and deploy 
        for (int i=0;i<artifactsToDeploy.size;i++) {
            def buildResult = build job: 'deploy-artifact', parameters: [[$class: 'StringParameterValue', name: 'ARTIFACT', value:artifactsToDeploy[i].artifact],
            [$class: 'StringParameterValue', name: 'WORKSPACE', value:artifactsToDeploy[i].workspace]]
        }
    }
    echo 'pipeline end'
}

Thorsten Meinl

unread,
Mar 7, 2017, 11:36:10 AM3/7/17
to Jenkins Users
Write files with the desired parameters in the "build" job, archive the files, and unarchive them in the downstream job. Then you can read the parameters stored in the file. Here's a sketch how we do it:

In the upstream job:
def downstreamParams = [:]
downstreamParams
.foo = 'bar'
downstreamParams
.bar = 'foo'
writeFile file
: 'my.params', text: downstreamParams.inspect()
archiveArtifacts artifacts
: "*.params"


And in the downstream job:
step([$class: 'CopyArtifact', filter: '*.params', projectName: 'upstream-project',
      selector
: [$class: 'TriggeredBuildSelector', fallbackToLastSuccessful: true], target: ''])

def upstreamParams = Eval.me(readFile(file: 'my.params'))



Reply all
Reply to author
Forward
0 new messages