not able to change string value in Jenkinsfile

67 views
Skip to first unread message

Somshekar

unread,
Apr 19, 2018, 4:41:34 AM4/19/18
to Jenkins Users
Hi All, 

I am trying to change the value of string dependign upon the unittest cases passed by reading a file. 
For next stage to proceed I want to set the string depending upon the value next stage qa or other stage can proceed. 
but not able to change value f string in jenkins file. 

value of res is not changing at all

also to verify I have added one more script block belwo it to verify if able to read file proerly and its value, its working fine.  below its printing failure as value is set to 1 in result file. 

not sure on this, please let me know possible solution thanks in advance

=====================
environment {
                        res = 'pass'
                        qapass  = 'fail'
                        output  = '-1'
                }

....
....

 script {
                            output = readFile 'result'
                            echo "Value of output is $output"
                            echo 'inside the script'
                            sh '''
                                awk '{ if ($1 == 0 ) { $res = "passed" } else $res = "fail" }' result
                                echo 'outside the script'
                                echo "value is $res"
                             '''
                        }
 script {
                            sh '''
                                awk '{ if ($1 == 0 ) { print "Success"; } else print "failure" }' result
                                echo 'outside the script'
                             '''
                        }

==========================

Slide

unread,
Apr 19, 2018, 9:09:31 AM4/19/18
to jenkins...@googlegroups.com
You can't set res inside a shell script and have it set in the pipeline. It seems like you are trying to use shell scripts to do things that could be done directly in the Jenkinsfile as well. Is there a reason you are using shell scripts for that?

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/8b5e5fd5-8e6f-486f-aa54-f5bc97aa1f03%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Somshekar C Kadam

unread,
Apr 19, 2018, 9:16:02 AM4/19/18
to jenkins...@googlegroups.com
Hi Slide, 

First of thanks for the response. Ok understood
 Am not finding any documentation to do the same in jjenkinsfile. 
Please can you point to it or any example will help 

regards
Somshekar

Regards
Somshekar C Kadam

To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/CAPiUgVfvD%3DhiFmw2-RcTZhAV%3D0UAfX1VGMYdG%2B4%2BNyMq5rNp4Q%40mail.gmail.com.

Slide

unread,
Apr 19, 2018, 9:22:25 AM4/19/18
to jenkins...@googlegroups.com
Well, it depends on what you are expecting to happen with these lines:

awk '{ if ($1 == 0 ) { $res = "passed" } else $res = "fail" }' result  

awk '{ if ($1 == 0 ) { print "Success"; } else print "failure" }' result 

The shell scripts don't take parameters, so what are you expecting to be in $1? What does the variable output contain? Will it contain the 0 or other exit code? If so, just use an if statement in the script {} block to check the value

script {
    ...
    output = readFile 'result'
    if(output == "0") {

    } else {

   }

}



To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/CALbGK-q3d-9vOVGcviqS6JqnKU0U669ey7Dw7irVkZNL7XSqoQ%40mail.gmail.com.

Somshekar C Kadam

unread,
Apr 19, 2018, 9:24:57 AM4/19/18
to jenkins...@googlegroups.com
Hi Slide, 

You got it right. 

result file contains either 0 or 1 as a unittest pass or fail. 
I need to read this file which contains 0 or 1 and proceed next stage.

thanks in advance 
regards
Somshekar

Regards
Somshekar C Kadam

To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/CAPiUgVfz8KEM9KS7Q2VP5RAai0%3D6KY1Dbig_1mvTs1T6wDNa1A%40mail.gmail.com.

Slide

unread,
Apr 19, 2018, 9:43:07 AM4/19/18
to jenkins...@googlegroups.com
Ok, then my suggestion is fine, after readFile, the variable output has the value, so just use an if check directly in the script block of the pipeline. script blocks are groovy scripts, so you can use most of the functionality of groovy to do what you want.

To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/CALbGK-ps-XRJaG-R7jsPC-%2BbD3nZu4NJEpkJYsyA_cCT5rgrBA%40mail.gmail.com.

Somshekar C Kadam

unread,
Apr 19, 2018, 9:51:24 AM4/19/18
to jenkins...@googlegroups.com
Hi Slide, 

Thanks for the inputs. 
I did check the value of output its returning always 1, actually it has value 0. not sure why?

one more clarification can we call awk  in script { } block ?

regards
Somshekar


Regards
Somshekar C Kadam

To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/CAPiUgVdy-67daooVFrLtk-wgtCt6YpJzCtGWREGK7ucBXcxq5Q%40mail.gmail.com.

Somshekar C Kadam

unread,
Apr 19, 2018, 9:53:02 AM4/19/18
to jenkins...@googlegroups.com
Hi Slide, 

I meant always the result file has 0, using above readfile when I compare in if cond it always says 1, I have even echoed tha value its shows 1, not sure why 

regards
Somshekar

Regards
Somshekar C Kadam

Slide

unread,
Apr 19, 2018, 10:09:59 AM4/19/18
to jenkins...@googlegroups.com
Post your full script

