coding_idiot
coding_idiot

Reputation: 13734

Mix and match shell variables with groovy variables in Jenkinsfile

I've tried various variations of the below code, none of them seems to work

def runScript(command){
    sh '''#!/bin/bash
        file="env.txt"
        while IFS='=' read -r key value
        do
            export ${key}="${value}"
        done < "$file"
        pwd
        "${command}"
    '''
}

command is the dynamic shell command that I want to execute after re-creating the environment variables from env.txt

Any idea ?

Upvotes: 2

Views: 6187

Answers (1)

coding_idiot
coding_idiot

Reputation: 13734

Use this

   def runScript(command){
        sh '''#!/bin/bash
            file="env.txt"
            while IFS='=' read -r key value
            do
                export ${key}="${value}"
            done < "$file"
            pwd
        ''' + "${command}"
    }

The single quotes ''' will create multi-line shell script. The " quotes will get the value of command variable and concatenate it with shell script. Note that there are two shell variables key & value & hence, ''' can't be replaced with """

References :

Access a Groovy variable from within shell step in Jenkins pipeline

How to pass variables from Jenkinsfile to shell command

Upvotes: 3

Related Questions