I am using a multibranch pipeline in Jenkins and I need to set the name of the custom workspace directory dynamically based on the branch name that is being built. When I define the custom workspace for the pipeline, I attempt to access the environment variable that contains the branch name as follows:
pipeline {
agent {
node {
label 'master'
customWorkspace 'some/path/${BRANCH_NAME}'
However, during git init of the workspace, the string interpolation does not happen. Instead of inserting whatever the branch name is into the string, it tries to set the workspace name as '${BRANCH_NAME}' without interpolating the branch name variable. If I interpolate the variable outside the agent block (for example, if I interpolate the variable in a stage block), the string interpolation occurs perfectly fine and I can receive the name of the branch from Jenkins.
What might be the cause of this problem? Are there any other ways to set a custom workspace based on the branch name in a multibranch pipeline?
Thanks!
The string interpolation works with double quotes, but does not work with single quotes. When I change the syntax to the following, it works:
pipeline {
agent {
node {
label 'master'
customWorkspace "some/path/${BRANCH_NAME}"
Notice the double quotes used around the customWorkspace definition.
Apparently, Groovy cannot execute string interpolation on single-quoted strings. For some reason, however, string interpolation in single-quoted strings works outside the agent block (such as in a stage block), but refuses to work in the example above.
Related
I have a jenkins pipeline which uses the defualt choice parameter.
parameters {
choice(choices: ['optA', 'optB'], description: 'Some options', name: 'CHOSEN')
}
Whenever it run the chosen options will always have a comma appended to it. Resulting in the step it's used in taking it as part of the argument.
someprogram.sh optA,
This breaks everything. How do I disable the comma? Also why is this default behavior?
I tried removing it with a combination of shell substition and jenkins groovy string manipulation.
I searched for the option 'omit field value' which 'fixes' this for a choices plugin (I am not using the reactive choices plugin but this is the only response I could find) When I try to find the 'omit field value' box in pipeline settings in parameters it doesn't exists.
Try replacing it like below.
sh "someprogram.sh ${params.CHOSEN}"
I have two Jenkins nodes, but only one node can be ran on special parameter, how do I add restriction to the master that if the parameter was set to true the job will run on target node?
You can configure to accept a parameter that indicate if its special or not.
In jenkins groovy job just use a conditional if to check if parameter is send as special one or its normal accordingly you can create a new variable like
def Agent_Var = Special_Agent (The label of special agent)
Note - please configure label for the agents to determine which agent to use.
after setting up Agent_Var variable you can define Agent_Var label in pipeline as below -
pipeline {
agent { label "$Agent_Var " }
I am using a password parameter option in jenkins pipeline job where i store a password and need to inject that into the environment of jenkins file
Here is the part of the jenkins file where i am trying to inject this PASS
pipeline {
agent any
environment {
USER= 'abcd'
PW= '${params.PASS}'
}
stages {
stage('staring tests') {
When i try echo-ing $PW, it just echoes ${params.PASS}.
Any pointers will be appreciated!!
In Groovy Strings are single quotes. They cannot be interpolated with variables. You would need to concatenate strings and values. i.e. PW='pass' + myVar
GStrings are double quotes. http://docs.groovy-lang.org/latest/html/api/groovy/lang/GString.html
So you would need to change it to PW="${params.PASS}" and this would interpolate the variable into the string.
However since your not doing any interpolation in that example, you don't need to use GStrings either. Whereever you need access to that value just call it directly echo params.PASS
You should not put a 'params' value into an environment variable. It won't work for you. The environment sections initialize only string variables, therefore, it initializes your PW with a pure string of ${params.PASS} since it does no 'calculations' for it.
Instead, in your pipeline, just use the params.PASS directly in your code.
There is declarative pipeline. In the beginning of pipeline block the agent selection is made using agent directive. Label-based selection is being conducted. Agent selected this way is the standard/default agent.
How to set for whole pipeline a special agent when certain condition is met?
The plan is to do condition check based on pipeline's one parameter >> can that work?
What are the points the chosen approach needs to address?
Current solution blueprint:
Groovy code prior to pipeline block
Mentioned groovy code sets a variable based on value of pipeline's parameter how to access pipeline's parameter from within Groovy code located out of pipeline?
agent section uses variable set in Groovy code matching label special agent got attached to
Both Jenkins.io and Cloudbees do not support dynamic agent selection with Declarative Pipeline syntax. Hence adding "when" expression within agent block will not work. However, the below approach can be tried
1. Create pipeline library - with a groovy file in vars folder. Keep all the stages inside this file and parameterize the "Agent" block
2. Jenkinsfile - embed the library inside Jenkinsfile and invoke the above groovy file using call(body) syntax. Pass agent deciding parameter from this Jenkinsfile.
For library syntax, please refer this url
Shared Library syntax
I want to get the name of the Jenkins job. The name is a Jenkins environmental variable called JOB_NAME (and which looks like project_name/branch). JOB_BASE_NAME strips off the project_name/, leaving branch.
I want to get project_name.
I thought there might be something like this I could do:
environment {
NAME = "${env.JOB_NAME}.replaceAll(~/\/.*$/, '')
}
and then use ${NAME} later in the pipeline, but this doesn't seem to work
You can also do env.JOB_NAME - env.JOB_BASE_NAME and then drop one character off the end of that if you need to lose the final /.
Once you've defined something in your environment closure, it should be accessible not as a normal variable like NAME, but instead like the predefined env variables, such as env.NAME