Using environment variables in Jenkins - shell

Following is a screenshot of how I am setting an environment variable in Jenkins.
I am trying to access it in a shell script, but it is not echoing its value.
echo "Starting ..."
echo ${BUILD_NUMBER}
echo ${DATABASE}
${BUILD_NUMBER} is a global env variable.

The correct use of variables in Jenkins is the following:
echo "The current build number is: ${BUILD_NUMBER}"
The important part is to use ". If you would use ' Jenkins would not notice that you want to access the variable and simply print your variable as string.
Source: Jenkins String Interpolation

Related

How do I check that a single variable has multiple parameters populated

In a Dockerfile I have
ENV JAVA_OPTS="-DmyKeyStore=${blah1} -DmyApi=${blah2} -Dsalt=${blah3}"
The ${blah} variables are populated during our CI/CD run but I want the Docker build to fail if one of the parameters fails to get populated.
I can use the below code to check if ENV JAVA_OPTS as a whole isn't populated.
RUN if [ -z "$JAVA_OPTS" ]; then echo 'Environment variable JAVA_OPTS must be specified. Exiting.'; exit 1; fi
but I want to do a deeper check within that variable and fail if -DmyKeyStore=blank/null for example.
Are you sure docker uses bash, and not something internal? If it uses bash, then you could always use the ${param:?message} parameter expansion to generate an error.
Something like this:
JAVA_OPTS="-DmyKeyStore=${blah1:?} -DmyApi=${blah2:?} -Dsalt=${blah3:?}"
You can test it in an interactive shell like this:
$ : ${t:?}
bash: t: parameter null or not set
$ t=1
$ : ${t:?}

Jenkins shell fetching secret txt

I have a secret txt and want to pass the Variables in the Jenkins Shell script (Not the Pipeline)
Need help on this
withCredentials([string(credentialsId: 'API_TOKEN_ID', variable: 'TOKEN_VALUE')])
the above works in shell script inside the pipeline, but want to execute this in "Execute Shell" mode in Jenkins.
You may try this : https://plugins.jenkins.io/credentials-binding/
If it’s a token you can put it as an env variables

Jenkins Pipeline Step withEnv does not work without BASH

I've trouble setting an environment variable for a container in an Jenkins pipeline.
It seems, that "withEnv" does not work nicely with machines without bash.
Can you confirm that? I cannot find an official statement ;-)
When I run the following snippet on the Jenkins slave it works.
But when it is executed in a docker container without BASH "$test" isn't set.
withEnv(["test='asd'"]){
sh 'echo $test'
}
https://jenkins.io/doc/pipeline/steps/workflow-basic-steps/#code-withenv-code-set-environment-variables
If I'm not mistaken, I believe the variable is not set correctly.
Try this:
withEnv(["test=asd"]){
sh "echo \$test"
}
Within a Jenkins pipeline:
$var = Groovy parameter
\$var (within a sh closure) = Bash parameter
${var} = also refers to Groovy parameter
In order to insert a groovy variable into a bash variable:
sh ("VAR=${GROOVY_VAR}")
Using a bash variable inside a sh closure:
sh (" echo \$BASH_VAR")
We have to use single quote when using withEnv in Jenkins.
withEnv(['test=asd']){
sh "echo \$test"
}
Because, the variable expansion is being done by the Bourne shell, not Jenkins.
(Quoting from documentation)
Find more info here: https://jenkins.io/doc/pipeline/steps/workflow-basic-steps/

Jenkins - Passing variable password to external shell

I am using the BUILD STEP "Execute shell script on remote host" and I'm injecting a password to my project:
The jenkins call a script.sh, but the script does not print variable PASS passed by jenkins.
As a step variable issued by Jenkins to my external script?
PASS=${PASSWORD}
echo PASSWORD=$PASS
sh /root/script.sh
You need to export your variable in order to make it available to subshells:
export PASS=${PASSWORD}
If you don't want other programs you invoke in the same script to see your password, consider this safer way:
PASS=${PASSWORD} /root/script.sh

How to set env variables which contain a command using EnvInject plugin of Jenkins

I have installed the EnvInject plugin of Jenkins.
I add it in the properties content (In script content also doesn't work: echo's nothing)
I able to set environment variable e.g.:
TEST="hello world"
In shell:
echo ${TEST}
Output: Hello World
But when I try to put the output of a command in my variable it doesn't work:
HOSTNAME=`hostname`
In Shell
echo ${HOSTNAME}
Output: `hostname`
While when I set the environment variable in my shell (without the plugin it works):
In Shell
HOSTNAME=`hostname`
echo ${HOSTNAME}
Output: localhost
From job configuration you should use Inject environment variables to the build process / Evaluated Groovy script.
Depending on the configuration you could execute command and save it in map containing environment variables:
return [HOSTNAME: 'hostname'.execute().text]
or run Groovy equivalent:
return [HOSTNAME: java.net.InetAddress.getLocalHost().getHostName()]

Resources