具有Slack的Jenkins管道评估步骤

时间:2018-12-07 15:05:15

标签: jenkins jenkins-pipeline pipeline

  

我正在尝试在configFileProvider块中进行评估,   在步骤块中,在管道中,这是完整的代码。

<div class="three-obj">
  <div class="two">
    <div class="one">
      <label for="thing">thing1</label>
      <input class="inputColor" type="text" name="thing" value="">
    </div>
  </div>
</div>

<div class="three-arr">
  <div class="two">
    <div class="one">
      <label for="thing2">thing2</label>
      <input class="inputColor" type="text" name="thing2" value="">
    </div>
  </div>
</div>
  

我感兴趣的区块是:

pipeline {
agent any
environment {
    GIT_CREDS = credentials('GIT')
    }

stages {
    stage ('Load Repos') {
    steps {
        cleanWs notFailBuild: true
        sh """
        env
        git clone "https://$GIT_CREDS@$GIT_URL
        git clone "https://$GIT_CREDS@$CONTENT_URL_SHORT"
        """
    }
    }

    stage ('Run Content Build Script') {
    steps {
        slackSend channel: 'notif', message: "Started Content Build and Validate for ${env.CONTENT_GIT_REPO} for Repository ${env.CONTENT_GIT_URL} by ${env.JOB_NAME} (<${env.BUILD_URL}|Open>)", tokenCredentialId: '<Token>'
        configFileProvider(
        [configFile(fileId: '605dae22-d459-4c9a-8d3c-f88d305195aa', variable: 'script')]) {
            sh(returnStatus: true, script: """python3.6 $script""")
            if (returnStatus != 0) {
                currentBuild.result = 'FAILED'
                slackSend channel: SLACK_CHANNEL, message: "Content Build and Validate FAILED for ${env.CONTENT_GIT_REPO} for Repository ${env.CONTENT_GIT_URL} by ${env.JOB_NAME} (<${env.BUILD_URL}|Open>)", color: 'warning', tokenCredentialId: 'slack-token'
            }
            else {
                slackSend channel: SLACK_CHANNEL, message: "Content Build and Validate Succeded for ${env.CONTENT_GIT_REPO} for Repository ${env.CONTENT_GIT_URL} by ${env.JOB_NAME} (<${env.BUILD_URL}|Open>)", color: 'success', tokenCredentialId: 'slack-token'
            }
        }
    }
    }
  

基本上,我想根据返回状态评估脚本的成功,并根据此状态发送松弛消息。我尝试了几种不同的方法,例如尝试将脚本设置为像评估那样运行:

if(python3.6“ $ script){......

  

那也不起作用,任何建议将不胜感激。

1 个答案:

答案 0 :(得分:2)

几乎正确。 (1)您需要将if逻辑放在脚本步骤中。 (2)您需要保存sh步骤的结果,并在if中使用它。

configFileProvider(
    [configFile(fileId: '605dae22-d459-4c9a-8d3c-f88d305195aa', variable: 'script')]) {
        script {
            def int buildStatus = sh(returnStatus: true, script: """python3.6 $script""")
            if (buildStatus != 0) {
                currentBuild.result = 'FAILED'
                slackSend channel: SLACK_CHANNEL, message: "Content Build and Validate FAILED for ${env.CONTENT_GIT_REPO} for Repository ${env.CONTENT_GIT_URL} by ${env.JOB_NAME} (<${env.BUILD_URL}|Open>)", color: 'warning', tokenCredentialId: 'slack-token'
            }
            else {
                slackSend channel: SLACK_CHANNEL, message: "Content Build and Validate Succeded for ${env.CONTENT_GIT_REPO} for Repository ${env.CONTENT_GIT_URL} by ${env.JOB_NAME} (<${env.BUILD_URL}|Open>)", color: 'success', tokenCredentialId: 'slack-token'
            }

        }
    }

尝试一下,看看它如何工作。