在构建Jenkins管道期间访问阶段名称
假设我们有以下在构建Jenkins管道期间访问阶段名称,jenkins,groovy,jenkins-workflow,Jenkins,Groovy,Jenkins Workflow,假设我们有以下Jenkinsfile: stage name: "Cool stage" sh 'whoami' stage name: "Better stage" def current_stage = getCurrentStageName() echo "CONGRATULATIONS, you are on stage: $current_stage" 问题是如何实现getCurrentStageName()。我知道,我可以使用currentBuild.raw
Jenkinsfile
:
stage name: "Cool stage"
sh 'whoami'
stage name: "Better stage"
def current_stage = getCurrentStageName()
echo "CONGRATULATIONS, you are on stage: $current_stage"
问题是如何实现getCurrentStageName()
。我知道,我可以使用currentBuild.rawBuild
访问构建运行时。
但如何从这一点上获得舞台名称呢
在电子邮件通知中,我需要一些自定义设置,这样我就可以始终捕获失败的阶段名称并将其包含到电子邮件正文中。作为一种解决方法,在失败电子邮件中,我包含一个指向管道步骤页面的链接。此页面清楚地显示了每个步骤的绿色和红色小球,使电子邮件收件人能够轻松地了解到不仅仅是阶段,还包括失败的步骤 在以下示例电子邮件正文中,
FlowGraphTable
链接到管道步骤:
def details=“”Job'${env.Job_NAME},build${env.build_NUMBER}结果是${buildStatus}。
请仔细检查构建并采取纠正措施
有关详细信息的快速链接:
- 。
此页面将显示失败的步骤,并提供访问权限
到作业工作区
“”“
这是我在他的文章中介绍的CloudBees的
notifyBuild()
实现的摘录。Aleks的解决方案很好,只是觉得值得分享代码
node ("docker") {
def sendOk = {
String stage -> slackSend color: 'good', message: stage + " completed, project - ${env.JOB_NAME}:1.0.${env.BUILD_NUMBER}"
}
def sendProblem = {
String stage, error -> slackSend color: 'danger', message: stage + " did not succeed, project - ${env.JOB_NAME}:1.0.${env.BUILD_NUMBER}, error: ${error}, Find details here: ${env.BUILD_URL}"
}
def exec = {
work, stageName ->
stage (stageName) {
try {
work.call();
sendOk(stageName)
}
catch(error) {
sendProblem(stageName, error)
throw error
}
}
}
exec({
git credentialsId: 'github-root', url: 'https://github.com/abc'
dir ('src') {
git credentialsId: 'github-root', url: 'https://github.com/abc-jenkins'
}
sh "chmod +x *.sh"
}, "pull")
exec({ sh "./Jenkinsfile-clean.sh \"1.0.${env.BUILD_NUMBER}\"" }, "clean")
exec({ sh "./Jenkinsfile-unit.sh \"1.0.${env.BUILD_NUMBER}\"" }, "unit")
exec({ sh "./Jenkinsfile-build.sh \"1.0.${env.BUILD_NUMBER}\"" }, "build")
exec({ sh "./Jenkinsfile-dockerize.sh \"1.0.${env.BUILD_NUMBER}\"" }, "dockerize")
exec({ sh "./Jenkinsfile-push.sh \"1.0.${env.BUILD_NUMBER}\"" }, "push")
exec({ sh "./Jenkinsfile-prod-like.sh \"1.0.${env.BUILD_NUMBER}\"" }, "swarm")
}
这应该在管道共享库中工作:
#!/usr/bin/env groovy
import hudson.model.Action;
import org.jenkinsci.plugins.workflow.graph.FlowNode
import org.jenkinsci.plugins.workflow.cps.nodes.StepStartNode
import org.jenkinsci.plugins.workflow.actions.LabelAction
def getStage(currentBuild){
def build = currentBuild.getRawBuild()
def execution = build.getExecution()
def executionHeads = execution.getCurrentHeads()
def stepStartNode = getStepStartNode(executionHeads)
if(stepStartNode){
return stepStartNode.getDisplayName()
}
}
def getStepStartNode(List<FlowNode> flowNodes){
def currentFlowNode = null
def labelAction = null
for (FlowNode flowNode: flowNodes){
currentFlowNode = flowNode
labelAction = false
if (flowNode instanceof StepStartNode){
labelAction = hasLabelAction(flowNode)
}
if (labelAction){
return flowNode
}
}
if (currentFlowNode == null) {
return null
}
return getStepStartNode(currentFlowNode.getParents())
}
def hasLabelAction(FlowNode flowNode){
def actions = flowNode.getActions()
for (Action action: actions){
if (action instanceof LabelAction) {
return true
}
}
return false
}
def call() {
return getStage(currentBuild)
}
您现在可以通过内置的方式来实现这一点,因为Jenkins 2.3。像这样:
steps {
updateGitlabCommitStatus name: STAGE_NAME, state: 'running'
echo '${STAGE_NAME}'
}
有关更多信息,请参阅:您是否找到了此问题的答案?我现在也在寻找同样的问题不是真正的答案而是解决办法。您可以创建一种包装器,它将接受stage名称,将其写入全局变量并调用
stage
I已设法修改您的脚本以获得先前执行的stage。你知道一种方法来获得前几个阶段的构建结果吗?明白了!我举了一个例子来获得阶段的结果:很棒的东西,我还能够提取parallel()
分支名称。这些函数没有在沙盒中使用的白名单。非常有用,谢谢(它需要添加到他们的文档中!)如果我们能够获得阶段名称和阶段结果,那就太好了
node {
stage('Stage One'){
echo getCurrentStage()
}
stage('Stage Two'){
echo getCurrentStage()
}
}
steps {
updateGitlabCommitStatus name: STAGE_NAME, state: 'running'
echo '${STAGE_NAME}'
}