Jenkins - Build Steps - Changes Build Description - Token Macros Don't Work - jenkins

In a Jenkins Freestyle Job Config, I am trying to use "Build Steps -> Changes Build Description" to provide a URL to an external website where I have stored build artifacts.
From the job config inline help for "Build Steps -> Changes Build Description" for Freestyle Jobs:
Normally, build description is empty, but it can be changed by setting what name new build gets. This field can contain macros.
But attempting to do a Token Macro transform in that field produces an error, despite documentation indicating that the transform is supported.
Jenkins Version : CloudBees CI Managed Controller 2.361.3.2-rolling
Plugin Version : Token Macro Plugin Version 308.v4f2b_ed62b_b_16
Assume the following Build Parameters:
PARAM1=my_job
PARAM2=test_param_22_01_03
I want to use the last 8 characters of a Build Parameter to form the URL to supply in the Build Description, e.g. :
Link to Artifacts
Per Jenkins Documentation on Token Transforms , the PARAM2 token macro transform above is documented as supported, but in my console log, I see:
Failed to evaluate description macro:org.jenkinsci.plugins.tokenmacro.MacroEvaluationException: Error processing tokens
If I specify just the parameters ${PARAM1} and ${PARAM2} with No Token Transform, e.g:
Link to Artifacts
All works OK, but as soon as I attempt to extract a substring from PARAM2, I get the error above. What am I doing wrong?

Related

Jenkins (freestyle project) secret text using Parameter expression

I have a python project that I'm using pytest with integration to Report Portal.
I execute the tests from Jenkins (freestyle project).
In order to to get the user name for each session in Report Portal, I need to know the Report Portal access_token of the user.
After finding those, I added to Jenkins for each user a secret text with the ID as the user Jenkins BUILD_USER_ID.
Next, In the Jenkins configure page, I clicked on Use secret text(s) or file(s) under Build Environment
Then clicked on Add and Secret text.
I called the Variable RP_UUID.
And set Credentials to Parameter expression.
Jenkins gave me a hint:
A valid parameter expression consists of the parameter name enclosed
within ${ and }
I tried to set this filed to be ${ BUILD_USER_ID }, but I got the following error:
Rebuilds build #17
Running as SYSTEM
Building remotely on Automation in workspace /var/lib/jenkins/workspace/test
[WS-CLEANUP] Deleting project workspace...
[WS-CLEANUP] Deferred wipeout is used...
[WS-CLEANUP] Done
ERROR: Could not find credentials entry with ID '${ BUILD_USER_ID }'
Finished: FAILURE
I tried all sorts of things, but it seems that Jenkins in this stage doesn't recognize any variable name.
Any idea how to resolve this, or any different approach?
p.s.
The field Set Jenkins user build variables is ticked.
See JENKINS-36470 the only parameter type that can be expanded in the Parameter expression section is a Credentials Parameter. All other parameters are not supported.
So if you want to use this mechanism you must define the input parameter as a Credentials type:
You'd probably need to set "Run as SYSTEM" in Access Control for Builds at
https://jenkins-url/configureSecurity
When I used "Run as SYSTEM" just for "Project default Build Authorization" it didn't work. Once I set it also under "Per-project configurable Build Authorization" - Then the triggered job could evaluate the parameter expression for the credentials bindings.

Jenkins-cli's set-build-parameter command not working anymore, how to modify the value of a build parameter across build steps?

