2016-04-08 21 views
17

Hãy nói rằng chúng tôi có những điều sau Jenkinsfile:Tên truy cập Giai đoạn trong xây dựng trong các đường ống Jenkins

stage name: "Cool stage" 
    sh 'whoami' 
stage name: "Better stage" 
    def current_stage = getCurrentStageName() 
    echo "CONGRATULATIONS, you are on stage: $current_stage" 

Câu hỏi đặt ra là làm thế nào để thực hiện getCurrentStageName(). Tôi biết, tôi có thể truy cập để xây dựng thời gian chạy bằng cách sử dụng currentBuild.rawBuild. Nhưng làm thế nào để có được tên sân khấu từ thời điểm đó?

Tôi cần điều này cho một số tùy chỉnh trong thông báo qua email, để tôi luôn có thể bắt được tên sân khấu không thành công và đưa nó vào nội dung email.

+1

Cậu thậm chí còn tìm thấy câu trả lời cho điều này? Tôi đang tìm kiếm cùng một điều bây giờ – Craigt

+4

Không phải là câu trả lời thực sự nhưng workaround. Bạn có thể tạo một loại trình bao bọc, sẽ chấp nhận tên giai đoạn, viết nó vào biến toàn cầu và gọi là 'stage' – Aleks

Trả lời

0

Cách giải quyết khác, trong email không thành công, tôi bao gồm liên kết tới trang Đường ống. Trang này hiển thị rõ ràng các quả bóng màu xanh lá cây và màu đỏ cho mỗi bước, giúp người nhận email dễ dàng tìm ra không chỉ giai đoạn, mà là bước không thành công.

Trong cơ thể ví dụ email sau, FlowGraphTable liên kết liên kết đến Pipeline bước:

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

Đây là một đoạn trích từ thực hiện của tôi notifyBuild() rằng BitwiseMan của CloudBees trình bày trong bài viết của mình, Sending Notifications in Pipeline.

workaround
3

Aleks' hoạt động tốt, chỉ nghĩ nó có giá trị chia sẻ mã

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

này nên làm việc từ một đường ống dẫn thư viện chia sẻ:

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

Ví dụ sử dụng:

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

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

Tôi đã quản lý để sửa đổi tập lệnh của bạn để có được các giai đoạn đã thực hiện trước đó. Bạn có biết cách để có được kết quả xây dựng của những giai đoạn trước đó không? – andsens

+0

OK! http://javadoc.jenkins.io/plugin/pipeline-rest-api/com/cloudbees/workflow/flownode/FlowNodeUtil.html#getStatus-org.jenkinsci.plugins.workflow.graph.FlowNode- – andsens

+0

Tôi đã làm ví dụ cho nhận kết quả của các giai đoạn: https://gist.github.com/GuillaumeSmaha/fdef2088f7415c60adf95d44073c3c88 – GuillaumeS

5

Bây giờ bạn có thể làm điều này một cách tích hợp, kể từ Jenkins 2.3. Cũng giống như vậy:

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

Để biết thêm thông tin xem: https://issues.jenkins-ci.org/browse/JENKINS-44456

Các vấn đề liên quan