To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/CALbGK-q3%2BJrJFqpa6e6hAN4XYLfrnGvCbttpuR1_GXtrTjjdBQ%40mail.gmail.com.

Somshekar C Kadam

unread,
Apr 20, 2018, 1:10:40 AM4/20/18
to jenkins...@googlegroups.com
Hi Slide,

Sorry for the delay got some personal work had to step out. 
Here is the full script  for test stage 
Here verify script writes to "result" file 0 on success and 1 on failure. 
I am checking on the machine its value is 0, but still when i compare value of output as mentioned by you it is till 1
also below script block using awk gives value as 1 always. 

Any other way I need to do this. 

===============================

            stage(‘Test’) {
                agent {
                   label 'build'
                }
    environment {
res = 'pass'
        qapass  = 'fail'
        output  = '-1'
    }
                steps {
                        
sh 'cd /media/usb'
sh 'rm -rf testing'
sh 'mkdir testing'
sh 'cd testing'
        git branch: 'feature', url: 'g...@github.com:CelestialSystem/cyelp.git'
sh 'cp develop-unit/arm/ltp.sh /media/usb; chmod 777 /media/usb/ltp.sh;'
sh 'cp develop-unit/arm/verify.sh /media/usb; chmod 777 /media/usb/verify.sh;'
sh '/media/usb/ltp.sh'
sh 'cd /media/usb'
sh '/media/usb/verify.sh' 
sh 'cd /media/usb/results'
script {
    output = readFile 'result'
echo "Value of outttt is $output "
    if(output == "0") {
print "Unittest cases Passed"
    } else {

print "Unittest cases Failed"
    }
awk '{ if ($1 == 0 ) { print "Success"; } else print "failure" }' result

}

script {
    sh '''
awk '{ if ($1 == 0 ) { print "Success"; } else print "failure" }' result
    echo 'outside the script'
     '''
                }
                echo "$unitpass"
                        echo 'hey man done complete'
                    }
                }


            stage(‘Deploy’) {
                agent {
                        //echo ‘Testing..’
                   label 'build'
                }
                steps {
                        //echo ‘Deploying….’
                        echo 'hey man done complete'
                        
                        }
                    }

        }
        post {

     failure {
        mail to: 'somka...@gmail.com',
             subject: "Failed Pipeline: ${currentBuild.fullDisplayName}",
             body: "Something is wrong with ${env.BUILD_URL}"
       }
     success {
        mail to: 'somka...@gmail.com',
             subject: "Success Pipeline: ${currentBuild.fullDisplayName}",
             body: "Build Completed boy  ${env.BUILD_URL}"
       }
}   
}

===========================================================

thanks and regards
Somshekar
Post your full script

To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/CAPiUgVf%2Bj2gU6-9PKh1yTPSmGGvvSLDNsCcCGD6OHQh5eJHbtw%40mail.gmail.com.

Somshekar C Kadam

unread,
Apr 20, 2018, 4:09:58 AM4/20/18
to jenkins...@googlegroups.com
Hi Slide, 

One more observation, this result file is not in workspace. that is the reason its always outputting 1 when we print the file content. 
This could be root cause in my understanding. 
I am sorry I should have mentioned this ealrier that verify script writes this file on hardisk which is not part of the workspace. 
I will try to add this in workspace hope as you said it should work 

regards
Somshekar

Regards
Somshekar C Kadam

Slide

unread,
Apr 20, 2018, 11:30:08 AM4/20/18
to jenkins...@googlegroups.com
The other problem is that you are expecting state to be retained between invocations of sh.

                       sh 'cd /media/usb'
sh 'rm -rf testing'
sh 'mkdir testing'
sh 'cd testing'
        git branch: 'feature', url: 'g...@github.com:CelestialSystem/cyelp.git'
sh 'cp develop-unit/arm/ltp.sh /media/usb; chmod 777 /media/usb/ltp.sh;'
sh 'cp develop-unit/arm/verify.sh /media/usb; chmod 777 /media/usb/verify.sh;'
sh '/media/usb/ltp.sh'
sh 'cd /media/usb'
sh '/media/usb/verify.sh' 
sh 'cd /media/usb/results'

Each call to sh is a completely separate shell script invocation, which means that any cd you do will not be retained on the next invocation of sh. If you want to do stuff like this, you should use one shell script invocation

# clone the code into the workspace, it will be copied later
git branch: 'feature', url: 'g...@github.com:CelestialSystem/cyelp.git'

# run test procedure (it would be even better to put this into a shell script within your code base)
sh """
cd /media/usb
rm -rf testing
mkdir testing
cd testing
cp -r ${WORKSPACE}/cyelp ./
cp develop-unit/arm/ltp.sh /media/usb; chmod 777 /media/usb/ltp.sh
cp develop-unit/arm/verify.sh /media/usb; chmod 777 /media/usb/verify.sh
/media/usb/ltp.sh
cd /media/usb
/media/usb/verify.sh
cd /media/usb/results
cp results ${WORKSPACE}
"""
# now read the output file that was copied in the shell steps above
output = readFile 'results'

etc.





Post your full script

