在Jenkins管道中构建期间访问阶段名称

时间:2016-04-08 15:00:39

标签: 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.rawBuild获得构建运行时的访问权限。 但是如何从那一点获得舞台名称?

我需要在电子邮件通知中进行一些自定义,以便我可以始终捕获失败的阶段名称并将其包含在电子邮件正文中。

4 个答案:

答案 0 :(得分:13)

现在,您可以以内置方式执行此操作,因为Jenkins 2.3。像这样:

steps {
    updateGitlabCommitStatus name: STAGE_NAME, state: 'running'
    echo '${STAGE_NAME}'
}

有关详细信息,请参阅:https://issues.jenkins-ci.org/browse/JENKINS-44456

答案 1 :(得分:6)

这应该来自管道共享库:

#!/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)
}

使用示例:

node {
    stage('Stage One'){
        echo getCurrentStage()
    }

    stage('Stage Two'){
        echo getCurrentStage()
    }
}

答案 2 :(得分:4)

阿莱克斯&#39;解决方法工作正常,只是认为它值得分享代码

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")
}

答案 3 :(得分:0)

作为解决方法,在失败的电子邮件中,我包含指向管道步骤页面的链接。此页面清楚地显示了每一步的绿色和红色球,使电子邮件收件人不仅可以轻松找出舞台,还可以找出失败的步骤。

在以下示例电子邮件正文中,treemap(grouped, index="Sub-Category", vSize="Total_Sales", vColor="Ship Mode", type="categorical" ) 链接指向管道步骤

FlowGraphTable

这是我实施def details = """<p>Job '${env.JOB_NAME}', build ${env.BUILD_NUMBER} result was ${buildStatus}. Please scrutinize the build and take corrective action.</p> <p>Quick links to the details: <ul> <li><a href="${env.JOB_URL}">${env.JOB_NAME} job main page</a></li> <li><a href="${env.BUILD_URL}">Build ${env.BUILD_NUMBER} main page</a></li> <ul> <li><a href="${env.BUILD_URL}console">Console output</a></li> <li><a href="${env.BUILD_URL}changes">Git changes</a></li> <li><a href="${env.BUILD_URL}flowGraphTable">Pipeline steps</a>. This page will show you which step failed, and give you access to the job workspace.</li> </ul> </ul></p>""" 的摘录,其中BitwiseMan的CloudBees在他的文章Sending Notifications in Pipeline中提到了这一点。