Scripted Pipeline - Declare a variable and modify it inside a docker.image directive

11 views
Skip to first unread message

Roberto Fabrizi

unread,
Apr 11, 2018, 9:27:31 AM4/11/18
to Jenkins Users
Dear Friends,
I'm trying to accomplish this with my pipeline:
  • test a part of my newly produced artifact against some previous artifacts inside a docker base image
  • if they are the same, launch a docker build using dockerfileA, otherwise using dockerfileB
I've arrived at this point:

                        docker.image("mybaseimage").inside {
                                try {
                                        ...
                                        sh 'find tmp_unzip_path/dependencies -type f -exec md5sum {} + | sort -k 2 | awk {\'print $1\'} > dir1.txt'
                                        sh 'find /opt/docker/lib -type f -exec md5sum {} + | sort -k 2 | awk {\'print $1\'} > dir2.txt'
                                        //sh 'printenv'
                                        sh '''if [ $(echo $?) -eq 0 ]; then
                                                echo "No changes to the dependencies!"
                                        else
                                                echo "Dependencies must be updated!"

What I'd like to do is assign the result of if statement to a variable, so I can use it to make another choise later on.
I've tried variables, environment variables, but neither is working. This is one try for example:

withEnv(["DEP_UNCHANGED=0"]) {
...
same code as before
sh '''if [ $(echo $?) -eq 0 ]; then
     DEP_UNCHANGED=0
else 
    DEP_UNCHANGED=1
...
}

This says that DEP_UNCHANGED is not defined. Ironically enough if I do this:

withEnv(["DEP_UNCHANGED=0"]) {
...
same code as before
sh '''if [ $(echo $?) -eq 0 ]; then
     $DEP_UNCHANGED=0
else 
    $DEP_UNCHANGED=1
...
}

it complains that 0 = 0, so it IS defined I guess....
printenv also does print DEP_UNCHANGED=0, so it IS defined?

What is the proper way to accomplish this?
Thank you so much,
Best regards,
Roberto
Reply all
Reply to author
Forward
0 new messages