COPYARTIFACT_BUILD_NUMBER_SUFFIX in workflow jobs - jenkins

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.

Related

In Jenkins pipeline, how to set a value for an environment variable when loading a shared library?

I have a multi-branch pipeline that uses a Jenkinsfile to load a shared library defined in my system configuration.
#Library("my-shared-library") _
import com.company.exa.builders.BaseBuilder
import com.company.exa.builders.EdiBuilder
import hudson.model.*
buildNumbers = getBuildNumbers() // Function not shown, but it works
properties ([
disableConcurrentBuilds(),
[$class: 'jenkins.model.BuildDiscarderProperty',
strategy: [$class: 'LogRotator',
numToKeepStr: '50',
artifactNumToKeepStr: '20']],
parameters ([
choiceParam(name: "VERSION_CHOICE",
choices: buildNumbers,
description: "Version from Builds"),
stringParam(name: "VERSION_PASSEDIN",
defaultValue: env.BRANCH_NAME,
description: "Passed-in version. Note this will override VERSION_CHOICE."),
booleanParam(name: "UPLOAD_ARTIFACTS",
defaultValue: false,
description: "Upload artifacts to file servers?"),
choiceParam(name: "DEBUG_LEVEL",
choices: ["0", "1", "2", "3"],
description: "Debug level; 0=less verbose, 3=most verbose")
])
])
When I run it clicking Scan Multibranch Pipeline Now, I get
00:00:01.018 Loading library my-shared-library
00:00:01.019 Attempting to resolve maser from remote references...
00:00:01.019 > git --version # timeout=10
00:00:01.023 > git --version # 'git version 2.17.1'
00:00:01.023 using GIT_SSH to set credentials Jenkins Master SSH
00:00:01.028 > git ls-remote -h -- git#bitbucket.org:cfouts-kmha/kmha-infrastructure.git # timeout=10
00:00:01.546 Found match: refs/heads//master revision a1bc1e273b41c4e892d7c25814d0f2a1c261f7e5
00:00:01.546 ERROR: Checkout failed
00:00:01.546 java.lang.IllegalArgumentException: Null value not allowed as an environment variable: VERSION_PASSEDIN
00:00:01.546 at hudson.EnvVars.put(EnvVars.java:379)
00:00:01.546 at hudson.model.StringParameterValue.buildEnvironment(StringParameterValue.java:59)
...complaining that variable VERSION_PASSEDIN is null. I've tried setting the VERSION_PASSEDINvariable to just "" in the following locations to no avail...
The multi-branch pipeline's Folder properties
The multi-branch pipeline's parent folder properties
In the Jenkinsfile itself
In the System configuration global properties
Any clues on how to fix this? I have a feeling it's something obvious that I'm not seeing.
Note that if I run the job with a branch's "Build with parameters" link, the job runs fine.
You have a chicken-and-egg problem when trying to set default value for parameters, e.g. here:
stringParam(name: "VERSION_PASSEDIN",
defaultValue: env.BRANCH_NAME,
To run your pipeline, Jenkins needs to figure out the value of the parameters. However, to figure out the value of the parameters, Jenkins needs to run your pipeline. See the problem?
To overcome this problem, here's what happens: Jenkins assumes the value of nothing for both VERSION_CHOICE and VERSION_PASSEDIN. In the first case, it's an empty choice; in the second, it's null.

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'])

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()

Copy artifact within a jenkins pipeline

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

Updating Jira tickets from Jenkins workflow (jenkinsfile)

How can I update a jira issue from within a Jenkinsfile (jenkins-worflow/pipeline)?
Is there a way I could use the Jira Issue Updater plugin as a step in the Jenkinsfile?
I know I could use the Jira RestAPI, but I'm trying to figure out if I can re-use the functionality provided by the jira-updater-issue.
What I'm looking for is a something similar to the example below calling Junit archiver, and atifact archiver, but calling jira updater.
node {
git url: 'https://github.com/jglick/simple-maven-project-with-tests.git'
def mvnHome = tool 'M3'
sh "${mvnHome}/bin/mvn -B -Dmaven.test.failure.ignore verify"
step([$class: 'ArtifactArchiver', artifacts: '**/target/*.jar', fingerprint: true])
step([$class: 'JUnitResultArchiver', testResults: '**/target/surefire-reports/TEST-*.xml'])
}
The Jira Plugin is compatible with Pipeline.
This should work:
step([$class: 'hudson.plugins.jira.JiraIssueUpdater',
issueSelector: [$class: 'hudson.plugins.jira.selector.DefaultIssueSelector'],
scm: [$class: 'GitSCM', branches: [[name: '*/master']],
userRemoteConfigs: [[url: 'https://github.com/jglick/simple-maven-project-with-tests.git']]]])
You can get a full reference in the built-in Pipeline Snippet Generator.
The JIRA Steps Plugin provides a more declarative way to update an existing Jira Ticket:
node {
stage('JIRA') {
# Look at IssueInput class for more information.
def testIssue = [fields: [ // id or key must present for project.
project: [id: '10000'],
summary: 'New JIRA Created from Jenkins.',
description: 'New JIRA Created from Jenkins.',
customfield_1000: 'customValue',
// id or name must present for issuetype.
issuetype: [id: '3']]]
response = jiraEditIssue idOrKey: 'TEST-01', issue: testIssue
echo response.successful.toString()
echo response.data.toString()
}
}
Since you would like to use the Jenkinsfile to define your pipeline, that should be the preferred way for you to go...
As this was way harder for me than it should be, here is a working example. This will update a custom field of a ticket with a specific value:
step([$class: 'IssueFieldUpdateStep',
issueSelector: [$class: 'hudson.plugins.jira.selector.ExplicitIssueSelector', issueKeys: ticket],
fieldId: field,
fieldValue: value
])
The snippet generator did not work for me. The variables ticket, field and value are all strings. Starting from this you can look for options here: https://www.jenkins.io/doc/pipeline/steps/jira/
Yes, seems like this page answers your question:
https://wiki.jenkins-ci.org/display/JENKINS/Jira+Issue+Updater+Plugin
After you install the plugin, add a build step, or pre/post build step to call this plugin
There you can give it the REST URL to your Jira server, the creds and the JQL to find the issues

Resources