在 Jenkins 管道中使用环境变量

问题描述 投票:0回答:0

我正在通过组装 Jenkinsfile 来学习 Jenkins 管道。在该文件的各个部分中,我正在创建新的/访问现有的本地/全局环境变量;一些开箱即用的;一些用户定义的..

一切似乎都按预期工作,但对于一个用户定义的环境变量,我将其命名为PYTEST_RESULT。我在脚本顶部声明它(我认为是全局的??),但是当稍后尝试在该脚本中重写它的值时,重写的值似乎被分配给一些动态新创建的变量,而不是被分配给全局环境变量按预期进行。任何我可能做错的想法将不胜感激!

以下是我的 Jenkinsfile 中的适用片段:

// Declare global variables:
def pytestExitCode = ''
def pytestExitCodeDesc = ''
    
pipeline {
    agent any
    environment{
        PROJECT_NAME = "BigTime Web Transformation"
            BUILD_ENV = ""
            BUILD_NAME = "${currentBuild.fullDisplayName}"
            PYTEST_RESULT = "TBD"
    }
    stages {
        stage('Build') {
            steps {
                script {
                      try {
                ...............
                ...........
                ......


        stage('Test') {
            steps {
                // Create environment variable via multi-line 'sh' statement, to capture exit code of pytest run..
                sh '''
                    export pytestExitCode
                    echo "pytestExitCode before being assigned the pytest exit code is: "$pytestExitCode
                '''
                // Run pytest then capture the exit code of its execution..
                script {
                    pytestExitCode = sh(returnStatus: true, script: 'pytest')
                    echo "Pytest completed with exit code: ${pytestExitCode}"
                }
            }
        }


    ...............
    ...........
    ......

    post {
        always {
            script {
                //Evaluate the applicable string to add to email body, based on exit code of Test Stage's pytest run:
                switch(pytestExitCode) {
                    case 0:
                        pytestExitCodeDesc = "All tests were collected and passed successfully"
                        break;
                    case 1:
            ...............
            ...........
            ......

                    // Pass environment variable values into the email template body:
                    env.BUILD_ID=sh(script: 'echo ${BUILD_ID}', returnStdout: true)
                    echo env.BUILD_ID
                    env.JOB_NAME=sh(script: 'echo ${JOB_NAME}', returnStdout: true)
                    echo env.JOB_NAME
                    env.PROJECT_NAME=sh(script: 'echo ${PROJECT_NAME}', returnStdout: true)
                    echo env.PROJECT_NAME
                    env.BUILD_URL=sh(script: 'echo ${BUILD_URL}', returnStdout: true)
                    echo env.BUILD_URL
                    env.BUILD_NAME=sh(script: 'echo ${BUILD_NAME}', returnStdout: true)
                    echo env.BUILD_NAME

                    env.PYTEST_RESULT = "${pytestExitCode} - '${pytestExitCodeDesc}'"   // <- this is what doesn't work !!

                    env.PYTEST_RESULT=sh(script: 'echo ${PYTEST_RESULT}', returnStdout: true)   // <- the initial value of 'TBD' remains the latest value..
                    echo env.PYTEST_RESULT

            ...............
            ...........
            ......
environment-variables jenkins-pipeline jenkins-groovy
© www.soinside.com 2019 - 2024. All rights reserved.