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

docker.build() should sanitize input for newlines

XMLWordPrintable

      Using the following basic Jenkinsfile I accidentally broke all my docker.build() invocations in a very difficult to grok bug.

      node('docker') {
          /* Make sure we're starting with a pristine workspace */
          deleteDir()
      
          String imageName = 'jenkinsciinfra/terraform'
          String commitHash
          def container = null
      
          stage('Checkout') {
              checkout scm
              commitHash = sh(returnStdout: true, script: 'git rev-parse --short origin/master')
          }
      
          stage('Build') {
              container = docker.build("${imageName}:${commitHash}")
          }
      }
      

      This would fail with:

      [Pipeline] // stage
      [Pipeline] stage
      [Pipeline] { (Build)
      [Pipeline] sh
      [terraform] Running shell script
      + docker build -t jenkinsciinfra/terraform:f84fd39
      "docker build" requires exactly 1 argument(s).
      See 'docker build --help'.
      
      Usage:  docker build [OPTIONS] PATH | URL | -
      
      Build an image from a Dockerfile
      [Pipeline] }
      [Pipeline] // stage
      

      The problem here is that the commitHash has a newline character hidden at the end of it; the fix is obviously to add .trim(), but the fact that docker.build() doesn't sanitize the imageName parameter means that the user isn't provided any useful feedback when the invalid input breaks the command.

      I would suggest either sanitizing the imageName input, or run some validation to ensure that imageName is valid.

            Unassigned Unassigned
            rtyler R. Tyler Croy
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated: