Copy artifact within a jenkins pipeline - jenkins

I have a Jenkins pipeline job that archives an Artifact in its first phase, I then need to copy that Artifact in another stage of the pipeline build
node {
stage 'Stage 1 of build'
// Run tests, if successful archive the artifact
archiveArtifacts artifacts: 'build/test.js', excludes: null
stage 'Stage 2 of build'
// want to copy artifact from stage 1 of the build
step([$class: 'CopyArtifact', filter: 'build/test.js', fingerprintArtifacts: true, flatten: true, projectName: 'echo-develop-js-pipeline', selector: [$class: 'WorkspaceSelector'], target: './client/public/vendor/echo/'])
}
With this I get a unable to find a build for artifact copy
When the artifact is created it is saved here:
http://localhost:8181/view/Echo JS Develop/job/echo-develop-js-pipeline/233/artifact/build/test.js
How do I access the created artifact from within a pipeline job?

I needed this recently, and none of the other solutions here did exactly what I wanted, because I need to use multiple parameter filters for my selection. Here's what I did using the "Run Selector Plugin" in addition to the direct calling of the "Copy Artifact Plugin":
Step One: Select the build number you need.
prereq_build = selectRun filter: parameters("TARGET_OS=${TARGET_OS},GIT_BRANCH_NAME=${GIT_BRANCH_NAME}"), job: 'prereq_rpms', selector: status('STABLE'), verbose: true
Step Two: Copy (updated 2017-11: Native pipeline support now!).
copyArtifacts(
projectName: 'prereq_rpms',
filter: '**/*.rpm',
fingerprintArtifacts: true,
target: 'prereq',
flatten: true,
selector: specific(prereq_build.getId())
)

Figured this one out, so using the var ${BUILD_NUMBER} you can access artifacts un the current pipeline
step([$class: 'CopyArtifact', filter: 'build/test.js', fingerprintArtifacts: true, flatten: true, projectName: 'echo-develop-js-pipeline', selector: [$class: 'SpecificBuildSelector', buildNumber: '${BUILD_NUMBER}'], target: './client/public/vendor/echo/'])

In pipeline plugin, there is a new feature called 'stash', 'unstash' instead of artifacts.
Artifact: Archives are designed for longer term file storage (e.g., intermediate binaries from your builds). Artifact requires more storage space and resource management.
Stash: Saves a set of files and use later in the same build, generally on another node/workspace. stash and unstash steps are designed for use with small files. Stash/unstash can be used inside a pipeline with just assigning a name to the stash & works only locally.
Here is a good example for stash/unstash: Tutorial

Related

Use workspace location in the post build script in jenkins

I am trying to use the artifacts created in the workspace post jenkins build in a postbuild shell script.
I am not able to use them as workspace artifacts are automatically getting deleted before it comes postbuild script.
Could anyone help me to address this?
When the post-build stage is running, your workspace is already removed. When you think of it, your regular stage and post-build stage may even be running on different nodes, so there can't be any expectation that the files are in your workspace.
To access your artifacts in the post-build stage, you need to fetch them manually, e.g. by using Copy Artifact plugin:
post {
always {
// fetch artifacts of this job and this number to $WORKSPACE
step([
$class: 'CopyArtifact',
filter: '*',
fingerprintArtifacts: true,
optional: true,
projectName: "${JOB_NAME}",
selector: [$class: 'SpecificBuildSelector',
buildNumber: "${BUILD_NUMBER}"]
])
script {
try {
for(file in findFiles(glob: "*")) {
println "Found file ${file}"
}
} catch(error) {
println "Failed to find files"
}
}
}
}

Jenkinsfile copyartifact cannot find artifact when using filter criteria with wildcard

