Jenkins pass variable into groovy script - jenkins

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

Related

JenkinsFile parameters settings for key:value pair not allowing strings

I'm trying to set a variable for a key value pairing in a jenkinsfile and can't get it to recognize the variable as a string.
zip = "name_of_zip_file_to_use"
createZipFile = [src:"./test", destination:"./"+zip+".zip"]
I have tried to use the variable zip as the whole string also but nothing seems to work. I'm not sure why it isn't recognizing the destination value variable as a string. Any ideas why I can't get this to work in the jenkinsfile?
Referencing and interpolating variable is possible this way - using ${VARIABLE_NAME} syntax. So in your case it would look:
zip = "name_of_zip_file_to_use"
createZipFile = [src:"./test", destination:"./${zip}.zip"]
If interested more about referencing variables and/or a string concatenation, see docs or similar StackOverflow topics:
variable interpolation in groovy: https://www.jenkins.io/doc/book/pipeline/jenkinsfile/#string-interpolation and http://docs.groovy-lang.org/latest/html/documentation/#_string_interpolation
how to concatenate strings in a Jenkinsfile?

Injecting variable from jenkins into jenkins pipeline

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.

Using git branch in environment variable in Jenkinsfile

I would like to customize one of environment variable depending on branch that is being build.
environment {
CUSTOM_ENV='xyz_${GIT_BRANCH}'
}
I'd like to get CUSTOM_ENV=xyz_dev for origin/dev and CUSTOM_ENV=xyz_master for origin/master. Not sure if its important, but its multibranch project in Jenkins.
I tried things like xyz_${GIT_BRANCH} or xyz_env.GIT_BRANCH, but none of this worked out.
If your shell happens to be compatible with ksh or bash then you can use the variable-expansion modifier ## to discard everything up to and including the final / character, leaving get just the part of ${GIT_BRANCH} that comes after that /. That would look like:
CUSTOM_ENV="xyz_${GIT_BRANCH##*/}"
Note the double-quotes " rather than the single-quotes ' you used in your question. Single-quotes prevent the evaluation of variables inside the quoted string, and that's definitely not what you want in this case.
If your shell does not understand the ## modifier then you'll have to use something like sed to get just the last part of ${GIT_BRANCH}. That would look like this:
CUSTOM_ENV="xyz_$(echo ${GIT_BRANCH} | sed -e 's#.*/##')"
When you are doing a substitution in jenkinsfile for a variable. It should always be in "". From environment directive , I guess you are using pipelines. So, you can leverage the groovy syntax to achieve string manipulation.
Something like,
environment {
GIT_BRANCH = 'orgin/master'.split('/')[1]
CUSTOM_ENV="xyz_${GIT_BRANCH}"
}

How to write Travis env variable to file?

I'm trying to write a custom Travis env variable to a file for a simple proof of concept thing that I need. However, I'm having trouble getting this to work.
How would I define this in the travis yaml file if my variable is called VARIABLE_X ?
Thanks!
One way to do this is using linux commands, something like:
printenv | grep VARIABLE > all_env
However I don't know how Travis handles the environment (take a look at their docs, here) but it might not work as easily due to encryption, but it should work since your apps wouldn't function if they didn't have the same level of access. If such a case occurs, modifying a few parameters (maybe TRAVIS_SECURE_ENV_VARS) is worth looking into.
If you solved the problem in another way, consider sharing with the community.
Write the environment variable as usual (Shell - Write variable contents to a file)
Define the following within script:
- echo "$VARIABLE_X" > example.txt

How can I define values from outside my script?

I'd like to pass some variables into my .nsi script. Either from the environment or the command line, how do I do this?
I found a section in the documentation that suggests I can use the syntax $%envVarName% to use environment variables in my script, but this doesn't seem to work, when I have
File "/oname=$pluginsdir\inst.msi" "$%VERSION%-Installer-64bit.msi"
I get the error
File: "$%VERSION%-Installer-64bit.msi" -> no files found.
$VERSION is in my environment.
Is there something I'm doing wrong with trying to read environment variables, or some other way of passing values into my script?
$%VERSION% should work if you used set VERSION=1.2.3.4
Or you can create defines: makensis -DVERSION=1.2.3.4 myscript.nsi and File: "${VERSION}-Installer-64bit.msi"

Resources