Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-42152

Docker Pipeline causes build to fails because of use of --force=yes for tagging

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: Major Major
    • docker-workflow-plugin
    • None
    • Jenkins 2.32.2
      Docker 1.12.3
      Docker Pipeline Plugin 1.10

      First off, I verify that I have read other tickets relating to similar issues, especially https://issues.jenkins-ci.org/browse/JENKINS-41140. I am convinced this is not a duplicate.

      This Jenkinsfile exits and fails the build as soon as I try to do a Image.push('tag') with error code 125.

      #!groovy
      // define name of project
      projectBaseName = 'gradle'
      
      node {
        def tagVersion= sh(script:"git tag -l --sort=-creatordate | head -n 1", returnStdout: true)
        echo tagVersion
        stage('checkout') {
          checkout scm: [
            $class: 'GitSCM', 
            userRemoteConfigs: [[credentialsId: 'erentodevsSSH', url: 'git@github.com:erento/infra-gradle.git']],
            branches: [[name: "refs/tags/${tagVersion}"]]],
            changelog: false,
            poll: false  
        }
      
        def myImage
        stage('build image') {
          myImage = docker.build("eu.gcr.io/erento-docker/${projectBaseName}")
        }
      
        stage('publish image') {
          myImage.push "${tagVersion}"
          myImage.push 'latest'
        }
      }
      

      Here is the end of the log where the build fails:

      Successfully built 5cd83ad22ca8
      [Pipeline] dockerFingerprintFrom
      [Pipeline] }
      [Pipeline] // stage
      [Pipeline] stage
      [Pipeline] { (publish image)
      [Pipeline] sh
      [-_Infra_infra-gradle_master-324YNW3MJA4FOWI2NQMEZBAZKMJ727NUFOWZA5FUGORPAAWKKMVA] Running shell script
      + docker tag --force=true eu.gcr.io/erento-docker/gradle eu.gcr.io/erento-docker/gradle:3.3-1
      unknown flag: --force
      See 'docker tag --help'.
      [Pipeline] }
      [Pipeline] // stage
      [Pipeline] }
      [Pipeline] // node
      [Pipeline] End of Pipeline
      
      GitHub has been notified of this commit’s build result
      
      ERROR: script returned exit code 125
      Finished: FAILURE
      

      I have verified that switching the order of pushing latest and tagVersion causes the push of latest to work, and the push of tagVersion fails in the exact same manner as shown here.

      I also verified that:
      docker tag --force=true eu.gcr.io/erento-docker/gradle eu.gcr.io/erento-docker/gradle:3.3-1 - fails on console
      docker tag eu.gcr.io/erento-docker/gradle eu.gcr.io/erento-docker/gradle:3.3-1 - does not fail on console

      Please let me know if I can provide any more data useful for fixing this.

          [JENKINS-42152] Docker Pipeline causes build to fails because of use of --force=yes for tagging

          Volker Krebs added a comment -

          jglick, the fallback is using || (OR operator) in shell

          docker tag --force=${force} ${id} ${taggedImageName} || docker tag ${id} ${taggedImageName}

          see Docker.groovy

          How can I prevent in the nasty error message from appearing in the output by setting my environment correctly ?

          I was searching for a couple of hours now, and I think most users get confused, because the error message is still appearing because of the OR operator.

          Volker Krebs added a comment - jglick , the fallback is using || (OR operator) in shell docker tag --force=${force} ${id} ${taggedImageName} || docker tag ${id} ${taggedImageName} see Docker.groovy How can I prevent in the nasty error message from appearing in the output by setting my environment correctly ? I was searching for a couple of hours now, and I think most users get confused, because the error message is still appearing because of the OR operator.

          Jesse Glick added a comment -

          How can I prevent in the nasty error message from appearing in the output

          You cannot. But the stated issue summary is that the build fails and that is what can be seen from sample console output. So I think you are not actually hitting this issue, whatever its cause.

          Jesse Glick added a comment - How can I prevent in the nasty error message from appearing in the output You cannot. But the stated issue summary is that the build fails and that is what can be seen from sample console output. So I think you are not actually hitting this issue, whatever its cause.

          Josh Mesilane added a comment - - edited

          I'm running into this error also. I have the following config:

          Jenkins 2.60.2
          Docker Pipeline Plugin: 1.12

          node {
            docker.withRegistry('someregistry/', 'someid') {
              git url: "somerepo", credentialsId: 'someotherid'
              def version = sh(script: 'date +%y%m%d%H%M-dev', returnStdout: true)
              println version
              stage("Build Image") {
                app = docker.build("somename")
              }
              stage("publish image") {
                app.push "${version}"
              }
            }
          }

          And I'm seeing:

          .......
          Successfully built c57055abfb79

          [Pipeline] dockerFingerprintFrom[Pipeline] }[Pipeline] // stage[Pipeline] stage[Pipeline] { (publish image)[Pipeline] sh

          [sometaskname] Running shell script
          + docker tag --force=true somename somerepo/somename:1707210447-dev
          unknown flag: --force
          See 'docker tag --help'.

          [Pipeline] }[Pipeline] // stage[Pipeline] }[Pipeline] // withDockerRegistry[Pipeline] }[Pipeline] // withEnv[Pipeline] }[Pipeline] // node[Pipeline] End of Pipeline

          ERROR: script returned exit code 125
          Finished: FAILURE

          Josh Mesilane added a comment - - edited I'm running into this error also. I have the following config: Jenkins 2.60.2 Docker Pipeline Plugin: 1.12 node {   docker.withRegistry('someregistry/', 'someid') {     git url: "somerepo", credentialsId: 'someotherid'     def version = sh(script: 'date +%y%m%d%H%M-dev', returnStdout: true)     println version     stage("Build Image") {       app = docker.build("somename")     }     stage("publish image") {       app.push "${version}"     }   } } And I'm seeing: ....... Successfully built c57055abfb79 [Pipeline] dockerFingerprintFrom [Pipeline] } [Pipeline] // stage [Pipeline] stage [Pipeline] { (publish image) [Pipeline] sh [sometaskname] Running shell script + docker tag --force=true somename somerepo/somename:1707210447-dev unknown flag: --force See 'docker tag --help'. [Pipeline] } [Pipeline] // stage [Pipeline] } [Pipeline] // withDockerRegistry [Pipeline] } [Pipeline] // withEnv [Pipeline] } [Pipeline] // node [Pipeline] End of Pipeline ERROR: script returned exit code 125 Finished: FAILURE

          Josh Mesilane added a comment -

          Interestingly, if I specify the tag to be just "latest", or any other static string, this works.

          If however I try to pull the value in from a variable, it produces the above error.

          Josh Mesilane added a comment - Interestingly, if I specify the tag to be just "latest", or any other static string, this works. If however I try to pull the value in from a variable, it produces the above error.

          Josh Mesilane added a comment -

          So I think I just figured this out.

          If you pull a version from a variable, that's generated from a shell script, you have to have trim() on the end. So you do it like:

              def version = sh(script: 'date +%y%m%d%H%M-dev', returnStdout: true).trim()

          ALthough I can actually find no reference to trim() in the pipeline documentation, I'm assuming this trims any newline chars off the end of the variable, and as such allows the push to work sucessfully.

          Josh Mesilane added a comment - So I think I just figured this out. If you pull a version from a variable, that's generated from a shell script, you have to have trim() on the end. So you do it like:     def version = sh(script: 'date +%y%m%d%H%M-dev', returnStdout: true).trim() ALthough I can actually find no reference to trim() in the pipeline documentation, I'm assuming this trims any newline chars off the end of the variable, and as such allows the push to work sucessfully.

          Jesse Glick added a comment -

          zindello yes that is a user error, mentioned months ago IIRC in some open PR in this plugin. The upshot is that attempts to pass a malformed tag should fail fast.

          Jesse Glick added a comment - zindello yes that is a user error, mentioned months ago IIRC in some open PR in this plugin. The upshot is that attempts to pass a malformed tag should fail fast.

          Vadimo added a comment -

          We have Docker Pipeline Plugin 1.10 Jenkins ver. 2.73

          we see exact the same issue:

          Successfully built 3e1c5820e826
          Successfully tagged docker.8gears.com/pdr/helmsman:latest
          [Pipeline] dockerFingerprintFrom
          [Pipeline] sh
          [workspace] Running shell script
          + docker tag --force=true docker.8gears.com/pdr/helmsman docker.8gears.com/pdr/helmsman:latest
          unknown flag: --force
          See 'docker tag --help'.
          + docker tag docker.8gears.com/pdr/helmsman docker.8gears.com/pdr/helmsman:latest
          [workspace] Running shell script
          [Pipeline] sh
          + docker push docker.8gears.com/pdr/helmsman:latest
          The push refers to a repository [docker.8gears.com/pdr/helmsman]
          

          this is the stage

           

                stage("build") {
                  steps {
                    script {
                        def helmsmanImage = docker.build("docker.8gears.com/pdr/helmsman")
                        helmsmanImage.push('latest')
                    }
                  }
                }
          

          Vadimo added a comment - We have Docker Pipeline Plugin 1.10 Jenkins ver. 2.73 we see exact the same issue: Successfully built 3e1c5820e826 Successfully tagged docker.8gears.com/pdr/helmsman:latest [Pipeline] dockerFingerprintFrom [Pipeline] sh [workspace] Running shell script + docker tag --force= true docker.8gears.com/pdr/helmsman docker.8gears.com/pdr/helmsman:latest unknown flag: --force See 'docker tag --help' . + docker tag docker.8gears.com/pdr/helmsman docker.8gears.com/pdr/helmsman:latest [workspace] Running shell script [Pipeline] sh + docker push docker.8gears.com/pdr/helmsman:latest The push refers to a repository [docker.8gears.com/pdr/helmsman] this is the stage   stage("build") { steps { script { def helmsmanImage = docker.build("docker.8gears.com/pdr/helmsman") helmsmanImage.push('latest') } } }

          Jesse Glick added a comment -

          vadimo as you can see, the tagging and pushing succeeds—as designed.

          Jesse Glick added a comment - vadimo as you can see, the tagging and pushing succeeds—as designed.

          Matthew Coleman added a comment - - edited

          If you're using Declarative Pipeline, I was able to push two tags of the same image, one latest and one using the commit, doing the following:

          stage("checkout') {
            steps {
              script {
                # Parse the revision.
                sh 'git rev-parse HEAD > .rev'
                rev = readFile('.rev').trim()
              }
            }
          }
          ... other things ...
          stage('publish') {
            steps {
              script {
                image.push rev
                image.push 'latest'
              }
            }
          }

          The `trim()` call is key, otherwise you get errors mentioned in this issue.

          Matthew Coleman added a comment - - edited If you're using Declarative Pipeline, I was able to push two tags of the same image, one latest and one using the commit, doing the following: stage("checkout') { steps { script { # Parse the revision. sh 'git rev-parse HEAD > .rev' rev = readFile( '.rev' ).trim() } } } ... other things ... stage( 'publish' ) { steps { script { image.push rev image.push 'latest' } } } The `trim()` call is key, otherwise you get errors mentioned in this issue.

          Code changed in jenkins
          User: Aaron Weiker
          Path:
          src/main/resources/org/jenkinsci/plugins/docker/workflow/Docker.groovy
          http://jenkins-ci.org/commit/docker-workflow-plugin/c961afc373ca9cb65051865e6d6651c1911f620d
          Log:
          JENKINS-42152 Do not use --force when tagging images

          SCM/JIRA link daemon added a comment - Code changed in jenkins User: Aaron Weiker Path: src/main/resources/org/jenkinsci/plugins/docker/workflow/Docker.groovy http://jenkins-ci.org/commit/docker-workflow-plugin/c961afc373ca9cb65051865e6d6651c1911f620d Log: JENKINS-42152 Do not use --force when tagging images

            Unassigned Unassigned
            wduda Wojciech Duda
            Votes:
            4 Vote for this issue
            Watchers:
            18 Start watching this issue

              Created:
              Updated: