如果上传失败,Nexus 工件上传插件不会使管道失败

问题描述

我创建了一个声明式管道

  1. 使用 maven 构建工件
  2. 将工件上传到 Nexus 存储库

使用的插件- Nexus Artifact 上传插件

Nexus- Sonatype Nexus OSS 3.17

但是在这里,当上传工件到 nexus repo 失败时,管道不会失败,它显示为成功。我查了很多其他人都报告了这个问题。

那么我可以在此处添加任何解决方法,以便在上传失败时使我的管道失败吗?

我附上了发布神器阶段和截图

    stage("Publish to Nexus") {

    environment { 
                        tag_version= sh (returnStdout: true,script: 'mvn -f core build-helper:parse-version help:evaluate -Dexpression=project.version -q -DforceStdout').trim()
                    }
    steps {

        sh 'echo "tag_version=$tag_version"'

        script {
                    
                    nexusArtifactUploader(
                        nexusversion: NEXUS_VERSION,protocol: NEXUS_PROTOCOL,nexusUrl: NEXUS_URL,groupId: "com.cable",version: tag_version,repository: NEXUS_REPOSITORY,credentialsId: NEXUS_CREDENTIAL_ID,artifacts: [
                            // Artifact generated such as .jar,.ear and .war files.
                            [artifactId: 'core',classifier: '',file: "./core/target/core-$tag_version-jar-with-dependencies.jar",type: 'jar'],]
                    );

                
            }
        }
    }

控制台输出

Failed to deploy artifacts: Could not transfer artifact com.cable:core:jar:8.6.1 from/to snapshots (https://nexus.yo-digital.com/repository/snapshots): Failed to transfer file: https://nexus.yo-digital.com/repository/snapshots/com/cable/core/8.6.1/core-8.6.1.jar. Return code is: 400,ReasonPhrase:Repository version policy: SNAPSHOT does not allow version: 8.6.1.
    [Pipeline] }
    [Pipeline] // script
    [Pipeline] }
    [Pipeline] // withEnv
    [Pipeline] }
    [Pipeline] // withEnv
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] }
    [Pipeline] // withEnv
    [Pipeline] }
    [Pipeline] // withEnv
    [Pipeline] }
    [Pipeline] // withEnv
    [Pipeline] }
    [Pipeline] // ws
    [Pipeline] }
    [Pipeline] // node
    [Pipeline] End of Pipeline
    Finished: SUCCESS

解决方法

我发现这是插件(Nexus Artifact Uploader)的问题。因此,为了在发生任何此类情况时使管道失败,我们可以添加一个阶段来检查控制台输出日志,并在出现任何“错误”字符串时使管道失败。

        stage('Results') 
        {
           steps 
           {
              script 
              {
                  def logz = currentBuild.rawBuild.getLog(10000);
                  def result = logz.find { it.contains('Failed to deploy artifacts') }
                  if (result) 
                  {
                     error (result)
                  }
               }
            }
         }