Jenkins token expansion doesn't accept parameters? - jenkins

I want to pass a version label like master.104.a1b2c3d down to a triggered build. Using ${GIT_BRANCH} in its default shape I get origin/master instead of master, which I don't want. The Token Macro plugin suggests I should be able to add parameters (and the code suggests a fullName parameter exists) so I need to be able to apply these parameters to manipulate the output.
Expected:
target=c42cccac2cc…
versionLabel=master.104.c42ccca
Observed:
target=c42cccac2cc…
versionLabel=${GIT_BRANCH,fullName=false}.104.${GIT_REVISION,length=7}
Perhaps I'm using the parameters incorrectly? Even the example from the documentation doesn't work here, but any expansion without parameters goes just fine.

The additional parameters won't work everywhere in jenkins config. See JENKINS-25465 for details.

Related

Can I pass jenkin's buildId to copyArtifacts instead of a projectName+selector?

the normal way to used the copyArtefact-step in a jenkinsfile would be to use the parameters projectName and selector to specify a target, like so:
copyArtifacts(projectName: 'my_project/master', selector: specific("1234"));
But $BUILD_ID already contains the necessary information, so it should be possible to pass the build-id to copyArtefacts instead of the other two informations, like so
copyArtifacts(buildId: 'jenkins-my_project-master-1234');
unfortunatly copyArtifacts doesn't have a parameter buildId (at least not that I know of).
My experience with jenkins have shown me, that there often are some shortcuts (like special notations or java-classes) that can be used, so hopefully there is no need to do magic stringoperations in bash to extract the needed parameters.
Background: We store the build-Id as a label in a dockerimage, which we then use in different other jenkins-projects and also outside-jenkins. One of the other jenkins-projects doesn't only need the docker-image, but also the source-code used to build the image. I want to use copyArtefacts to give the source-code to that one jenkins project.

Generating a dynamic parameter in Jenkins based off a parameter from the same job

I've got a Jenkins build with a choice box for build prefixes by release. It helps trigger a job based off the value of whatever specific build the person wanted.
I wanted to take the value of that choice box and transform the variable into the correct prefix based off the naming conventions typically used on this server for triggering the job based off its name.
So let's say I've got build prefix choices specifically for,
ReleaseOne
ReleaseTwo
none
For none, meaning the parameters used won't try to access or set any specific release-based info by triggering the non-release-specified build.
I wanted to take the value of Release_Prefix and transform it, if needed, for the job that I trigger later. I was hoping to accomplish this with a dynamic parameter or similar mechanism. I'm not sure if my script is bugged, or something fundamental is not working to my intent. This might be the case, based off some alluded feedback from a similar question.
Can I do something like this snippet below? If not with Dynamic Parameter plugin + GroovyScript, what would you suggest? This currently seems to return nothing, regardless of my choice.
Formatted_Prefix parameter, Dynamic Parameter
switch(binding.getVariables().get("Release_Prefix"))
{
case "none":
return "";
case "ReleaseOne":
return "ReleaseOne_";
case "ReleaseTwo":
return "ReleaseTwo_";
default:
def prefix = binding.getVariables().get("Release_Prefix")
return "$prefix_";
}
There's multiple ways I can overcome this, but if I can do it at the initial parameter stage, that would be best for me.
You can use EnvInject Plugin for this.
check the checkbox Prepare an environment for the run and
write your script inside Evaluated Groovy script text box
def prefix1 = Release_Prefix + "mydata"
return[prefix:prefix1]

How to get interactivity with maven release plugin

The maven release plugin in Jenkins prompts you for input when doing a release:prepare. It'll give you a suggested value, but you also have the choice to override that suggested value to put your own value. How can you do this same thing when writing a Jenkins pipeline in groovy? The only thing that I've found online is -> https://gist.github.com/cyrille-leclerc/552e3103139557e0196a. This method is strictly command and just gives batch mode with no user interaction.
The parameter definitions passed to the input step can specify default values. Try it in Snippet Generator.

In Jenkins can I use a parameter value in other config fields?

I am setting up a parameterized build on my Jenkins server.
Basically I want to have the git branch name as a parameter. Then I want to use that parameter in various other fields in the job config.
I don't know if this is even possible, but I hope that it might be as it seems an obvious need.
The only docs I could find is this old wiki page
https://wiki.jenkins-ci.org/display/JENKINS/Parameterized+Build
It shows that build parameter is available as an ENV var, but it doesn't show how to use it elsewhere in the job config.
despite being undocumented, this syntax works in job config fields
${PARAM_NAME}
Just use $PARAM_NAME anywhere in the configure.
The easiest way is to use the "Git parameter" parameter type in your config and then you can reference that simply in the Source Code Management section - see here. This assumes that the Jenkins instance has the Git parameter plugin installed.
If you don't have that option, then:
you can simply add a String parameter to the configuration and use that in you configuration, but you need to uncheck "Lightweight checkout" to avoid errors like this:
stderr: fatal: Couldn't find remote ref refs/heads/${BRANCH}
String parameter declaration:
String parameter usage:
Source of pictures

How to read Jenkins Parameters?

I am trying to Read Parmeters in jenkins job. I have set String Parameter for Job as follows.
but when I am tring to read this parameter in windows batch command as
it wont read it's value but simply return variable name only as follows
I have also installed parameterized trigger plugin.
Is there any additional plugin I need to install?
Kindly suggest
In Windows, it's %NODENUMBER% (or when using delayed expansion: !NODENUMBER!)
In *nix, it's $NODENUMBER or ${NODENUMBER} (curly brackets are only required when there is ambiguity with adjacent words, but are a good habit anyways)

Resources