I have 2 jobs with 2 different jenkinsfile.
A job can generate artifacts correctly with:
post {
always {
mstest testResultsFile:"testresults*.trx", keepLongStdio: true
archiveArtifacts artifacts: 'from/path/Deploy/**/*.*', fingerprint: true
I use these artifacts in another job, copying with:
steps {
step([$class: 'CopyArtifact', fingerprintArtifacts: true, flatten: true, projectName: 'branch/name', selector: lastSuccessful(), filter: "webpackage.*", target: './artifacts'])
When I run without 'filter' criteria, all artifacts are copied. When I use 'filter' as used above, I have this error:
ERROR: Failed to copy artifacts from branch/name with filter: webpackage.*
Jenkins and agent are running on Windows SO.
Should I specify path diferrently?
According to the documentation CopyArtifact uses ant expression syntax. Hence this probably works on paths. Also I would skip the dot.
steps {
step([$class: 'CopyArtifact', fingerprintArtifacts: true, flatten: true, projectName: 'branch/name', selector: lastSuccessful(), filter: "**/webpackage*", target: './artifacts'])

Copy artifacts from an upstream multi-branch pipeline

I have the following Jenkins setup:
A multi-branch pipeline which sometimes (on certain tag builds) triggers
a pipeline that builds an installer from the upstream artifacts.
In the upstream MB-pipeline, I have the following fragments:
options {
copyArtifactPermission('my-downstream-project');
}
post {
success {
script {
if (isRelease()) {
build job: 'my-downstream-project'
}
}
}
}
The downstream pipeline, I then try to grab the artifacts:
copyArtifacts projectName: 'my-upstream-project',
selector: upstream(),
filter: '*.jar',
fingerprintArtifacts: true
While the downstream build is started, it fails with:
ERROR: Unable to find project for artifact copy: hds-access-code-cache
This may be due to incorrect project name or permission settings; see help for project name in job configuration.
My understanding so far:
While I can't configure the Copy Artifact permission via the configuration UI for the MB-pipeline, the option is accepted and should work.
The examples I can find would use projectName: 'my-upstream-project/tag-name' as that's the actual job. I don't have a fixed branch or tag, though.
How can I correctly access the upstream artifact?
It is possible to pass down the job name as parameter.
Change the upstream pipeline to:
build job: 'my-downstream-project',
parameters: [string(name: 'upstreamJobName', value: env.BRANCH_NAME)]
Add the parameter to the downstream pipeline:
parameters {
string(name: 'upstreamJobName',
defaultValue: '',
description: 'The name of the job the triggering upstream build'
)
}
And change the copy directive to:
copyArtifacts projectName: "my-upstream-project/${params.upstreamJobName}",
selector: upstream(),
filter: '*.jar',
fingerprintArtifacts: true
Et voila:
Copied 1 artifact from "My Upstream Project » my-tag" build number 1

How to Copy Artifacts from other Jenkins Job from a Pipeline?

I want to copy a build artifact from another Jenkins Job using the CopyArtifact plugin.
The artifact is created using the following command:
archiveArtifacts artifacts: '_Builds/BuildRelease/**', fingerprint: true
build 'Release Installer'
Within the 'Release Installer' job, I try the obtain the archived artifacts
using the following command within the Pipeline:
stages {
stage('Get Artifacts') {
steps {
step([ $class: 'CopyArtifact',
projectName: "MyBuildJob",
filter: "_Builds/BuildRelease/archive.zip"
])
}
}
When the "Release Installer" Job is executed, the artifact is not found.
Both jobs are executed on the same Build node.
I think my filter rule is missing something. Unfortunately the available Jenkins documentation is a little thin on details and examples.
I believe that the default is to copy from the last successful job. However, it looks like you currently want the upstream job. Here is a snippet:
copyArtifacts fingerprintArtifacts: true, projectName: 'MyBuildJob', selector: upstream()
I generated this code using the snippet generator. It should exist on the left panel of the classic view of a job. The button text reads "Pipeline Syntax" and the url is "my.jenkins.instance.com/pipeline-syntax/"
Specifying an artifact filter is not required, it will copy all of them. However if you want to keep the filter:
copyArtifacts filter: '_Builds/BuildRelease/archive.zip', fingerprintArtifacts: true, projectName: 'MyBuildJob', selector: upstream()

COPYARTIFACT_BUILD_NUMBER_SUFFIX in workflow jobs

Does the COPYARTIFACT_BUILD_NUMBER_SUFFIX work on expected lines in workflow/pipeline jobs?
copy artifact command am using is
step([$class: 'CopyArtifact', filter: '*.rpm', fingerprintArtifacts: true, projectName: 'test_pkg', resultVariableSuffix: 'testsuffix', selector: [$class: 'ParameterizedBuildSelector', parameterName: 'testparam'], target: 'test_packages'])
when i do
println COPYARTIFACT_BUILD_NUMBER_TESTSUFFIX
am getting the below error
groovy.lang.MissingPropertyException: No such property: COPYARTIFACT_BUILD_NUMBER_TESTSUFFIX for class: WorkflowScript
I am also having the same problem. What I (and I guess KishorePP too) want to achieve is, to retrieve the BuildNumber from the build that generated the artifacts.
But the environment variable is somehow not existent or differently named.
The plugin I use is this: https://wiki.jenkins-ci.org/display/JENKINS/Copy+Artifact+Plugin
As in source Code of https://github.com/jenkinsci/copyartifact-plugin/blob/master/src/main/java/hudson/plugins/copyartifact/CopyArtifact.java#L739 we can see that there the Variable is pushed into the env map.
Try doing a printenv in your shell script.

Resources