Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins pipeline share information between jobs

We are trying to define a set of jobs on Jenkins that will do really specific actions. JobA1 will build maven project, while JobA2 will build .NET code, JobB will upload it to Artifactory, JobC will download it from Artifactory and JobD will deploy it. Every job will have a set of parameters so we can reuse the same job for any product (around 100).

The idea behind this is to create black boxes, I call a job with some input and I get always some output, whatever happens between is something that I don't care. On the other side, this allows us to improve each job separately, adding the required complexity, and instantly all products will get benefit.

We want to use Jenkins Pipeline to orchestrate the execution of actions. We are going to have a pipeline per environment/usage.

  • PipelineA will call JobA1, then JobB to upload to artifactory.
  • PipelineB will download package JobC and then deploy to staging.
  • PipelineC will download package JobC and then deploy to production based on some internal validations.

I have tried to get some variables from JobA1 (POM basic stuff such as ArtifactID or Version) injected to JobB but the information seems not to be transfered. Same happens while downloading files, I call JobC but the file is in the job workspace not available for any other and I'm afraid that"External Workspace Manager" plugin adds too much complexity.

Is there any way rather than share the workspace to achieve my purpose? I understand that share the workspace will make it impossible to run two pipelines at the same time Am I following the right path or am I doing something weird?

like image 871
sickfear Avatar asked Mar 10 '23 12:03

sickfear


2 Answers

There are two ways to share info between jobs:

  1. You can use stash/unstash to share the files/data between multiple jobs in a single pipeline.

    stage ('HostJob') {
        build 'HostJob'
        dir('/var/lib/jenkins/jobs/Hostjob/workspace/') {
            sh 'pwd'
            stash includes: '**/build/fiblib-test', name: 'app' 
        }
    }
    
    stage ('TargetJob') {
        dir("/var/lib/jenkins/jobs/TargetJob/workspace/") {
        unstash 'app'
        build 'Targetjob'
    }
    

    In this manner, you can always copy the file/exe/data from one job to the other. This feature in pipeline plugin is better than Artifact as it saves only the data locally. The artifact is deleted after a build (helps in data management).

  2. You can also use Copy Artifact Plugin.

    There are two things to consider for copying an artifact:

    a) Archive the artifacts in the host project and assign permissions.

    b) After building a new job, select the 'Permission to copy artifact' → Projects to allow copy artifacts: *

    c) Create a Post-build Action → Archive the artifacts → Files to archive: "select your files"

    d) Copy the artifacts required from host to target project. Create a Build action → Copy artifacts from another project → Enter the ' $Project name - Host project', which build 'e.g. Lastest successful build', Artifacts to copy '$host project folder', Target directory '$localfolder location'.

like image 75
lakshmi kumar Avatar answered Mar 15 '23 13:03

lakshmi kumar


The first part of your question(to pass variables between jobs) please use the below command as a post build section:

post {
    always {
        build job:'/Folder/JobB',parameters: [string(name: 'BRANCH', value: "${params.BRANCH}")], propagate: false
    }
}

The above post build action is for all build results. Similarly, the post build action could be triggered on the current build status. I have used the BRANCH parameter from current build(JobA) as a parameter to be consumed by 'JobB' (provide the exact location of the job). Please note that there should be a similar parameter defined in JobB.

Moreover, for sharing the workspace you can refer this link and share the workspace between the jobs.

like image 34
Karthick Avatar answered Mar 15 '23 13:03

Karthick