Injecting variable from jenkins into jenkins pipeline - jenkins

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.

Related

Variable indirection in a Jenkins pipeline?

In my Jenkins system configuration page, I have 3 variables defined, namely, sandbox_deployed, staging_deployed, and production_deployed. In my pipeline, I want to access one of these variables, based on a pipeline property, BUILD_ENV, defined in the job's configuration page. In other words, in my job's configuration page I have
BUILD_ENV=sandbox
How can I write pipeline code that does
println "$env.${env.BUILD_ENV}_deployed"
If I write it like in the above println, I get
org.jenkinsci.plugins.workflow.cps.EnvActionImpl#336841dd.sandbox_deployed
But I really want this
println "env.sandbox_deployed"
which prints out the correct value of the sandbox_deployed variable.
Try this code, at least it worked for me
println "${env."${env.BUILD_ENV}_deployed"}"

Jenkins Environment Variable String Interpolation for Setting Custom Workspace

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.

Jenkins pass variable into groovy script

Hi I need to pass a variable from select into the groovy script and I haven't a clue how to do it in variable bindings, anyone has an idea how to do it?
I tried with:
version=version
version=$version
version=${version}
version="${version}"
If you are going to use ${ ... } Notation you should enclose it double quotes, i.e.:
versionVar = "${version}"
The other think that keeps bothering me is that you are using the same variable name. I haven't tried but I think that using the same name you could are trying to use the same variable.
Where is the 'version' variable came from?
If you are trying to use Environment variables, then you should try like,
version = env.version

Reference groovy variables over multiple script parts in jenkins

In a Jenkins job, I have a groovy script, that is split in two parts. It does something before SCM and does some other thing at the end of the job as last build instruction.
Now, I need to access a variable in the second part, that I set in the first part.
How to do that?
I tried to mark a variable as a field with #Field Boolean myFlag = false, but still myFlag cannot be accessed in the second part of the script.
Interpreter says:
groovy.lang.MissingPropertyException: No such property: myFlag for class: Script1
Does anyone know how to accomplish accessing a variable from the first part of the script in the second?
Thanks!
you can pass these variables as the Jenkins Job's parameters. That way you have access to the variables throughout.
It was not clear from your question if you're running a single script in the job or if you're executing multiple scripts.

How to replace part of a declarative Jenkinsfile pipeline variable to get the name of the job?

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

Resources