0

I already use Jenkins API for some tasks in my build pipeline. Now, there is a task that I want to persist some simple dynamic data say like "50.24" for each build. Then be able to retrieve this data back in a different job.

More concretely, I am looking for something on these lines

POST to http://localhost:8080/job/myjob//api/json/store {"code-coverage":"50.24"}

Then in a different job

GET http://localhost:8080/job/myjob//api/json?code-coverage

One idea is to do archiveArtifacts and save it into a file and then read it back using the API/file. But I am wondering if there is plugin or a simple way to write some data for this job.

satyajit
  • 1,470
  • 3
  • 22
  • 43

1 Answers1

1
  • If you need to send a variable from one build to another:

The parametrized build is the easiest way to do this: https://wiki.jenkins.io/display/JENKINS/Parameterized+Build

the URL would look like:

http://server/job/myjob/buildWithParameters?PARAMETER=Value

If you need to share complex data, you can save some files in your workspace and use it (send the absolute path) from another build.

  • If you need to re-use a simple variable computed during your build

I would go for using an environment var, updated during your flow:

Jenkinsfile (Declarative Pipeline)

pipeline {
    agent any

    environment {
        DISABLE_AUTH = 'true'
        DB_ENGINE    = 'sqlite'
    }

    stages {
        stage('Build') {
            steps {
                sh 'printenv'
            }
        }
    }
}

All the details there: https://jenkins.io/doc/pipeline/tour/environment/

  • If you need to re-use complex data between two builds

You have two case there, it's if your build are within the same workspace or not. In the same workspace, it's totally fine to write your data in a text file, that is re-used later, by another job. archiveArtifacts plugin is convenient if your usecase is about extracting test results from logs, and re-use it later. Otherwise you will have to write the process yourself.

If your second job is using another workspace, you will need to provide the absolute path to your child-job. In order for it to copy and process it.

Luc
  • 1,393
  • 1
  • 6
  • 14
  • I will not know this data ahead of time. this is generated dynamically during the build. – satyajit Apr 28 '18 at 17:26
  • I havee updated my response. if it is a trivial data that you need, I would go for env variable. Otherwise I would save it in a txt file and re-use it later. – Luc Apr 29 '18 at 10:31