To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/CALbGK-rASCLkiROwHKAkAb-eRRqP4CictgPAjiscsrdF4PHAzA%40mail.gmail.com.

Somshekar C Kadam

unread,
Apr 25, 2018, 3:20:23 AM4/25/18
to jenkins...@googlegroups.com
Thanks Slide for the help, things are clear. 
One more doubt I have, can we not set global variable which will set that variable between different stages 

For ex consider 
once Unit test is passed 
set variable pass = true 

same variable will be used in QA test stage to start if pass = true, start QA test 

thanks 
Somshekar


Regards
Somshekar C Kadam

Post your full script

To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/CAPiUgVcM68Dy1ZXF0SoNkGmU%2BwGnyBnhbLnNk7czjSj1B3tfbA%40mail.gmail.com.

Slide

unread,
Apr 25, 2018, 7:36:57 AM4/25/18
to jenkins...@googlegroups.com
Yes, you can do that, you just can't set it from a shell script.

Post your full script

To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/CALbGK-owxV0ssqbozZMwwTGLpKo_b_0Tc_TUjuNJnE%3DDNoBj_w%40mail.gmail.com.

Somshekar C Kadam

unread,
Apr 25, 2018, 8:41:08 AM4/25/18
to jenkins...@googlegroups.com
Ok let me check, thanks for the advice 
regards
Somshekar

Regards
Somshekar C Kadam

Post your full script

To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/CAPiUgVcAWHZDY%2BcWcJvmY_z%2BFdhszQoo%3DUTzszdU02s9Up4ryQ%40mail.gmail.com.

Somshekar C Kadam

unread,
Apr 26, 2018, 5:23:09 AM4/26/18
to jenkins...@googlegroups.com
Hi Slide,

I am trying to set global variable test_result in test stage and then execute deploy stage using when directive. 
As test_result is not changing at all, deploy stage is being skipped, not sure why 
I have tried otuside shell also using script block {}
File result is aviable in workspace, and it contains 0 value saying unit test case passed. verified it. 

=========
#!/bin/groovy

def test_result = 0
def build_arm  = 0
def build_intel = 0

pipeline {
    agent none

    environment {
        unitpass = "true"
        qapass  = "fail"
        output  = '-1'
    }
    
stage(‘BuildARM’) {
                agent {
                    label 'build'
                }
                steps {
          }
}

stage(‘Test’) {
                agent {
                   label 'build'
                }

    environment {
res = 'pass'
        qapass  = 'fail'
        output  = '90'
    }
                steps {
        git branch: 'feature', url: 'g...@github.com:CelestialSystem/cyelp.git'

script {
                        echo 'Building..'
sh '${WORKSPACE}/develop-unit/arm/ltp.sh'
sh '${WORKSPACE}/develop-unit/arm/verify.sh'
}


script {
                                echo ' wait'
    sh '''
awk '{ if ($1 == 0 ) { test_result = (test_result) + 1; print "Success" } else { test_result = (test_result) + 0; print "failure"} }' result
    echo 'outside the script'
echo "Value of test result is $test_result"
     '''
echo "Value of test result is $test_result"
                }
script {
def file = "${WORKSPACE}/result"
def content = readFile(file)
echo "Value of content is $content"
if ( content == 0 ) test_result = 1 else test_result = 0
}



 stage(‘Deploy’) {
                agent {
                   label 'build'
                }

when {
  anyOf {
                    environment name: 'Branch', value: 'feature'
                    environment name: 'test_result', value: '1'
                }
             }                

==================================
           Above value of test_result is not changing at all in Stage test.
          So the Deploy stage is always skipped. Not sure why test_result is not changing
           Any pointers on this will help

Thanks in advance 
regards
Somshekar





Regards
Somshekar C Kadam

Eric Pyle

unread,
Apr 26, 2018, 10:13:45 AM4/26/18
to jenkins...@googlegroups.com, Somshekar C Kadam
You are correctly getting the result value from file ${WORKSPACE}/result in the code below, and setting it into a Groovy variable called test_result. You must understand that this Groovy variable is completely separate from the environment variable test_result which you (uselessly) set in the awk statement.


script {
def file = "${WORKSPACE}/result"
def content = readFile(file)
echo "Value of content is $content"
if ( content == 0 ) test_result = 1 else test_result = 0
}


So when you want to read that variable, you need to read a Groovy variable, not an environment variable:


    environment name: 'test_result', value: '1'

Here you are reading an environment variable "environment name: 'test_result'..."
Instead you need to read your Groovy variable:

    expression {test_result == '1'}

or even more directly:

    expression { return readFile('result') == '1' }
-- 
Eric Pyle
Siemens PLM Software
Lebanon, NH

eric...@siemens.com
http://www.siemens.com/plm

Somshekar C Kadam

unread,
Apr 27, 2018, 3:00:10 AM4/27/18
to Eric Pyle, jenkins...@googlegroups.com
Hi Eric, 

Thanks for the feedback and clarifying between environment and groovy variable. 
I did use the experession yesterday itself and able to proceed onto next stage.



Regards
Somshekar C Kadam

Reply all
Reply to author
Forward
0 new messages