ZaCH
ZaCH

Reputation: 31

Bash not found in Jenkins declarative pipeline

I have an issue with Jenkins not recognizing bash.

I run a stage with docker agent using a golang image. When trying to run an sh file, I receive this error:

+ ./build/docker/docker.sh /home/jenkins/workspace/003-CGI-App-setup-ACS-build-4ZEA3UO3JRWIGZRIGIBD7BRQJV7JJ4LAKTS4XDXT6N3GDVU7Z4WA@tmp/durable-46264c73/script.sh: line 1: ./build/docker/docker.sh: not found

This is my stage:

stage('Build & publish') {
        agent {
            docker {
                image 'golang:1.10-alpine3.7'
            }
        }
        steps {
            checkout scm
            unstash 'war'
            sh 'export PATH=/bin/bash:$PATH'
            sh 'cp XXX/XXX/target/*.war build/docker/ROOT.war'
            sh 'chmod 777 ./build/docker/docker.sh'
            sh 'go build -o XXX'
            sh './XXX build'
            sh './build/docker/docker.sh'
        }
    }

My docker.sh file starts with #!/bin/bash as well.

Does anyone know if it's related to the docker image? If yes, am I adding bash correctly?

Thanks.

Upvotes: 3

Views: 5758

Answers (1)

David Maze
David Maze

Reputation: 159830

Many lighter-weight Docker images, especially those built on top of Alpine Linux, don't contain GNU Bash.

The best solution here is to rewrite your script to not specifically require bash. Change its shebang line to #!/bin/sh, and limit its syntax to what's allowed in the POSIX shell specification. This is more than sufficient for most straightforward "do A, B, C, and then D" style scripts; if you find yourself reaching for bash-specific features like arrays, consider a more powerful scripting language like Python.

Specifically in the single line

sh 'export PATH=/bin/bash:$PATH'
  • Each sh command runs in its own execution environment with a clean environment, so environment variable settings from an earlier sh statement don't affect a later one; this statement is a no-op.
  • Elements of $PATH are directories, not individual files.
  • /bin is all but required to be on $PATH and it is in all standard execution environments. (If /bin/bash exists, then explicit bash -c '...' commands and #!/bin/bash shebang lines will find it.)

(The other frequent cause of problems like this is mixed Windows/Linux environments where the actual shebang line ends with a DOS-style CR/LF two-character newline and the environment can't run /bin/bash\r.)

Upvotes: 6

Related Questions