Nexus Artifact Upload Plugin does not fail pipeline, if upload fails

Viewed 850

I have created a declarative pipeline which

  1. Builds the artifact using maven
  2. Uploads the artifact to Nexus repository

Plugins Used- Nexus Artifact Uploader Plugin

Nexus- Sonatype Nexus OSS 3.17

But here, when the upload artifact to nexus repo fails, pipeline does not fail, it shows as success. I checked that many other people have reported this issue.

So can I add any workaround here, to fail my pipeline if upload fails?

I have attached the publish artifact stage and the screenshot

    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'],
                        ]
                    );

                
            }
        }
    }

Console Output

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
1 Answers

I found that this is an issue with the plugin (Nexus Artifact Uploader). So in order to fail the pipeline if any such condition occurs, we can add a stage which will check the console output logs, and will fail the pipeline if any "error" string appears.

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