Passing Variable to Bash Script in a Jenkins Pipeline Job

To pass variables set in jenkins pipeline to shell script

If you want to interpolate $MY_VAR when executing sh step, you need to replace single quotes with the double-quotes.

def MY_VAR
def BUILD_NUMBER
pipeline {
agent any
stages {
stage('Stage One') {
steps {
script {
BUILD_NUMBER={currentBuild.number}
MY_VAR ='abc'
}
}
}
stage('Stage Two') {
steps {
sh """
cd /scripts/
./my_scripts.sh $BUILD_NUMBER $MY_VAR"""
}
}
}
}

The $BUILD_NUMBER worked, because pipeline exposes env.BUILD_NUMBER and this variable can be accessed inside the shell step as bash's $BUILD_NUMBER env variable. Alternatively, you could set MY_VAR as an environment variable and keep single quotes in the sh step. Something like this should do the trick:

pipeline {
agent any
stages {
stage('Stage One') {
steps {
script {
//you can remove BUILD_NUMBER assignment - env.BUILD_NUMBER is already created.
//BUILD_NUMBER={currentBuild.number}
env.MY_VAR ='abc'
}
}
}
stage('Stage Two') {
steps {
sh '''
cd /scripts/
./my_scripts.sh $BUILD_NUMBER $MY_VAR'''
}
}
}
}

You can learn more about Jenkins Pipeline environment variables from one of my blog posts.

Use a bash variable in jenkins build job

I will answer me.

When i use sh directive, a new instance of shell (most likely bash) is created. As usual Unix processes, it inherits the environment variables of the parent. When my script sets 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.

     def script_output = sh(returnStdout: true, script: '''
#!/bin/bash
nombre=$(cat /home/user/nombre.txt)
echo $nombre
''')
script_output = script_output.trim()
VAR_NAME = script_output
//USING VARIABLE IN 'bash enviroment'
echo "VAR_NAME is ${VAR_NAME}"
//USING VARIABLE IN 'jenkins enviroment'
echo VAR_NAME

I dont know if its a optimal way to do it. Let me know if someone have another way.

Pass variable from one Jenkins stage to others in sh

You can output the variables to same file in stage, then read back and assign to env at the beginning of each stage as following:

stage('a') {
steps {
script {
loadEnvs('env.props')
}
sh """
echo var1 = a >> env.props
"""
}
}

stage('b') {
steps {
script {
loadEnvs('env.props')
}
sh """
echo $env.var1

echo var2 = b >> env.props
"""
}
}

def loadEnvs(propFile) {
props = readProperties(propFile)
props.each { key, val ->
env[key] = val
}
}

Jenkins: pass a variable between shell scripts and stages

Environment variables that are assigned to in shell scripts don't persist outside of the shell session. You have to get the stdout from the sh step and assign it to a Groovy variable like this:

@groovy.transform.Field String SERVER = null

def fetch_server() {
SERVER = sh( returnStdout: true,
script: "${env.LOCAL_SCRIPT_PATH}/getServer.sh ${params.HOSTNAME} ${TOKEN}" ).trim()
}

Note the use @groovy.transform.Field annotation which is required to make the variable accessible from functions (otherwise it will actually be a local variable of the implicit run method of the script class).

Groovy: Jenkinsfile: Unable to pass argument with space to shell script in jenkins pipeline

https://community.synopsys.com/s/document-item?bundleId=integrations-detect&topicId=scripts%2Fscript-escaping-special-characters.html&_LANG=enus

To have literal detect.sh --detect.project.name=\"Project Test\"

You have to escape each backslash with another one in groovy string

sh 'detect.sh --detect.project.name=\\"Project Test\\"'

How to pass variables from Jenkinsfile to shell command

Your code is using a literal string and therefore your Jenkins variable will not be interpolated inside the shell command. You need to use " to interpolate your variable inside your strings inside the sh. ' will just pass a literal string. So we need to make a few changes here.

The first is to change the ' to ":

for (i in [ 'a', 'b', 'c' ]) {
echo i
sh "echo "from shell i=$i""
}

However, now we need to escape the " on the inside:

for (i in [ 'a', 'b', 'c' ]) {
echo i
sh "echo \"from shell i=$i\""
}

Additionally, if a variable is being appended directly to a string like you are doing above ($i onto i=), we need to close it off with some curly braces:

for (i in [ 'a', 'b', 'c' ]) {
echo i
sh "echo \"from shell i=${i}\""
}

That will get you the behavior you desire.



Related Topics



Leave a reply



Submit