I want to use some common value across different conditions in post section of pipeline hence I tried following -
1.
post {
script {
def variable = "<some dynamic value here>"
}
failure{
script{
"<use variable here>"
}
}
success{
script{
"<use variable here>"
}
}
}
2
post {
def variable = "<some dynamic value here>"
failure{
script{
"<use variable here>"
}
}
success{
script{
"<use variable here>"
}
}
}
But it results into compilation error.
Can you please suggest how I can declare a variable in post section which can be used across conditions?
You could use always condition which is guaranteed to be executed before any other conditions like success or failure. If you want to store String value, you can use use env variable to store it (environment variable always casts given value to a string). Alternatively, you can define a global variable outside the pipeline and then initialize it with the expected dynamic value inside the always condition. Consider the following example:
def someGlobalVar
pipeline {
agent any
stages {
stage("Test") {
steps {
echo "Test"
}
}
}
post {
always {
script {
env.FOO = "bar"
someGlobalVar = 23
}
}
success {
echo "FOO is ${env.FOO}"
echo "someGlobalVar = ${someGlobalVar}"
}
}
}
Output:
Running on Jenkins in /home/wololock/.jenkins/workspace/pipeline-post-sections
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Test)
[Pipeline] echo
Test
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Declarative: Post Actions)
[Pipeline] script
[Pipeline] {
[Pipeline] }
[Pipeline] // script
[Pipeline] echo
FOO is bar
[Pipeline] echo
someGlobalVar = 23
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
Related
I'm new to Groovy. I'm not able to figure out what's wrong here.
Depends on the choice of input, I expect the script to execute either Step 'Hello' or 'Bye' but it skips both. I mostly orientated to this Jenkins pipeline conditional stage using "When" for choice parameters, but still can't figure it out.
How can I use those choice parameters correctly?
pipeline {
agent any
stages {
stage('Init') {
steps('Log-in'){
echo 'Log-in'
}
}
stage('Manual Step') {
input {
message "Hello or Goodbye?"
ok "Say!"
parameters{choice(choices:['Hello','Bye'], description: 'Users Choice', name: 'CHOICE')}
}
steps('Input'){
echo "choice: ${CHOICE}"
echo "choice params.: " + params.CHOICE //null
echo "choice env: " + env.CHOICE //Hello
}
}
stage('Hello') {
when{ expression {env.CHOICE == 'Hello'}}
steps('Execute'){
echo 'Say Hello'
}
}
stage('Bye') {
when{ expression {env.CHOICE == 'Bye'}}
steps('Execute'){
echo 'Say Bye'
}
}
}
}
Output:
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Init)
[Pipeline] echo
Log-in
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Manual Step)
[Pipeline] input
Input requested
Approved by Admin
[Pipeline] withEnv
[Pipeline] {
[Pipeline] echo
choice: Hello
[Pipeline] echo
choice params.: null
[Pipeline] echo
choice env: Hello
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Hello)
Stage "Hello" skipped due to when conditional
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Bye)
Stage "Bye" skipped due to when conditional
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
From the docs:
Any parameters provided as part of the input submission will be available in the environment for the rest of the stage.
This means that your parameter CHOICE does not exist in the other stages. If you want to have a parameter that's available on all the stages, you can define a parameter outside of the stage, i.e.:
pipeline {
agent any
parameters {
choice(choices:['Hello','Bye'], description: 'Users Choice', name: 'CHOICE')
}
stages {
stage('Init') {
steps('Log-in') {
echo 'Log-in'
}
}
stage('Manual Step') {
steps('Input') {
echo "choice: ${CHOICE}"
echo "choice params.: " + params.CHOICE
echo "choice env: " + env.CHOICE
}
}
stage('Hello') {
when {
expression { env.CHOICE == 'Hello' }
}
steps('Execute') {
echo 'Say Hello'
}
}
stage('Bye') {
when {
expression {env.CHOICE == 'Bye'}
}
steps('Execute'){
echo 'Say Bye'
}
}
}
}
This will behave as expected. The difference is that the job won't ask you for input, instead, you will provide the wanted parameters before pressing build.
(edited/updated from original post to attempt to address confusion about what the problem is)
The problem is: Values that are set in a Jenkinsfile environment section are not added to the object returned by env.getEnvironment()
The question is: How do I get a map of the complete environment, including values that were assigned in the environment section? Because env.getEnvironment() doesn't do that.
Example Jenkinsfile:
pipeline {
agent any
environment {
// this is not included in env.getEnvironment()
ONE = '1'
}
stages {
stage('Init') {
steps {
script {
// this is included in env.getEnvironment()
env['TWO'] = '2'
}
}
}
stage('Test') {
steps {
script {
// get env values as a map (for passing to groovy methods)
def envObject = env.getEnvironment()
// see what env.getEnvironment() looks like
// notice ONE is not present in the output, but TWO is
// ONE is set using ONE = '1' in the environment section above
// TWO is set using env['TWO'] = '2' in the Init stage above
println envObject.toString()
// for good measure loop through the env.getEnvironment() map
// and print any value(s) named ONE or TWO
// only TWO: 2 is output
envObject.each { k,v ->
if (k == 'ONE' || k == 'TWO') {
println "${k}: ${v}"
}
}
// now show that both ONE and TWO are indeed in the environment
// by shelling out and using the env linux command
// this outputs ONE=1 and TWO=2
sh 'env | grep -E "ONE|TWO"'
}
}
}
}
}
Output (output of envObject.toString() shortened to ... except relevant part):
[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Init)
[Pipeline] script
[Pipeline] {
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Test)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
[..., TWO:2]
[Pipeline] echo
TWO: 2
[Pipeline] sh
+ env
+ grep -E ONE|TWO
ONE=1
TWO=2
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Notice ONE is missing from the env.getEnvironment() object, but TWO is present.
Also notice that both ONE and TWO are set in the actual environment and I am not asking how to access the environment or how to iterate through the values returned by env.getEnvironment(). The issue is that env.getEnvironment() does not return all the values in the environment, it excludes any values that were set inside the environment section of the Jenkinsfile.
I don't have a "why" answer for you, but you can cheat and get a map by parsing the output from env via the readProperties step.
def envMap = readProperties(text: sh(script: 'env', returnStdout: true))
println(envMap.getClass())
println("${envMap}")
I would get the env and convert it to map with the help of properties
pipeline {
agent any
environment {
// this is not included in env.getEnvironment()
ONE = '1'
}
stages {
stage('Init') {
steps {
script {
// this is included in env.getEnvironment()
env['TWO'] = '2'
}
}
}
stage('Test') {
steps {
script {
def envProp = readProperties text: sh (script: "env", returnStdout: true).trim()
Map envMapFromProp = envProp as Map
echo "ONE=${envMapFromProp.ONE}\nTWO=${envMapFromProp.TWO}"
// now show that both ONE and TWO are indeed in the environment
// by shelling out and using the env linux command
// this outputs ONE=1 and TWO=2
sh 'env | grep -E "ONE|TWO"'
}
}
}
}
}
Output of env.getEnvironment() method will not return a list or Map, Hence it's difficult to iterate with each but there are some workaround you can do to make this work.
import groovy.json.JsonSlurper
pipeline {
agent any;
environment {
ONE = 1
TWO = 2
}
stages {
stage('debug') {
steps {
script {
def jsonSlurper = new JsonSlurper()
def object = jsonSlurper.parseText(env.getEnvironment().toString())
assert object instanceof Map
object.each { k,v ->
echo "Key: ${k}, Value: ${v}"
}
}
}
}
}
}
Note - env.getEnvironment().toString() will give you a JSON String . While parsing the JOSN string if groovy jsonSlurper.parseText found any special character it will through an error
You can also explore a little bit around env Jenkins API and find an appropriate method that will either return a Map or List so that you can use each
I have trouble accessing nested JSON with readJSON
oldJson string:
{"branch":{"type-0.2":{"version":"0.2","rc":"1","rel":"1","extras":"1"}}}
I try to access it as in example
https://jenkins.io/doc/pipeline/steps/pipeline-utility-steps/#readjson-read-json-from-files-in-the-workspace
assert oldJson["rc"] == '1'
but it fails. I think it because "rc" is nested in "type-02". How could I access it?
You can always get the value of a nested element by its nested key using bracket notation or dot notation.
stage('Read-JSON') {
steps {
script {
def oldJson = '{"branch":{"type-0.2":{"version":"0.2","rc":"1","rel":"1","extras":"1"}}}'
def props = readJSON text: oldJson
println(props['branch']['type-0.2']['rc'])
\\ or println(props.'branch'.'type-0.2'.'rc')
}
}
}
Output:
[Pipeline] stage
[Pipeline] { (Read-JSON)
[Pipeline] script
[Pipeline] {
[Pipeline] readJSON
[Pipeline] echo
1
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
I'm trying to pass in a commit messages concatenated string to a shell script via a Jenkins declarative pipeline. I can get the concatenated string, but I cannot figure out how to pass it to my shell script. Environment variables are readable in my shell script, but I cannot set the Environment variable outside of my stages, as the stage is where I define my git connection, and if I set it in the stage it does not update the Environment variable that I call in my post section. How can I pass the value of changeString to my bash script (in success)?
pipeline {
agent any
environment {
CHANGE_STRING = 'Initial default value.'
}
stages {
stage('Build') {
environment {
CHANGE_STRING = 'This change is only available in this stage and not in my shell script'
}
steps {
echo 'Build stage'
git branch: 'develop',
credentialsId: 'blah',
url: 'blah.git'
sh """
npm install
"""
script{
MAX_MSG_LEN = 100
def changeString = ""
def changeLogSets = currentBuild.changeSets
for (int i = 0; i < changeLogSets.size(); i++) {
def entries = changeLogSets[i].items
for (int j = 0; j < entries.length; j++) {
def entry = entries[j]
truncated_msg = entry.msg.take(MAX_MSG_LEN)
changeString += " - ${truncated_msg} [${entry.author}]\n"
}
}
if (!changeString) {
changeString = " - No new changes"
}
//I would like to set CHANGE_STRING here
}
}
}
}
post {
success {
echo 'Successfull build'
sh """
bash /var/lib/jenkins/jobs/my-project/hooks/onsuccess
"""
}
}
}
If you want to export environment variable from script step and access it outside the current stage you have to use a variable name that was not specified in global or local environment {} block. Consider following example:
pipeline {
agent any
environment {
IMMUTABLE_VARIABLE = 'my value'
}
stages {
stage('Build') {
steps {
script{
def random = new Random()
if (random.nextInt(2) == 1) {
env.CHANGE_STRING = "Lorem ipsum dolor sit amet"
} else {
env.CHANGE_STRING = "Foo Bar"
}
env.IMMUTABLE_VARIABLE = 'a new value'
echo "IMMUTABLE_VARIABLE = ${env.IMMUTABLE_VARIABLE}"
}
}
}
}
post {
success {
echo 'Successfull build'
sh '''
echo $CHANGE_STRING
echo "IMMUTABLE_VARIABLE = $IMMUTABLE_VARIABLE"
'''
}
}
}
This is just a simplification of your pipeline script. When I run it I see following console output:
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/test-pipeline
[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Build)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
IMMUTABLE_VARIABLE = my value
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Declarative: Post Actions)
[Pipeline] echo
Successfull build
[Pipeline] sh
[test-pipeline] Running shell script
+ echo Foo Bar
Foo Bar
+ echo IMMUTABLE_VARIABLE = my value
IMMUTABLE_VARIABLE = my value
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
The shell script in post success block prints Foo Bar in the first line and IMMUTABLE_VARIABLE = my value in the second one. Also notice that even though I have explicitly try to override
env.IMMUTABLE_VARIABLE = 'a new value'
it didn't make any effect and when I did
echo "IMMUTABLE_VARIABLE = ${env.IMMUTABLE_VARIABLE}"
it simply echoed the initial value from environment {} block:
IMMUTABLE_VARIABLE = my value
Hope it helps.
My question is similar to this one about how to load an external groovy script, and then calling a method from it in a different groovy script. So far I have been able to get methods that don't return a value to work but I am having trouble getting a returned value into a variable that is called.
For example, the following pipeline code works but gives a value of null for $build_user when I run the Jenkins pipeline. It doesn't actually return what I expect it to and I don't know why.
node {
stage('test') {
def tools = load "/var/lib/jenkins/workflow-libs/vars/tools.groovy"
build_user = tools.get_user()
echo "build_user: $build_user"
}
}
Here is what the relevant tools.groovy looks like.
def exampleMethod() {
// Do stuff
}
// Try to get a build username
def get_user() {
try {
wrap([$class: 'BuildUser']) {
// Set up our variables
fallback_user = 'GitHub'
github_user = BUILD_USER
commit_author = 'Test1'
// Try to use Jenkins build user first
if (github_user) {
echo "using github_user: $github_user"
return github_user
}
// Otherwise try to use commit author
else if (commit_author) {
echo "using commit_author: $commit_author"
return commit_author
}
// Otherwise username is blank so we use the default fallback
else {
echo "using fallback: $fallback_user"
return fallback_user
}
}
}
catch (err) {
// Ignore errors
}
echo "Done."
}
return this
Here is the full Jenkins output for the above code.
Started by user XXX
[Pipeline] node
Running on master in /var/lib/jenkins/workspace/test
[Pipeline] {
[Pipeline] stage
[Pipeline] { (test)
[Pipeline] load
[Pipeline] { (/var/lib/jenkins/workflow-libs/vars/tools.groovy)
[Pipeline] }
[Pipeline] // load
[Pipeline] wrap
[Pipeline] {
[Pipeline] echo
using github_user: XXX
[Pipeline] }
[Pipeline] // wrap
[Pipeline] echo
Done.
[Pipeline] echo
build_user: null
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
The above method doesn't work at all if I remove return this at the end and throws the following error in Jenkins.
java.lang.NullPointerException: Cannot invoke method get_user() on
null object
...
What am I doing wrong? I suspect that I'm missing something easy but I'm not great with Groovy, so I'm not sure what it could be.
You have to end your tools.groovywith return this.
See the answer on this question How do you load a groovy file and execute it
your function get_user() returns nothing.
the return(s) inside wrap([$class: 'BuildUser']) {...} do return from wrap class and not from your function.