舞台和后行动之间詹金斯管道中变量的范围
我有遵循用于自动化测试运行的管道代码,其中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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果要在阶段之间使用变量,则需要将它们定义为全局变量。请参阅以下管道。
If you want to use variables between stages, you need to define them as global variables. Refer to the following pipeline.