Jenkins 管道阶段在尝试步骤之前就失败了,没有失败的日志

问题描述 投票:0回答:1

我正在尝试向 Jenkins 管道添加一个阶段,该阶段将通过 Postman 集合运行 42Crunch 一致性扫描。然而,由于某种原因,管道阶段在阶段的

steps
部分之前或处失败。这是我添加到我们的管道中的内容:

stage('42 Crunch Conformance Scan')  
  {
    echo("*** Started 42 Crunch Conformance Scan ***")
    environment {
      http_proxy = '****'
      https_proxy = '****'
    }
    echo("Proxy Set")
      steps{
 
        withCredentials([string(credentialsId: '42crunch-api-token', variable: 'API_KEY')]) {
          sh 'newman -version'
          sh("newman run integration/42crunch/${appName}42Crunch.postman_collection.json "
                            + "-e integration/smoke/${appName}ApigeeSmokeTest.postman_environment.json --env-var runningOnPipeline=yes --env-var x-api-key=$API_KEY")
        }
      }
      post {
        failure {
          archiveArtifacts artifacts: '**/newman/*.html', excludes: null, fingerprint: true
          }
        }
      }

我收到了

Proxy Set
日志,但此后它立即失败。它甚至没有达到
post
步骤。我有 Credentials Binding 插件,并将 newman npm 包添加到我们的 NodeJS 插件中。我尝试将整个阶段包装起来,然后只将
steps
包装在 try/catch 中,但每次仍然失败,没有任何日志。任何帮助或指导将不胜感激。谢谢你。

jenkins jenkins-pipeline jenkins-plugins jenkins-groovy
1个回答
0
投票

看起来您正在以不允许的方式混合脚本化和声明性语法。在声明性语法中,

echo
是一个步骤,它不能在
steps
块之外工作。而且 AFAIR,
withCredentials
只能在
script
块中使用。正确的示例如下所示:

stage('42 Crunch Conformance Scan') {
  environment {
    http_proxy = '****'
    https_proxy = '****'
  }
  steps {
    echo("*** Started 42 Crunch Conformance Scan ***")
    echo("Proxy Set")
    script {
        withCredentials([string(credentialsId: '42crunch-api-token', variable: 'API_KEY')]) {
        sh 'newman -version'
        sh("newman run integration/42crunch/${appName}42Crunch.postman_collection.json "
                            + "-e integration/smoke/${appName}ApigeeSmokeTest.postman_environment.json --env-var runningOnPipeline=yes --env-var x-api-key=$API_KEY")
      }
    }
    post {
      failure {
        archiveArtifacts artifacts: '**/newman/*.html', excludes: null, fingerprint: true
      }
    }
  }
}

为了使您的管道代码更加干净和易于理解,您可以使用凭证绑定插件的功能,该功能允许将凭证直接绑定到

environment
块中的环境变量:

stage('42 Crunch Conformance Scan') {
  environment {
    http_proxy = '****'
    https_proxy = '****'
    API_KEY = credentials('42crunch-api-token')
  }
  steps {
    echo("*** Started 42 Crunch Conformance Scan ***")
    echo("Proxy Set")
    script {
      sh 'newman -version'
      sh("newman run integration/42crunch/${appName}42Crunch.postman_collection.json "
                            + "-e integration/smoke/${appName}ApigeeSmokeTest.postman_environment.json --env-var runningOnPipeline=yes --env-var x-api-key=$API_KEY")
    }
    post {
      failure {
        archiveArtifacts artifacts: '**/newman/*.html', excludes: null, fingerprint: true
      }
    }
  }
}

此外,编写额外的日志代码通常会使日志变得不必要的混乱(特别是当使用

echo
而不是
println
时,因为它是重复的),因为 Jenkins 已经记录了任何块的开始和停止。相反,如果您最终需要这个,请考虑使用
label
步骤的
sh
参数。请注意,在此示例中,我还按照建议将两个
sh
调用合并为一个,以减少处理 Groovy 代码时对 Jenkins 控制器造成的负载,并使用多行字符串以获得更好的可读性:

stage('42 Crunch Conformance Scan') {
  environment {
    http_proxy = '****'
    https_proxy = '****'
    API_KEY = credentials('42crunch-api-token')
  }
  steps {
    script {
      sh label: 'Started 42 Crunch Conformance Scan',
        script: '''
          newman -version
          newman run integration/42crunch/${appName}42Crunch.postman_collection.json \
            -e integration/smoke/${appName}ApigeeSmokeTest.postman_environment.json \
            --env-var runningOnPipeline=yes \
            --env-var x-api-key=$API_KEY
        '''
    }
    post {
      failure {
        archiveArtifacts artifacts: '**/newman/*.html', excludes: null, fingerprint: true
      }
    }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.