fabric8io / jenkins-pipeline-library

a collection of reusable jenkins pipelines and pipeline functions
Apache License 2.0
276 stars 180 forks source link

Unable to run script.sh inside docker agent #193

Open tomdottom opened 7 years ago

tomdottom commented 7 years ago

When trying to run the following declarative pipeline:

pipeline {
    agent { docker 'alpine' }
    stages {
        stage('Test') {
            steps {
                sh('printenv')
            }
        }
    }
}

I get the error:

[Pipeline] {
[Pipeline] stage
[Pipeline] { (Declarative: Agent Setup)
[Pipeline] sh
[TmpTest] Running shell script
+ docker pull alpine
Using default tag: latest
latest: Pulling from library/alpine
Digest: sha256:1072e499f3f655a032e88542330cf75b02e7bdf673278f701d7ba61629ee3ebe
Status: Image is up to date for alpine:latest
[Pipeline] }
[Pipeline] // stage
[Pipeline] sh
[TmpTest] Running shell script
+ docker inspect -f . alpine
.
[Pipeline] withDockerContainer
Jenkins does not seem to be running inside a container
$ docker run -t -d -u 107:113 -w /var/lib/jenkins/workspace/TmpTest -v /var/lib/jenkins/workspace/TmpTest:/var/lib/jenkins/workspace/TmpTest:rw,z -v /var/lib/jenkins/workspace/TmpTest@tmp:/var/lib/jenkins/workspace/TmpTest@tmp:rw,z -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** --entrypoint cat alpine
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Provision Server)
[Pipeline] sh
[TmpTest] Running shell script
sh: /var/lib/jenkins/workspace/TmpTest@tmp/durable-1abfbc69/script.sh: not found
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
$ docker stop --time=1 db551b51404ba6305f68f9086320634eeea3d515be134e5e55b51c3c9f1eb568
$ docker rm -f db551b51404ba6305f68f9086320634eeea3d515be134e5e55b51c3c9f1eb568
[Pipeline] // withDockerContainer
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 127
Finished: FAILURE

When monitoring the pipelines @tmp directory whilst its running I can see script.sh created.

some system details

Jenkins running as a single node system which has docker installed.

Jenkins v2.60.1
(all plugins fully updated)
docker --version
Docker version 17.06.0-ce, build 02c1d87
AndresCidoncha commented 6 years ago

Same here, some fix? My config:

AndresCidoncha commented 6 years ago

@tomdottom The docker image must have inside the jenkins agent ready for run. In my case, a workaround is use a custom Dockerfile with base of https://hub.docker.com/r/jenkinsci/slave/~/dockerfile/ and adding the things I need (docker for example). Once you have a jenkins agent running inside the container, the sh will exec correctly.

tomdottom commented 6 years ago

@AndresCidoncha thanks for the suggested solution.

bariman commented 6 years ago

@tomdottom do you have '/bin/bash' as Shell executable command on Jenkins->Configure System page? In this case '/bin/sh' should fix it.

jlccx-incontact commented 6 years ago

I had the same problem, and it was caused because the format of the bash script was incorrect. I noticed it when I tried to run my script on my local computer and I saw the error message:

/bin/sh^M: bad interpreter: No such file or directory.

After to fix this issue my script was executed successfully.