-
Bug
-
Resolution: Unresolved
-
Minor
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.
I just spent several hours getting burned by this. The easiest fix is to just change Docker.groovy below to trim both the image and the args, but it's possible that you'd want to go further with args, since any embedded newlines would be an issue. In my case, image was the issue with the newlines. I'd be happy to make the change and submit a PR if folks are happy with the solution.