舞台和后行动之间詹金斯管道中变量的范围

发布于 2025-02-11 18:57:41 字数 2072 浏览 0 评论 0原文

我有遵循用于自动化测试运行的管道代码,其中IM面临一个问题,在该问题中,阶段部分中定义的变量并试图在post Action部分中使用该变量的值,但是问题出现在此处,而变量的值则为null。因此,我怀疑这与变量范围有关。有人可以指导我解决这个问题吗?

请找到我的代码片段

import java.text.SimpleDateFormat
env.JenkinsAgent = 'JenkinsNode'
env.StartTime
env.EndTime

pipeline 
    {
    agent {label """'${JenkinsAgent}'"""} 
    stages 
        { 
       stage ('Execute Test Run & Notify Users')
            {
            steps 
                {
                script 
                    {
                    env.StartTime = new Date ()
                    env.StartTime = (env.StartTime.getTime())
                    echo "Epoch Timestamp - Start time is: ${StartTime} "
                    bat label: '', returnStdout: true,script: "Run.bat"
                    echo "LoadTest Execution completed" 
                    env.EndTime = new Date ()
                    env.EndTime = (env.EndTime.getTime())
                    echo "Epoch Timestamp - End time is: ${EndTime} "
                    }
                }
            post // action to be taken based on the above steps success or failure
            {
            success
                {
                script 
                    { 
                    currentBuild.result = 'SUCCESS'
                    echo "Load Test executed Successfully"
                    mail bcc: ' ',
                    body: """Test completed. Start time is ${StartTime} and End time is ${EndTime}""", 
                    cc: """${EmailCC}""", 
                    from: 'Jenkins', 
                    mimeType: 'text/html', 
                    replyTo: '',
                    subject: """Jenkins Alert: Stage : Execute Load Test completed""", 
                    to: """${EmailTo}"""
                    }
                }
            failure
                {
                script 
                    {
                    currentBuild.result = 'FAILURE'
                    error('Aborting the build.')
                    }
                }   
            }    
        }
    } // stages  
} //pipeline

i have following pipeline code created for automating test runs in which im facing an issue where the variable defined in Stage section and trying to use value of that variable in post action section, but problem comes here where the value of variable comes as null. So i suspect this something related to the variable scope. Could someone please guide me in this problem?

Please find my code snippet

import java.text.SimpleDateFormat
env.JenkinsAgent = 'JenkinsNode'
env.StartTime
env.EndTime

pipeline 
    {
    agent {label """'${JenkinsAgent}'"""} 
    stages 
        { 
       stage ('Execute Test Run & Notify Users')
            {
            steps 
                {
                script 
                    {
                    env.StartTime = new Date ()
                    env.StartTime = (env.StartTime.getTime())
                    echo "Epoch Timestamp - Start time is: ${StartTime} "
                    bat label: '', returnStdout: true,script: "Run.bat"
                    echo "LoadTest Execution completed" 
                    env.EndTime = new Date ()
                    env.EndTime = (env.EndTime.getTime())
                    echo "Epoch Timestamp - End time is: ${EndTime} "
                    }
                }
            post // action to be taken based on the above steps success or failure
            {
            success
                {
                script 
                    { 
                    currentBuild.result = 'SUCCESS'
                    echo "Load Test executed Successfully"
                    mail bcc: ' ',
                    body: """Test completed. Start time is ${StartTime} and End time is ${EndTime}""", 
                    cc: """${EmailCC}""", 
                    from: 'Jenkins', 
                    mimeType: 'text/html', 
                    replyTo: '',
                    subject: """Jenkins Alert: Stage : Execute Load Test completed""", 
                    to: """${EmailTo}"""
                    }
                }
            failure
                {
                script 
                    {
                    currentBuild.result = 'FAILURE'
                    error('Aborting the build.')
                    }
                }   
            }    
        }
    } // stages  
} //pipeline

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

装迷糊 2025-02-18 18:57:41

如果要在阶段之间使用变量,则需要将它们定义为全局变量。请参阅以下管道。

// Global variable
def globalVar = "Initial Value"

pipeline {
    agent any

    stages {
        stage('Build') {
            steps {
                script {
                    echo "Printing initial values : ${globalVar}"
                    // Reassigning a value
                    globalVar = "New value"
                }
            }

            post {
                success {
                    echo "${globalVar}"
                }
            }
        }
    }
}

If you want to use variables between stages, you need to define them as global variables. Refer to the following pipeline.

// Global variable
def globalVar = "Initial Value"

pipeline {
    agent any

    stages {
        stage('Build') {
            steps {
                script {
                    echo "Printing initial values : ${globalVar}"
                    // Reassigning a value
                    globalVar = "New value"
                }
            }

            post {
                success {
                    echo "${globalVar}"
                }
            }
        }
    }
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文