Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass build artifact into another job in Jenkins

Setup

I have two jobs in Jenkins:

  • build master - this builds the code and creates an artifact
  • deploy master - this deploys the artifact

Currently, deploy master has "Copy artifacts from another project" build step using "latest successful build".

build-master

My Goal

I want to change this step from "latest successful build" to "specified by a build parameter" so that I can select a specific build when deploying without modifying the configuration of deploy master job each time.

What I've tried

First, I changed to "specified by a build parameter".

build-specific

Then I checked the box next to "This project is parameterized" and added a string parameter for BUILD_SELECTOR.

parameter

Then I selected build and enter the input 47 which is a build number from the build master job.

Additionally I tried the api call

$.ajax({   type: 'POST',   url: 'https://jenkins/job/deploy%20master/build?token=abc7f5abc0c45abcea0646ed858abcde&BUILD_SELECTOR=47' }); 

Result

Both times it failed with the following output:

Started by user styfle [EnvInject] - Loading node environment variables. Building in workspace C:\Jenkins\jobs\deploy master\workspace ERROR: Unable to find a build for artifact copy from: build master Started calculate disk usage of build Finished Calculation of disk usage of build in 0 seconds Started calculate disk usage of workspace Finished Calculation of disk usage of workspace in 0 seconds Finished: FAILURE 

Question

How do I configure this properly so I can specify a build number (or some other identifier) when deploying?

Update with solution

My solution thanks to Gerold's answer was to add a "Build selector for Copy Artifact" parameter and use a new environment variable to link to my string parameter I already added.

enter image description here

like image 683
styfle Avatar asked May 27 '16 14:05

styfle


People also ask

How do I copy artifacts in Jenkins?

Next you need to install the Copy Artifact plugin in the Manage Plugins section of Jenkins. Go to "[PROJECT-NAME]-Output" > configure and add a new build step. Because you have installed the Copy Artifact plugin you should see an option called 'copy artifacts from another project' in the drop down menu.

How do you add build artifacts in Jenkins?

How to Create an Artifact. In Jenkins, an artifact is created in either Freestyle projects or Pipeline projects. In Freestyle, add the “Archive the artifacts” post-build step. In Pipeline, use the archiveArtifacts step.

Which plugin can be used to copy file from one job to another?

Job import plugin can be used to copy jobs from other jenkins instance.


1 Answers

There is just one workspace per project/job in Jenkins. The directories of builds contain just information about the builds and their results.

The root directories of both are specified in Manage JenkinsConfigure SystemAdvanced....

To deploy an artifact of a previous build you have to copy it to somewhere else in build master and access it there from deploy master later.

UPDATE:

See the inline help for Which buildParameter Name:

A parameter with this name should be added in the build parameters section above. There is a special parameter type for choosing the build selector.

Use this Build selector for Copy Artifact instead of a String Parameter.

like image 176
Gerold Broser Avatar answered Oct 13 '22 08:10

Gerold Broser