Search code examples
javamavenjenkinsgroovyjenkins-workflow

Calling a job in a Multi Branch pipeline


Background

I want to create a very simple Pipeline in Jenkins. For example:

  • Stage 1: Build using maven
  • Stage 2: Build docker image

In the root of my project I have the following Jenkinsfile:

stage 'build'
node {
    def branch = env.BRANCH_NAME
    build job: 'Maven job', parameters: [[$class: 'StringParameterValue', name: 'BRANCH_TO_BUILD', value: branch], [$class: 'StringParameterValue', name: 'MAJOR_VERSION_NUMBER', value: '1.0']]
}
stage 'Docker image'
node{
    echo 'TODO://Get jar file from previous job and build Docker image'
}

The Maven Job job will create a jar file which I want to use in the next stage to build my docker image.

The reason I used an external job to build my project is because I'm using JFrog Artifactory and I want all my dependencies to be resolved with Artfifactory.

The documentation for the Artifactory Jenkins plugin states:

The current plugin version does not include pipeline DSL for Maven and Gradle builds, but this will become available soon.

If I build the project straight from the Groovy script using Maven it will not resolve dependencies from Artficatory which is not what I want.

I have the Copy Artifact plugin installed and my Maven Job archives the jar file at the end of the build.

Question

From Jenkinsfile, how can I access the archived jar files from the job I just called?


Solution

  • You can use stash option:

    stash excludes: 'files_i_dont_want', includes: 'files_i_want', name: 'my_custom_name' unstash 'my_custom_name'

    Example: stash some files: https://github.com/jenkins-infra/jenkins.io/blob/master/Jenkinsfile#L62 unstash some files: https://github.com/lordofthejars/starwars/blob/master/Jenkinsfile#L44

    Another option is to use Copy Artifacts Plugin:

    step([$class: 'CopyArtifact', excludes: 'do_not_copy.jar', filter: 'copy_this.jar', fingerprintArtifacts: true, projectName: 'Maven Job', target: 'target_dir'])

    Did you already tried this?

    Hope this helps!