In our Jenkins job, we need to sent an email containing a url in a post-build step (we also post the url to a Slack channel, but that's just about the same thing). The value of the url is dependent on the result of the build. Modification to an environment variable doesn't persist across build step. Writing the url to a file in one step and reading it in another is not an option either since the email/slack plugin has no ability of shell script execution. As a result, we've been using build parameter to solve this issue and the set-build-parameter command of jenkins-cli works fine.
After a recent upgrade of Jenkins (we're now at 2.75), set-build-parameter fails to work anymore. It reports the following error:
ERROR: Failed to identify the build being executed
According to Jenkins-cli's help command, set-build-parameter seems to be deprecated:
set-build-parameter
Update/set the build parameter of the current build in progress. [deprecated]
So, what's the correct way to modify the value of a build parameter across build steps in the latest version of Jenkins?
I frequently manipulate build parameters in shell script blocks, you have to edit them, save them in a file f. e.:
buildParamter=true>variable.txt
Now you can use the envInject Plugin and reference the the newly created file.

How to build with parameters in Jenkins from Gitlab push?

I have GitLab Community Edition 8.15.2 successfully trigger pipeline projects in Jenkins 2.32.1 using a webhook. I want the gitlab push to trigger a build with parameters but the parameter value is null when it comes through so the build fails.
The gitlab webhook looks like:
http://jenkins.server:8080/project/project-a/buildWithParameters?MYPARAM=foo
In my pipeline project I echo the parameter value out with
echo "MYPARAM: ${MYPARAM}"
and it's not set to anything. Any ideas on where I've gone wrong?
UPDATE
The actual code I'm using in the pipeline is:
node {
try {
echo "VM_HOST: ${VM_HOST}"
echo "VM_NAME: ${VM_NAME}"
stage('checkout') {
deleteDir()
git 'http://git-server/project/automated-build.git'
}
stage('build') {
bat 'powershell -nologo -file Remove-MyVM.ps1 -VMHostName %VM_HOST% -VMName "%VM_NAME%" -Verbose'
}
...
}
}
The parameter VM_HOST has a default value but VM_NAME doesn't. In my Console output in Jenkins I can see:
[Pipeline] echo
VM_HOST: HyperVHost
[Pipeline] echo
VM_NAME:
I have been struggling with this for weeks. I had it working once, but I couldn't get it to work again, untill today. And the solution was mindblowingly obvious ofcourse...
Automatically for each pipeline job I ticked the following box:
Build when a change is pushed to GitLab. GitLab CI Service URL:
http://jenkins.dev:8080/project/MyProject
Then from GitLab I used the webhook to trigger the above.
Like you I tried to add /buildWithParameters and tried many other things that didn't work.
The problem was, I ticked the wrong checkbox!
Since I trigger the build from a GitLab webhook, the above checkbox (build when a...) does not have to be checked at all.
What needs to be checked is:
Trigger builds remotely (e.g., from scripts)
That checkbox provides you with a new URL:
Use the following URL to trigger build remotely:
JENKINS_URL/job/MyProject/build?token=TOKEN_NAME or
/buildWithParameters?token=TOKEN_NAME
Like all the documentation I came along states and as you can see, the URL now no longer starts with /project, but with /job instead!
So tick that box and change your URL accordingly:
http://jenkins.server:8080/**job**/project-a/buildWithParameters?token=TOKEN_NAME&MYPARAM=foo
Least I want to mention the token:
In the GitLab webhook there is a seperate field for "token", which states:
Use this token to validate received payloads. It will be sent with the request in the X-Gitlab-Token HTTP header.
So, the token provided there will be sent along the request as a HTTP header.
This is the token which can be provided globally in the Jenkins setup.
The token you must provide in the Jenkins job when ticking the box Use the following URL to trigger build remotely must be send in the URL as GET parameter, just like the example shows.
Final note: personally I have never got this working completely, because I don't get the Jenkins CSRF protection off my back. Disabling it gives me another error. However, hopefully the above does fix the problem for you and others.
GitLab plugin does not allow you to pass arbitrary parameters. In their project there is an open issue for it that deserves to be upvoted.
My convoluted solution was to use the desired values for the push trigger as the default parameters of the job. Then I used the Parameterized Scheduler plugin to use other values in the scheduled executions.
The problem is that I got a bad usability for the job when it was manually run, since the default parameters were appropriate for the push hook.
I found the solution here https://www.jittagornp.me/blog/jenkins-gitlab-webhook/
I verified it with Jenkins 2.263.1 and GitLab Community Edition 13.6.1
Your webhook url will look like
https://hunter:11a403302a4f01b9b4975c0ac27441a5cc#jenkinsservername.com/job/yourjenkinsproject/buildWithParameters?token=Aju9ryHUu6t7W8wLSeCWtY2bWjzQduYNPyY7B3gs&yourparam=yourvalue
"hunter" ist your username in Jenkins.
The following is the Jenkins API Token you have to create in your Jenkins User Managment independent of the project.
The last Token is the one you specify in the jenkins project options under "Trigger builds remotely (e.g., from scripts)"
The last thing is to add your Parameter and value to the url with &param=value

Does jenkins job dsl plugin support "parameter filter"?

Is "parameter filter" supported in Job DSL plugin? What I am trying to do is to copy artifacts from another project when the build parameter task_id has the same value as the current project, e.g.
Build step: Copy artifacts from another project
Project Name : SomeJob
Which build : Latest successful build
Parameter filters : task_id=${task_id}
In Job DSL documentation, the following contexts are supported but I don't find "parameter filter":
includePatterns
excludePatterns
targetDirectory
flatten
optional
buildSelector
Thanks!
It seems that this is not yet supported by Job DSL, but a pull request changing this has been submitted 2 hours ago.

Parameter List in Jenkins to display list of build numbers from another build

I have two Jenkins builds, one for compiling and one for deploying.
The developer wants to be able to choose a build from the compiler build when running the deploy build, not always run the most recent build.
What I am after is a method of populating a choice parameter for the deploy build with a list of successful\unstable builds from the compile build.
I will then use the the option listed in the parameter to deploy that artifact.
Using the Dynamic Parameters plugin
In your promote job:
[x] This build is parameterized
Add Parameter
Dynamic Choice Parameter
Set Name to whatever
Paste below into Choices Script
import jenkins.model.Jenkins
import hudson.model.AbstractProject
import hudson.model.Result
import hudson.util.RunList
AbstractProject<?, ?> otherJob = Jenkins.getInstance().getItemByFullName("otherJobName", AbstractProject.class)
RunList<?> builds = otherJob.getBuilds().overThresholdOnly(Result.SUCCESS)
def list = builds.limit(5).collect { it.number }
Screenshot from wiki page:
As Dynamic Parameters plugin is no more accessible. You can use Active Choice parameter plugin in Jenkins.
Now you can list all successful Jenkins builds as parameterized option in Jenkins Job/pipeline
Follow below steps to access the list of successful jobs [as dropdown list]
In job configuration [General section] select this project is parameterized
Select add parameter as "Active choice parameter"
Give name for parameter
Select groovy script and paste below code in groovy script text box
return jenkins.model.Jenkins.instance.getJob('<Jenkins-job>').builds.findAll{ it.result == hudson.model.Result.SUCCESS }.collect{ "$it.number" }
It worked awesome without without powershell and BASH
No need to process Jenkins API and filter JSON output
One option is to use the Promoted Builds plugin to mark a specific build to be deployed. This moves the choice from the deployment build into the compilation build. Select the Promote builds when... option in the compilation build and set up how you want promotion to work. The developer could choose (or automate) the build to promote. In the deployment build, the Copy Artifact plugin can grab the appropriate build (based on a permalink to the latest promoted build).
As far as I know, it is not possible to populate the choice parameter. However, you don't need to always use the newest build. I assume that you use the copy artifact plugin. This plugin provides the "Build selector for Copy Artifact" parameter. You still need to enter the build number manually, but when deploying you have all the standard choices, like "Latest successful build", but also "Specific Build". You need to enter the number and don't have a drop down, but I got my deployers trained well enough to enter the build number.

Resources