0
votes

I'm trying to create a Jenkins pipeline where I in my first stage I define a variable in a sh shell script. Then I want to run the next stages using a "when" condition depending on the previous defined variable.

pipeline {
    agent { label 'php71' }
    stages {
        stage('Prepare CI...') {
            steps{
                sh '''
                    # Get the comment that was made on the PR
                    COMMENT=`echo $payload | jq .comment.body | tr -d '"'`
                    if [ "$COMMENT" = ":repeat: Jenkins" ]; then
                        BUILD="build"
                    fi
                '''
            }
        }
        stage('Build Pre Envrionment') {
            agent { label 'php71' }
            when {
                expression { return $BUILD == "build" }
            }
            steps('Build') {
                sh '''
                    echo $BUILD
                    echo $COMMENT
                '''
            }
        }
    }
}

This gives me an error: groovy.lang.MissingPropertyException: No such property: $BUILD for class: groovy.lang.Binding

How can I do it? Is it possible? Thank you!

1
I've found some way of doing this here: stackoverflow.com/questions/44099851/… - XorX
I'm just trying to find a better solution without having to use a file... - XorX

1 Answers

1
votes

Probably use Jenkins scripted pipeline which is more flexible than declarative. Print the value in the sh script and use returnStdout to make it available to the pipeline script. See How to do I get the output of a shell command executed using into a variable from Jenkinsfile (groovy)? for more details.