Using git branch in environment variable in Jenkinsfile - jenkins

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}"
}

Related

Evaluate variable in Jenkinsfile - single quotes within single quotes

I am executing the following in a Jenkinsfile:
sh('''
terraform apply 'var some_list=["foo","bar"]'
''')
Now, I would like to place ["foo","bar"] in a variable and feed that to terraform instead, so I do:
sh('''
export MYVAR=["foo","bar"]
terraform apply 'var some_list=${MYVAR}'
''')
This however, does not work. ${MYVAR} is interpreted as a literal string instead of evaluating it as a variable.
I could do this:
sh('''
export MYVAR=["foo","bar"]
terraform apply var some_list=${MYVAR}
''')
In this case, ${MYVAR} is correctly intepreted, but then Terraform has trouble interpreting the parameter as a list, so the single quotes are needed.
One could perhaps use double quotes in stead , i.e. sh("""...""") and use Groovy evaluation, but I have several other variables where exactly the opposite problem happens. What I am really looking for is a way to use single quotes within single quotes and have the variable still be evaluated.
Does anyone know how I could achieve this?
EDIT:
By the way terraform apply 'var some_list=${MYVAR}', terraform apply 'var some_list=\${MYVAR}' and terraform apply \'var some_list=${MYVAR}\' all give the exact same result.
So, after more trial and error, I finally found that this will work:
sh('''
export MYVAR=["foo","bar"]
terraform apply 'var some_list='${MYVAR}''
''')
i.e. I have to close the inner single quotes before ${MYVAR} and open them again directly thereafter

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 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

Post build in Jenkins with last parameters

I would like to know if is possible to set a post build triggering the job with the last parameters. I've found this plugin https://wiki.jenkins-ci.org/display/JENKINS/Rebuild+Plugin but I can't have a properly way to call, this plugin is based on last job index.
It's a short question but I was not able to find a properly fix / workaround for that.
I think you must save this parameters in some file in workspace folder for example settings.xml or something similar, you can in easy way make command like this:
sh """echo '<name>$PARAMETER_VALUE</name>' >> ./settings.xml"""
When you would like to read this parameter you could use sed option or something else like:
sh """awk '{if($1 == "name") print $2}' ./settings.xml"""

How to specify $* in a parameter for a Jenkins job?

I have a Jenkins job that has a test.excludes parameter that I would like to default to **/*$* (ie exclude all inner classes). Normally I would specify this value in a file, but in this case, I don't want to submit any files since this is investigative work (as I see tests that are failing, I will add them to test.excludes).
The problem is that the $* in **/*$* is being expanded to the command line variables. Using **/*$$* only changes the problem to $$ being expanded to the pid. Escaping * using \ doesn't work (inner classes are still run). Escaping by wrapping the entire value in ' does nothing to prevent the $* from being replaced.
Is there a way to get the behavior I want?
The following really ugly expression works: $(echo '**/*$*').

Resources