28

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

styfle
  • 22,361
  • 27
  • 86
  • 128

2 Answers2

7

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.

Gerold Broser
  • 14,080
  • 5
  • 48
  • 107
  • The `build master` job has a "Post-build Action" of "Archive the artifacts". And I have been able to specify a build by number by editing the config. So I know this part works. – styfle May 27 '16 at 15:08
  • @styfle The _Archive the artifacts_ _post-build action_ stores the specified files in `//archive/...` accessible via `http:///job///artifact/...`. – Gerold Broser May 27 '16 at 15:24
  • That url works. Should I try using the full URL as a parameter in the deployment? – styfle May 27 '16 at 15:31
  • @styfle If the artifact name doesn't change the `build no.` should be sufficient. It's the only variable in the URL. – Gerold Broser May 27 '16 at 15:42
  • I tried the build number 47 as a parameter and it doesn't work (see my original post). Yet, visiting the URL https://jenkins/job/build%20master/47/artifact/ does work. – styfle May 27 '16 at 15:50
  • @styfle See the inline help for _Which build_ → _Parameter Name_: "_There is a special parameter type for choosing the build selector._". Use this instead of a _String Parameter_. – Gerold Broser May 27 '16 at 16:32
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/113170/discussion-between-styfle-and-gerold-broser). – styfle May 27 '16 at 16:55
  • is there a way to copy the archived artifact in the deploy job? I am using the *Copy artifacts from another project* but that copies the unarchived artifact from the build workspace. – Radu Apr 03 '18 at 12:29
  • 1
    @Radu See _File/Folder Copy_ of the [File Operations Plugin](https://wiki.jenkins.io/display/JENKINS/File+Operations+Plugin). – Gerold Broser Apr 03 '18 at 14:11
  • how can I get the buildNumber from BUILD_SELECTOR? – Radu Apr 16 '18 at 16:03
6

Add following in downstream project. "Build selector for Copy Artifact" instead of a "String Parameter"

"Build selector for Copy Artifact" instead of a "String Parameter"!!

Copy artifacts from another project

enter image description here

Thats it. Click "Build with Parameters" and pass build number enter image description here

Febin
  • 161
  • 2
  • 3