daks
daks

Reputation: 883

Declarative Jenkins Pipeline; How to declare a variable and use it in script or mail notification?

(update below)

I have a declarative pipeline job which can take an argument VERSION.

pipeline {
  parameters {
    string(name: VERSION, defaultValue: '')
  }

  // ...
}

If no VERSION is given, like when Gitlab send a hook to this job, I want to compute it from git, so I do something like this

stages {
  stage('Prepare') {
    steps {
      // ...
      if (! env.VERSION) {
        VERSION = sh(script: "git describe", returnStdout: true).trim()
      }
    }
  }
}       

Now I want to "inject" this variable to

I tried changing above code with

stages {
  stage('Prepare') {
    steps {
      // ...
      if (! env.VERSION) {
        env.VERSION = sh(script: "git describe", returnStdout: true).trim()
      }
    }
  }
}       

Got this error groovy.lang.MissingPropertyException: No such property: VERSION for class: groovy.lang.Binding

I then tried to add a "environment" step below

environment {
    VERSION = ${VERSION}
}

but it didn't solve my problem.

I'm looking for any help to solve it.

UPDATE

I now have a working pipeline which looks like

pipeline {
    agent any
    parameters {
        string(name: 'VERSION', defaultValue: '')
    }   
    environment {          
        def VERSION = "${params.VERSION}"
    }
    stages {
        stage('Prepare & Checkout') {
            steps {
                script {
                    if (! env.VERSION) {
                       VERSION = sh(script: "date", returnStdout: true).trim()
                    }
                    echo "** version: ${VERSION} **"
                }
            }
        }  
        stage('Build') {
            steps {
                // sh "./build.sh"
                echo "** version2: ${VERSION} **"
            }
        }
    } // stages
    post {
        always {
            mail to: '[email protected]',
                 subject: "SUCCESS: ${VERSION}",
                 body: """<html><body><p>SUCCESS</p></body></html>""",
                 mimeType: 'text/html',
                 charset: 'UTF-8'
            deleteDir()
        }
    }
} // pipeline

I needed to add the "environment" step to be able to get $VERSION in all Stages (not only in the one it is manipulated).

I still need to find a way to inject this $VERSION variable in the environment variables, so that my build script can find it.

Upvotes: 7

Views: 32483

Answers (2)

Vasiliki Siakka
Vasiliki Siakka

Reputation: 1283

If you want to inject the variable in the environment so that you can use it later, you could define another variable that is equal to env.VERSION or the output of the shell scrip. Then use that variable in your pipeline eg:

pipeline {
  parameters {
    string(name: VERSION, defaultValue: '')
  }

  def version = env.VERSION
  stages {
  stage('Prepare') {
    steps {
      // ...
      if (!version) {
        version = sh(script: "git describe", returnStdout: true).trim()
      }
    }
  }

  mail subject: "$version build succeeded", ...
}

If you want other jobs to be able to access the value of VERSION after the build is run, you can write it in a file and archive it.

Edit: In order for your script to be able to use the version variable, you can either make your script take version as a parameter or you can use the withEnv step.

Upvotes: 3

Ori Marko
Ori Marko

Reputation: 58772

Assuming you are using Parametrized pipelines, you should call variable as ${params.parameterName}

Although parameters are available in env they currently are created before the first time the pipeline is run, therefore you should access them via params:

In your case:

 ${params.VERSION}

Upvotes: 0

Related Questions