Richlewis Richlewis - 1 month ago 25
Bash Question

Using variables in Groovy and Jenkins pipeline build

I have a Jenkins pipeline build that needs to obtain the commit id (sha), so from what i can see the only way to get this is by doing the following

sh "git rev-parse --long HEAD > .git/commit-id"
GIT_COMMIT = readFile('.git/commit-id')

I need to use the GIT_COMMIT var within a curl command but am having trouble using it

sh '''curl "${GIT_COMMIT}"}"'''
sh '''curl ""}"'''

At the moment GIT_COMMIT is not being passed through, it is always blank, but yet if i echo it i get the value back


Can anyone advise on how i achieve what i require please



I have tried the following but still no luck

sh '''GIT_COMMIT=$(git rev-parse HEAD)'''
sh '''curl "$GIT_COMMIT"'''

Fo. Fo.

I don't know if you need to break up your shell commands into individual statements, but if so then you don't need to surround single line scripts with triple quotes, single will do.

You can do what you need in a multiline without outputting to a file, this way:

sh """
    GIT_COMMIT=`git rev-parse HEAD`

If you need single statements then you can use the file method you started with, like this:

sh "echo `git rev-parse HEAD` > file.txt"

GIT_COMMIT = readFile('./file.txt');

sh "curl$GIT_COMMIT"