Home > database >  Jenkins pipeline, is there a way to set environment variable from console output
Jenkins pipeline, is there a way to set environment variable from console output

Time:10-27

My jenkinsfile containes two stages, build and upload&scan (veracode for static scans). My console output would contain somthing like: build_id="21682834" refers to veracode scan ID.

Can anyone help in finding this number and set it as environment variable?

CodePudding user response:

Check the following code.

def consoleLog = Jenkins.getInstance().getItemByFullName(env.JOB_NAME).getBuildByNumber(Integer.parseInt(env.BUILD_NUMBER)).logFile.text

def buildId = (consoleLog =~ 'build_id="(.*)"')[0][1]
echo "build_id: $buildId"
env.build_id = buildId

Full Pipeline for testing.

pipeline {
    agent any
    stages {
        stage('Hello') {
            steps {
                script {
                    echo "Something"
                    echo "Something"
                    echo "Something"
                    echo "Something"
                    echo 'build_id="21682834"'
                    echo "Something"
                    echo "Something"
                    def consoleLog = Jenkins.getInstance().getItemByFullName(env.JOB_NAME).getBuildByNumber(Integer.parseInt(env.BUILD_NUMBER)).logFile.text

                    def buildId = (consoleLog =~ 'build_id="(.*)"')[0][1]
                    echo "build_id: $buildId"
                    env.build_id = buildId
                }
            }
        }
    }
}
  • Related