echo sh(returnstdout true script 'set') returns
[Pipeline] echo
BASH=/usr/bin/sh
BASHOPTS=cmdhist:extquote:force_fignore:hostcomplete:interactive_comments:progcomp:promptvars:sourcepath
BASH_ALIASES=()
BASH_ARGC=()
BASH_ARGV=()
BASH_CMDS=()
BASH_LINENO=([0]="0")
BASH_SOURCE=([0]=/home/jenkins_slave/xxxxxx/workspace/sachin-dockerDel#tmp/durable-7dca863e/script.sh)
BASH_VERSINFO=([0]="4" [1]="2" [2]="46" [3]="2" [4]="release" [5]="x86_64-redhat-linux-gnu")
BASH_VERSION='4.2.46(2)-release'
BUILD_DISPLAY_NAME='#48'
BUILD_ID=48
BUILD_NUMBER=48
BUILD_TAG=jenkins-sachin-dockerDel-48
BUILD_URL=https://xxxxxxx/job/sachin-dockerDel/48/
CI=true
DIRSTACK=()
EUID=1003
EXECUTOR_NUMBER=1
GROUPS=()
HOME=/home/jenkins_slave
HOSTNAME=dev-compile-004
HOSTTYPE=x86_64
HUDSON_COOKIE=0b74e7c1-d3f0-433f-837f-a3d0d1a3fc5d
HUDSON_HOME=/var/jenkins_home
HUDSON_SERVER_COOKIE=584c96ce34d28299
PWD=/var/xx/xx/sdds/xsdsd/x
.
.
.
How to get the values of these params in Jenkinsfile ? want to used few params HUDSON_COOKIE
"echo ${env.HUDSON_COOKIE}" throws an error not found
"echo ${env.PWD}" return blank
In short cannot use all the variables as env.<variable Name>
Observed same with printenv .
cannot used all the variables as env.<variable Name>. YES some environment variable works like this.
new to Jenkinsfile kindly help to collect value of any variable
Related
I'm trying to use the content of a Secret File as an environment variable as follows:
stage("Terraform_plan"){
environment {
TF_VAR__private_key = credentials('SFTP_DEV_KEY')
}
sh '''
## printenv --> Can't see the TF_VAR__private_key env var.
terraform -chdir=terraform/${COMPONENT} init -var-file=./tfvars/${ENV}.${GROUP}.tfvars -upgrade -input=false
terraform -chdir=terraform/${COMPONENT} plan -var- file=./tfvars/${ENV}.${GROUP}.tfvars -out=./plan
'''
}
Getting this error from Terraform:
Error: No value for required variable
on variables.tf line 77:
77: variable "private_key" {
The root module input variable "private_key" is not set, and has no
default value. Use a -var or -var-file command line argument to provide a
value for this variable.
Also tried:
stage("Terraform_plan"){
environment {
TF_VAR_private_key = credentials('SFTP_DEV_KEY')
}
sh '''
export TF_VAR_private_key = '${env.TF_VAR_private_key}'
## printenv --> Can't see the TF_VAR__private_key env var.
terraform -chdir=terraform/${COMPONENT} init -var-file=./tfvars/${ENV}.${GROUP}.tfvars -upgrade -input=false
terraform -chdir=terraform/${COMPONENT} plan -var- file=./tfvars/${ENV}.${GROUP}.tfvars -out=./plan
'''
}
This works as Terraform goes to the Apply stage but still when applying I'm still getting an error that there's no value for the env var.
printenv shows TF_VAR_private_key=${env.TF_VAR_private_key} and not the value of TF_VAR_private_key.
However, if I try to export the env var with export TF_VAR_private_key it works inside shell.
Shouldn't the env var already be set inside the shell?
Thanks in advance,
I have an environment var with default value
RUN_TESTS= true
My Jenkins job contains two stages, build and test stage.
The test stage modified to run 2 kind of tests sets depended on RUN_TESTS value.
RUN_TESTS value is set on build stage.
Is there a way to update value for environment variable for future use ?
Here what I tried without success
env.RUN_TESTS= true
node("node1"){
sh '''
printenv
RUN_TESTS="false"
'''
}
print RUN_TESTS
I am getting
+ RUN_TESTS=false
[Pipeline] }
[Pipeline] // node
[Pipeline] echo
true
[Pipeline] End of Pipeline
You can define your variable at the top of the jenkinsfile
environment {
RUN_TESTS=false
}
Then change it like this:
RUN_TESTS = true
with no "" IF you are assigning this directly from the jenkinsfile. However, in your case, you are using it within and sh command, so you might have to use something like
${RUN_TESTS} = true
Although I haven't been able to test it so I can't give a guarantee that it will work.
When you issue a sh directive, a new instance of shell (most likely bash) is created. As usual Unix processes, it inherits the environment variables of the parent. Notably, this includes RUN_TESTS variable and its value. So a copy of Jenkins environment variable RUN_TESTS is created for sh directive.
Your bash instance is then running your script. When your script sets or updates an environment variable, the environment of bash is updated. Once your script ends, the bash process that ran the script is destroyed, and all its environment is destroyed with it. Notably, this includes your updated RUN_TESTS variable and its value. This has no influence on the Jenkins environment variable RUN_TESTS that had its value copied before.
Here's a Python example demonstrating the same concept:
>>> def foo(i):
... print(i)
... i += 1
... print(i)
...
>>> i = 6
>>> i
6
>>> foo(i)
6
7
>>> i
6
The variable i internal to function foo was first initialized to 6, then incremented to 7, then destroyed. The outer variable bearing the same name i was copied for the invocation of foo but wasn't changed as a result.
Use it like this
String RUN_TESTS = "true"
node("node1"){
sh '''
printenv
RUN_TESTS = "false"
'''
}
print RUN_TESTS
I have a config file that has some environment variable including a variable called MONGO_UUID and I pass this variable to a test step via a configFileProvider plugin and save it to .env file as in step below:
stage('Build ') {
steps {
configFileProvider([configFile(fileId: 'jenkins_config_filename', variable: "CONFIG_FILE")]) {
sh '''
cp -f $CONFIG_FILE ./.env
npm run test // this step dynamically updates MONGO_UUID
echo "$MONGO_UUID" // trying to output newly updated value here
'''
jenkins_config_filename has following format:
MONGO_HOST=123.123.123.1
MONGO_DB=dbname
MONGO_US=user
MONGO_UUID=null
What I am trying to do is to output variable MONGO_UUID which was passed to the test step via config file and the value of MONGO_UUID get updated in the process. I can see that the MONGO_UUID got updated because I can see new record in mongodb but I am wondering how to echo that value in jenkins console.
add this line (with proper changes based on your MongoDB configuration) after npm run test:
MONGO_UUID=`mongo db.collection.find("whatever you query is")`
and then you can echo the variable to Jenkins output.
If you want to update the file, you can use sed like below :
sed -i "s/MONGO_UUID=.*/MONGO_UUID=${MONGO_UUID}/" .env
My scenario is, I have parameterized build and inside the build section, I have executed shell where I define a variable and then echo to print it. But it doesn't print anything in the console output.
I hope I have made myself clear. Could anyone please answer my question?
current_folder=`date +%Y%m%d-%H%M%S`
echo $current_folder
enter image description here
I'm using Jenkins ver. 2.32.3 and a simple freestyle job, running on mac OS, using an execute shell build step of:
current_folder=`date +%Y%m%d-%H%M%S`
echo $current_folder
Gives output of:
$ /bin/sh -xe /var/folders/kh/4fl0eeldofefmmsfd/T/hudson89388543547899686.sh
++ date +%Y%m%d-%H%M%S
+ current_folder=20180613-081712
+ echo 20180613-081712
20180613-081712
Finished: SUCCESS
In a similar fashion, setting the shell:
#!/bin/bash
current_folder=`date +%Y%m%d-%H%M%S`
echo $current_folder
Gives:
$ /bin/bash /var/folders/kh/by0kd93dfew5fgjhy000h6/T/hudson62702345565786787.sh
20180613-081655
Finished: SUCCESS
The same applies to a parameter that is defined as part of the Jenkins job, underneath the
This project is parameterized checkbox once set. For example, if you have a string parameter called userName with a default value of User1, then you can print it's value in an Execute Shell build step using:
echo $userName
echo ${userName}
echo "In a string ${userName}"
Giving:
User1
User1
In a string User1
Given the following pipeline:
stages {
stage ("Checkout SCM") {
steps {
checkout scm
sh "echo ${CHANGE_AUTHOR_EMAIL}"
sh "echo ${CHANGE_ID}"
}
}
}
Why do these variables fail to resolve and provide a value?
Eventually I want to use these environment variables to send an email and merge a pull request:
post {
failure {
emailext (
attachLog: true,
subject: '[Jenkins] $PROJECT_NAME :: Build #$BUILD_NUMBER :: build failure',
to: '$CHANGE_AUTHOR_EMAIL',
replyTo: 'iadar#...',
body: '''<p>You are receiving this email because your pull request was involved in a failed build. Check the attached log file, or the console output at: $BUILD_URL to view the build results.</p>'''
)
}
}
and
sh "curl -X PUT -d '{\'commit_title\': \'Merge pull request\'}' <git url>/pulls/${CHANGE_ID}/merge?access_token=<token>"
Oddly enough, $PROJECT_NAME, $BUILD_NUMBER, $BUILD_URL do work...
Update: this may be an open bug... https://issues.jenkins-ci.org/browse/JENKINS-40486 :-(
Is there any workaround to get these values?
You need to be careful about how you refer to environment variables depending on whether it is shell or Groovy code, and how you are quoting.
When you do sh "echo ${CHANGE_ID}", what actually happens is that Groovy will interpolate the string first, by replacing ${CHANGE_ID} with the Groovy property CHANGE_ID, and that's where your error message is from. In Groovy, the environment variables are wrapped in env.
If you want to refer to the environment variables directly from your shell script, you either have to interpolate with env, use single quotes, or escape the dollar sign. All of the following should work:
sh 'echo $CHANGE_ID'
sh "echo \$CHANGE_ID"
sh "echo ${env.CHANGE_ID}"
For anyone who may come across this, these variables are available only if the checkbox for Build origin PRs (merged with base branch) was checked (this is in a multi-branch job).
See more in this other Jenkins issue: https://issues.jenkins-ci.org/browse/JENKINS-39838