How to pass variables from Jenkinsfile to shell command


Joe:

I would like to use a variable inside a Jenkinsfilescript and then pass its value to shell script execution (either as an environment variable or command line argument).

But the following Jenkinsfile:

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

gives the output:

a
from shell i=
b
from shell i=
c
from shell i=

The desired output is similar to:

a
from shell i=a
b
from shell i=b
c
from shell i=c

Any idea how to pass the value ito shell scipt?

EDIT: Based on Matt's answer, I now use this solution:

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

The advantage is that I don't need escaping "in the shell script .

Matt Schuchard:

Your code uses literal strings, so your Jenkins variables will not be interpolated in shell commands. You need to use "to interpolate variables in the string sh. 'Only literal strings will be passed. So we need to make some changes here.

The first is 'to change to ":

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

However, now we need to escape "internally :

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

Also, if we append the variable directly to the string like we did above ( $iabove i=) , we need to close it with curly braces:

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

This will get you the desired behavior.

Related


How to pass variables from Jenkinsfile to shell command

Joe: I would like to use a variable inside a Jenkinsfilescript and then pass its value to shell script execution (either as an environment variable or command line argument). But the following Jenkinsfile: for (i in [ 'a', 'b', 'c' ]) { echo i sh 'echo

Pass variables extracted from shell in Jenkinsfile

devops84uk I'm trying to pass a variable extracted in a stage in a Jenkinsfile between stages. E.g: stage('Dummy Stage') { sh '''#!/bin/bash -l export abc=`output of some command` ..... ..... ''' Now, how do I pass the va

Pass variables extracted from shell in Jenkinsfile

devops84uk I'm trying to pass a variable extracted in a stage in a Jenkinsfile between stages. E.g: stage('Dummy Stage') { sh '''#!/bin/bash -l export abc=`output of some command` ..... ..... ''' Now, how do I pass the va

Pass variables extracted from shell in Jenkinsfile

devops84uk I'm trying to pass a variable extracted in a stage in a Jenkinsfile between stages. E.g: stage('Dummy Stage') { sh '''#!/bin/bash -l export abc=`output of some command` ..... ..... ''' Now, how do I pass the va

Pass variables extracted from shell in Jenkinsfile

devops84uk I'm trying to pass a variable extracted in a stage in a Jenkinsfile between stages. E.g: stage('Dummy Stage') { sh '''#!/bin/bash -l export abc=`output of some command` ..... ..... ''' Now, how do I pass the va

Pass variables extracted from shell in Jenkinsfile

devops84uk I'm trying to pass a variable extracted in a stage in a Jenkinsfile between stages. E.g: stage('Dummy Stage') { sh '''#!/bin/bash -l export abc=`output of some command` ..... ..... ''' Now, how do I pass the va

Pass variables extracted from shell in Jenkinsfile

devops84uk I'm trying to pass a variable extracted in a stage in a Jenkinsfile between stages. E.g: stage('Dummy Stage') { sh '''#!/bin/bash -l export abc=`output of some command` ..... ..... ''' Now, how do I pass the va

Pass variables extracted from shell in Jenkinsfile

devops84uk I'm trying to pass a variable extracted in a stage in a Jenkinsfile between stages. E.g: stage('Dummy Stage') { sh '''#!/bin/bash -l export abc=`output of some command` ..... ..... ''' Now, how do I pass the va

How to pass variables from awk to shell command?

Vahid Mirjalili I'm trying to run a shell command from awk for each line of a file that requires an input parameter. I tried to use system()but the input parameter is not recognized. Each line of this file is the address of a file that I want to run a command

How to pass variables from awk to shell command?

Vahid Mirjalili I'm trying to run a shell command from awk for each line of a file that requires an input parameter. I tried to use system()but the input parameter is not recognized. Each line of this file is the address of a file that I want to run a command

Execute shell command from jenkinsfile

Hardik Lasode I am trying to execute a set of commands from a jenkinsfile. The problem is when I try to assign the value of stdout to a variable it doesn't work. I've tried different combinations of double and single quotes, but no luck so far. Here I executed

Execute shell command from jenkinsfile

Hardik Lasode I am trying to execute a set of commands from a jenkinsfile. The problem is when I try to assign the value of stdout to a variable it doesn't work. I've tried different combinations of double and single quotes, but no luck so far. Here I executed

How to pass variables from shell script to .muttrc?

Amanda I want mutt( mutt-kz, actually - I'd love to migrate to, neomuttbut it's a bigger project) to get the SMTP password passso I don't have to store it in the config file in clear text, but I'm not sure how to correctly The password is passed to mine .muttr

How to pass variables from shell script to .muttrc?

Amanda I want mutt( mutt-kz, actually - I'd love to migrate to, neomuttbut it's a bigger project) to get the SMTP password passso I don't have to store it in the config file in clear text, but I'm not sure how to correctly The password is passed to mine .muttr

How to use pom version in shell command in jenkinsfile?

username I'm trying to get the pom.xml version in a Jenkinsfile and then use that version in a shell script. An exception occurs bad substitutionwhen trying to use it pom.version. What is the correct syntax to pass the pom version? script { def pom = readM

How to use pom version in shell command in jenkinsfile?

username I'm trying to get the pom.xml version in a Jenkinsfile and then use that version in a shell script. An exception occurs bad substitutionwhen trying to use it pom.version. What is the correct syntax to pass the pom version? script { def pom = readM

How to use pom version in shell command in jenkinsfile?

username I'm trying to get the pom.xml version in a Jenkinsfile and then use that version in a shell script. An exception occurs bad substitutionwhen trying to use it pom.version. What is the correct syntax to pass the pom version? script { def pom = readM

How to use pom version in shell command in jenkinsfile?

username I'm trying to get the pom.xml version in a Jenkinsfile and then use that version in a shell script. An exception occurs bad substitutionwhen trying to use it pom.version. What is the correct syntax to pass the pom version? script { def pom = readM

How to use pom version in shell command in jenkinsfile?

username I'm trying to get the pom.xml version in a Jenkinsfile and then use that version in a shell script. An exception occurs bad substitutionwhen trying to use it pom.version. What is the correct syntax to pass the pom version? script { def pom = readM

How to use pom version in shell command in jenkinsfile?

username I'm trying to get the pom.xml version in a Jenkinsfile and then use that version in a shell script. An exception occurs bad substitutionwhen trying to use it pom.version. What is the correct syntax to pass the pom version? script { def pom = readM

How to use pom version in shell command in jenkinsfile?

username I'm trying to get the pom.xml version in a Jenkinsfile and then use that version in a shell script. An exception occurs bad substitutionwhen trying to use it pom.version. What is the correct syntax to pass the pom version? script { def pom = readM

How to pass variables from blade to LARAVEL command

carmine rub I need to pass user_id from blade to route and then use that variable for Laravel command. What can I do? job_list.blade.php <div class="box-tools"> <a href="/stampasingoloreport/{{$utente->id}}" class="btn btn-box-tool" role="button"><i class

How to access variables from sh script in Jenkinsfile

Eric Way The code below is a very simplified version of my code, just for better explanation: def String FOLDER_NAME = "TestFolder" def createFolder() { sh(''' ls mkdir ${FOLDER_NAME} ls ''') } I want to create a new folder wi