How to create a Jenkins pipeline that builds a Docker image - docker

I'm new to Docker and Jenkins and I'm trying to create a Jenkins Pipeline that builds a Docker image.
I'm stuck when trying to build and keep receiving this error:
/var/jenkins_home/workspace/Docker-Pipeline#tmp/durable-a11b32f8/script.sh: line 1: docker: command not found
I've installed ubuntu on a VM.
Installed docker.
Installed jenkins/jenkins from dockerhub.
I followed this tutorial for the rest:
https://www.youtube.com/watch?v=z32yzy4TrKM&t=147s
I'm doing the exact same thing as him but it keeps failing.
Started by user admin
Obtained Jenkinsfile from git https://github.com/naorca/NodeApp.git
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start o
f Pipeline
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/Docker-Pipeline
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Clone repository)
[Pipeline] checkout
No credentials specified
> git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
> git config remote.origin.url https://github.com/naorca/NodeApp.git # timeout=10
Fetching upstream changes from https://github.com/naorca/NodeApp.git
> git --version # timeout=10
> git fetch --tags --progress https://github.com/naorca/NodeApp.git +refs/heads/*:refs/remotes/origin/*
> git rev-parse refs/remotes/origin/master^{commit} # timeout=10
> git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
Checking out Revision b74538f2f34b6c28306fcca8119e215b87124e5e (refs/remotes/origin/master)
> git config core.sparsecheckout # timeout=10
> git checkout -f b74538f2f34b6c28306fcca8119e215b87124e5e
Commit message: "Update Jenkinsfile"
> git rev-list --no-walk b74538f2f34b6c28306fcca8119e215b87124e5e # timeout=10
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Build image)
[Pipeline] sh
+ docker build -t naorca/nodeapp .
/var/jenkins_home/workspace/Docker-Pipeline#tmp/durable-a11b32f8/script.sh: line 1: docker: command not found
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 127
Finished: FAILURE

TL;DR: You must have Docker within your Jenkins Agent.
Following the process you described above, I got Jenkins up and running using the latest jenkins/jenkins image from Docker Hub. After looking over the container's file system, I confirmed what I had speculated about in my comment on your question: Docker is not installed within the Jenkins container. Assuming you are using the Jenkins Master server as your Agent for the pipeline job, you have a couple options that come to mind:
Extend the existing docker container -- using something like FROM jenkins/jenkins inside of a new docker file -- to include your dependencies.
Bind your existing docker daemon from the host into the run-time of the Jenkins container.
While I am partial to the first solution, I found an implementation of the second solution on the Docker Forums: "Using docker in a dockerized Jenkins container" I then gave that solution a try and can confirm that Docker is present for me in the Jenkins Master container after launching the Jenkins container with the following command:
docker run -v /var/run/docker.sock:/var/run/docker.sock \
-v $(which docker):$(which docker) \
-p 8000:8080 \
-p 50000:50000 \
jenkins/jenkins
I am not sure, but I would imagine there could be some negative security implications for the host of the Jenkins Master due to mounting its own Docker socket and Docker executable into the container; however, I would leave that up to someone more knowledgeable about Docker's internals to determine. Regardless, I can confirm that the solution above does work.

Related

Jenkins pipeline can't pull docker image

I am currently facing a situation whereby I can't pull a docker image during my Jenkins pipeline. Jenkins is running in a container as advised in the Jenkins Documentation
Whenever I run my pipeline, I come across the following error:
Started by user piii
Replayed #32
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/lms-portal
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Declarative: Checkout SCM)
[Pipeline] checkout
Selected Git installation does not exist. Using Default
The recommended git tool is: NONE
using credential 9d587d39-cf26-4eba-b08e-d9732a70a5b9
> git rev-parse --resolve-git-dir /var/jenkins_home/workspace/lms-portal/.git # timeout=10
Fetching changes from the remote Git repository
> git config remote.origin.url https://github.com/Piii-thagorus/lms_portal.git # timeout=10
Fetching upstream changes from https://github.com/Piii-thagorus/lms_portal.git
> git --version # timeout=10
> git --version # 'git version 2.30.2'
using GIT_SSH to set credentials jenksins_ssh
[INFO] SELinux is present on the host and we could not confirm that it does not apply actively: will try to relabel temporary files now; this may complain if context labeling not applicable after all
> /usr/bin/chcon --type=ssh_home_t /var/jenkins_home/workspace/lms-portal#tmp/jenkins-gitclient-ssh9208860798294607535.key
Verifying host key using known hosts file
You're using 'Known hosts file' strategy to verify ssh host keys, but your known_hosts file does not exist, please go to 'Manage Jenkins' -> 'Configure Global Security' -> 'Git Host Key Verification Configuration' and configure host key verification.
> git fetch --tags --force --progress -- https://github.com/Piii-thagorus/lms_portal.git +refs/heads/*:refs/remotes/origin/* # timeout=10
> git rev-parse refs/remotes/origin/testing^{commit} # timeout=10
Checking out Revision 3e0e59d57c873b5869628455c28fc82d66f1570f (refs/remotes/origin/testing)
> git config core.sparsecheckout # timeout=10
> git checkout -f 3e0e59d57c873b5869628455c28fc82d66f1570f # timeout=10
Commit message: "Removed docker as agent"
> git rev-list --no-walk 3e0e59d57c873b5869628455c28fc82d66f1570f # timeout=10
[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] isUnix
[Pipeline] withEnv
[Pipeline] {
[Pipeline] sh
+ docker inspect -f . node:latest
error during connect: Get "https://docker:2376/v1.24/containers/node:latest/json": dial tcp: lookup docker: no such host
[Pipeline] isUnix
[Pipeline] withEnv
[Pipeline] {
[Pipeline] sh
+ docker pull node:latest
error during connect: Post "https://docker:2376/v1.24/images/create?fromImage=node&tag=latest": dial tcp: lookup docker: no such host
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 1
Finished: FAILURE
Below is my pipeline, There
pipeline {
agent {
docker { image 'node:latest' }
}
stages {
stage('Prepare'){
steps{
sh 'echo "starting"'
}
}
}
}
What can be the cause of this? Any solution is appreciated.
Thank you
From what I understood you are running Jenkins as a container, and you want to run a build in which you require to pull a docker container.
If I am correct, let me make it clear.
You are running Jenkins as a docker container which means you do not have a local installation of Jenkins where you are running docker. Nor does Jenkins container run docker so you are trying to pull a docker container from where you do not have docker.
There is a work around for the same. You can install docker plugin and configure it.
Find the plugin documentation here
Also, you can try doing this.
There is a section in second hyperlink which shows how to configure docker agent before you use it.
Hope this helps,
Have a great day.

Jenkins K8s plugin WebSocket Timeout in container step

I have created a K3d cluster. I've Deployed a jenkins 2.319.1 controller inside, along with kubernetes plugin 1.31.1 (and git, pipeline and the like)
The idea is to run both the controller and the agents in the same cluster. To do so I've configured a cloud like in this picture:
[Cloud Configuration ][1]
[1]: https://i.stack.imgur.com/u91Fr.png
(I've done several attempts with different combinations for the agents to connect to the controller. Finally I've stayed with JNLP - NO WEBSOCKET - although, anyway, the timeout I'm about to describe is common to both of them)
With the cloud configured and being able to spawn the agents, after many attempts I finally discovered that the job was hanging (and dying of timeout) in the execution inside a container step. Actions within the default "jnlp" container are ok but the moment you do something as trivial as: sh 'ls -l' inside another container the job dies after 30 seconds with the following log:
> .
.
.
.
readOnly: false
nodeSelector:
kubernetes.io/os: "linux"
restartPolicy: "Never"
volumes:
- emptyDir:
medium: ""
name: "workspace-volume"
Running on prueba-6-tj9w5-r0qt9-kcst4 in /home/jenkins/agent/workspace/Prueba
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Get a Maven project)
[Pipeline] sh
+ git config --global http.proxy http://10.11x.xx.xx:8080
[Pipeline] sh
+ git config --global https.proxy http://10.11x.xx.xx:8080
[Pipeline] git
The recommended git tool is: NONE
No credentials specified
Cloning the remote Git repository
Cloning repository https://github.com/jenkinsci/kubernetes-plugin.git
> git init /home/jenkins/agent/workspace/Prueba # timeout=10
Fetching upstream changes from https://github.com/jenkinsci/kubernetes-plugin.git
> git --version # timeout=10
> git --version # 'git version 2.30.2'
> git fetch --tags --force --progress -- https://github.com/jenkinsci/kubernetes-plugin.git +refs/heads/*:refs/remotes/origin/* # timeout=10
Avoid second fetch
Checking out Revision a61114b275425489761f095e8a89b19cf2ab5c8e (refs/remotes/origin/master)
> git config remote.origin.url https://github.com/jenkinsci/kubernetes-plugin.git # timeout=10
> git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
> git rev-parse refs/remotes/origin/master^{commit} # timeout=10
> git config core.sparsecheckout # timeout=10
> git checkout -f a61114b275425489761f095e8a89b19cf2ab5c8e # timeout=10
> git branch -a -v --no-abbrev # timeout=10
> git checkout -b master a61114b275425489761f095e8a89b19cf2ab5c8e # timeout=10
Commit message: "[maven-release-plugin] prepare for next development iteration"
First time build. Skipping changelog.
[Pipeline] container
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Build a Maven project)
[Pipeline] sh
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // container
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // podTemplate
[Pipeline] End of Pipeline
java.io.IOException: Timed out waiting for websocket connection. You should increase the value of system property org.csanchez.jenkins.plugins.kubernetes.pipeline.ContainerExecDecorator.websocketConnectionTimeout currently set at 60 seconds
at org.csanchez.jenkins.plugins.kubernetes.pipeline.ContainerExecDecorator$1.doLaunch(ContainerExecDecorator.java:457)
at org.csanchez.jenkins.plugins.kubernetes.pipeline.ContainerExecDecorator$1.launch(ContainerExecDecorator.java:344)
at hudson.Launcher$ProcStarter.start(Launcher.java:507)
.
.
.
I don't know the websocket message as in the cloud configuration this is clearly unchecked. As I mention if I check websocket (along with clearing tunnel) I get the same result.
I don't know if this has anything to do with jenkins running inside a dockerized cluster. I have installed the same cluster (k3d v4.4.7) both in wsl2 in windows and also in rhel 7.9.
Sample pipeline used (this last one borrowed from ):
> podTemplate(containers: [
containerTemplate(name: 'maven', image: 'maven:3.3.9-jdk-8-alpine', ttyEnabled: true, command: 'cat'),
containerTemplate(name: 'golang', image: 'golang:1.8.0', ttyEnabled: true, command: 'cat')
]) {
node(POD_LABEL) {
env.http_proxy='http_proxy=http://10.11x.xx.xx:8080'
env.https_proxy='http_proxy=http://10.11x.xx.xx:8080'
stage('Get a Maven project') {
sh 'git config --global http.proxy http://10.11x.xx.xx:8080'
sh 'git config --global https.proxy http://10.11x.xx.xx:8080'
git 'https://github.com/jenkinsci/kubernetes-plugin.git'
container('maven') {
stage('Build a Maven project') {
sh 'mvn -B clean install'
}
}
}
stage('Get a Golang project') {
git url: 'https://github.com/hashicorp/terraform.git'
container('golang') {
stage('Build a Go project') {
sh """
mkdir -p /go/src/github.com/hashicorp
ln -s `pwd` /go/src/github.com/hashicorp/terraform
cd /go/src/github.com/hashicorp/terraform && make core-dev
"""
}
}
}
}
}
Regards
As mentioned in previous comment referring to post (Jenkins with Kubernetes Client Plugin - NoSuchMethodError) upgrading kubernetes plugin to v1.31.2 solved the problem. Already patched and tested.
KR

Local Jenkins install has no Docker rights

I installed Docker (19.03.3) and Jenkins (2.190.2 LTS from https://jenkins.io/download/) on my Ubuntu 19.04 machine. Then I created a pipeline that loads a Jenkinsfile from an example Git repository I created. Everything works well until Jenkins instructs Docker to pull an image. The response is that the system does not have permission to do so. I already added my system user to the docker group (sudo usermod -aG docker $USER) and successfully tested docker pull node:6-alpine in my terminal. But Jenkins still fails.
Anyone an idea what the problem is? I guess this is not very complicated and rather a typical configuration error.
Jenkins Pipeline Console Output:
Started by user John Doe
Obtained Jenkinsfile from git http://repo.myserver.com/john/example-app.git
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/lib/jenkins/workspace/example-app-builder
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Declarative: Checkout SCM)
[Pipeline] checkout
using credential jenkins
> git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
> git config remote.origin.url http://repo.myserver.com/john/example-app.git # timeout=10
Fetching upstream changes from http://repo.myserver.com/john/example-app.git
> git --version # timeout=10
using GIT_ASKPASS to set credentials Credentials for repo.myserver.com.
> git fetch --tags --force --progress -- http://repo.myserver.com/john/example-app.git +refs/heads/*:refs/remotes/origin/* # timeout=10
> git rev-parse refs/remotes/origin/master^{commit} # timeout=10
> git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
Checking out Revision f578983d6e153b3063e184c8df194dcff6ee39ab (refs/remotes/origin/master)
> git config core.sparsecheckout # timeout=10
> git checkout -f f578983d6e153b3063e184c8df194dcff6ee39ab # timeout=10
Commit message: "Add initial Jenkinsfile."
> git rev-list --no-walk f578983d6e153b3063e184c8df194dcff6ee39ab # timeout=10
[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] isUnix
[Pipeline] sh
+ docker inspect -f . node:6-alpine
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.40/containers/node:6-alpine/json: dial unix /var/run/docker.sock: connect: permission denied
[Pipeline] isUnix
[Pipeline] sh
+ docker pull node:6-alpine
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post http://%2Fvar%2Frun%2Fdocker.sock/v1.40/images/create?fromImage=node&tag=6-alpine: dial unix /var/run/docker.sock: connect: permission denied
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 1
Finished: FAILURE
Your Jenkins does not run with the dockers user. It runs with user jenkins. The command should therefore be the following in order to allow user jenkins to create Docker containers:
sudo usermod -aG docker jenkins

Jenkins Pipeline - ssh-agent can't find credentials

I have a task that's been working through the GUI as a Freestyle project. I'm trying to follow all of the instructions and documentation I can find to convert it to a Pipeline job, but I'm getting errors.
Here are the credentials I've created for the action.
I'm trying a fairly simple test to run a command on a remote Windows server.
#!/usr/bin/env groovy
pipeline {
agent any
stages {
stage("build") {
steps {
sshagent(credentials: ['1c0972a6-2bbf-4144-XXXX-XXXXXXXXXXXX']) {
sh """
ssh "dev user#XX.XX.XX.XX" su -c "powershell /project/getproj.bat | tee build.log"
"""
}
}
}
}
}
Finally, heres the output log.
Started by user XXXX XXXX
Obtained Jenkinsfile from git https://xx.xx.com/xxxx.git
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/Testing/xxxx
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Declarative: Checkout SCM)
[Pipeline] checkout
using credential 0d240009-1e30-4e3b-xxxx-xxxxxxxxxxxx
> git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
> git config remote.origin.url https://xx.xx.com/xxxx.git # timeout=10
Fetching upstream changes from https://xx.xx.com/xxxx.git
> git --version # timeout=10
using GIT_ASKPASS to set credentials
> git fetch --tags --progress https://xx.xx.com/xxxx.git +refs/heads/*:refs/remotes/origin/*
> git rev-parse refs/remotes/origin/convert_jenkinsfile^{commit} # timeout=10
> git rev-parse refs/remotes/origin/origin/convert_jenkinsfile^{commit} # timeout=10
Checking out Revision 40a510567b52ce621cb6590ab233289cb1948ad4 (refs/remotes/origin/convert_jenkinsfile)
> git config core.sparsecheckout # timeout=10
> git checkout -f 40a510567b52ce621cb6590ab233289cb1948ad4
Commit message: "Update Jenkinsfile"
> git rev-list --no-walk 6325e08c341a4e7e0cec8538640bd3d6cf6941fa # timeout=10
[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (build)
[Pipeline] sshagent
FATAL: [ssh-agent] Could not find specified credentials
[ssh-agent] Looking for ssh-agent implementation...
[ssh-agent] Exec ssh-agent (binary ssh-agent on a remote machine)
$ ssh-agent
SSH_AUTH_SOCK=/tmp/ssh-pRQqteq1L7US/agent.49961
SSH_AGENT_PID=49964
[ssh-agent] Started.
[Pipeline] {
[Pipeline] sh
+ ssh dev user#xx.xx.xx.xx su -c powershell /project/getproj.bat | tee build.log
Host key verification failed.
[Pipeline] }
$ ssh-agent -k
unset SSH_AUTH_SOCK;
unset SSH_AGENT_PID;
echo Agent pid 49964 killed;
[ssh-agent] Stopped.
[Pipeline] // sshagent
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 255
Finished: FAILURE
I can't tell if I'm doing something wrong with how I've identified the credentials, or there's something wrong in the Jenkinsfile. I'm intending to run multiple commands on the remote Windows server.
The ssh-agent plugin does not support user/password credentials.
It is not so easy to spot this information, but you can find that the plugin documentation says 'Note that only Private Key based credentials can be used.'
If you specify a credentials id referring to credentials of kind "Username with password", you always get "FATAL: [ssh-agent] Could not find specified credentials"
This might be related to JENKINS-32101. Please try the alternative syntax sshagent(['1c0972a6-2bbf-4144-XXXX-XXXXXXXXXXXX']) {...}.

Jenkins ansible plugin can't find executable

I'm running a Jenkins on Amazon EC2--the master in a Docker container and an agent on a separate box. My playbook executes an Ansible script, using the Jenkins Ansible plugin.
I had to install a new version of Ansible on the agent. I installed Ansible from git using the Running from Source instructions, and installed to /home/ec2-user/ansible. If I ssh to the agent and run which ansible I get ~/ansible/bin/ansible as expected. I entered /home/ec2-user/ansible/bin in the 'Ansible executables directory' for my new install, at the Manage Jenkins > Global Tool Configuration page.
When I run my Jenkins pipeline, however, I get this:
Running on docker-agent-1 in /home/ec2-user/jenkins/workspace/planet-healthcare-pipeline
[Pipeline] {
[Pipeline] pwd
[Pipeline] stage
[Pipeline] { (Download source and capture commit ID)
[Pipeline] sh
[planet-healthcare-pipeline] Running shell script
+ which ansible
which: no ansible in (/usr/local/bin:/bin:/usr/bin)
It says it's running on docker-agent-1 (which is the name of my agent), and I can see Ansible if I ssh there. Why can't Jenkins find the ansible executable?
UPDATE: After adding PATH as an environment variable, it can find Ansible, but now something else breaks. Here's the new output:
Running on docker-agent-1 in /home/ec2-user/jenkins/workspace/planet-healthcare-pipeline
[Pipeline] {
[Pipeline] pwd
[Pipeline] stage
[Pipeline] { (Download source and capture commit ID)
[Pipeline] sh
[planet-healthcare-pipeline] Running shell script
+ which ansible
/home/ec2-user/ansible/bin/ansible
[Pipeline] sh
[planet-healthcare-pipeline] Running shell script
+ ansible --version
ansible 2.2.0 (devel 1975a545bd) last updated 2016/09/20 16:19:06 (GMT +000)
lib/ansible/modules/core: (detached HEAD 70d4ff8e38) last updated 2016/09/20 16:19:08 (GMT +000)
lib/ansible/modules/extras: (detached HEAD db7a3f48e1) last updated 2016/09/20 16:19:09 (GMT +000)
config file = /home/ec2-user/jenkins/workspace/planet-healthcare-pipeline/ansible.cfg
configured module search path = Default w/o overrides
[Pipeline] git
> git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
> git config remote.origin.url git#bitbucket.org:planetgroup/planethealthcareportal.git # timeout=10
Fetching upstream changes from git#bitbucket.org:planetgroup/planethealthcareportal.git
> git --version # timeout=10
using GIT_SSH to set credentials Deployment key for Planet Healthcare Portal
> git fetch --tags --progress git#bitbucket.org:planetgroup/planethealthcareportal.git +refs/heads/*:refs/remotes/origin/*
> git rev-parse refs/remotes/origin/develop^{commit} # timeout=10
> git rev-parse refs/remotes/origin/origin/develop^{commit} # timeout=10
Checking out Revision e69608a15c9d433e2a22824c7e607048332a4160 (refs/remotes/origin/develop)
> git config core.sparsecheckout # timeout=10
> git checkout -f e69608a15c9d433e2a22824c7e607048332a4160
> git branch -a -v --no-abbrev # timeout=10
> git branch -D develop # timeout=10
> git checkout -b develop e69608a15c9d433e2a22824c7e607048332a4160
> git rev-list e69608a15c9d433e2a22824c7e607048332a4160 # timeout=10
[Pipeline] sh
[planet-healthcare-pipeline] Running shell script
+ git rev-parse --verify HEAD
[Pipeline] readFile
[Pipeline] echo
Current commit ID: e69608a
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Copy application.yml to environment)
[Pipeline] withCredentials
[Pipeline] {
[Pipeline] sh
[planet-healthcare-pipeline] Running shell script
+ sudo cp **** config/application.yml
[Pipeline] }
[Pipeline] // withCredentials
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Build image)
[Pipeline] sh
[planet-healthcare-pipeline] Running shell script
+ docker build -t planethealthcare/portal_app .
Sending build context to Docker daemon 557.1 kB
Sending build context to Docker daemon 1.114 MB
Sending build context to Docker daemon 1.671 MB
Sending build context to Docker daemon 2.228 MB
Sending build context to Docker daemon 2.785 MB
Sending build context to Docker daemon 3.342 MB
Sending build context to Docker daemon 3.398 MB
Step 1 : FROM ruby:2.3
---> 7b66156f376c
Step 2 : MAINTAINER David Ham <dham#uxfactory.com>
---> Using cache
---> 47f6f577f049
Step 3 : RUN apt-get update && apt-get install -y build-essential curl gstreamer1.0-plugins-base gstreamer1.0-tools gstreamer1.0-x libqt5webkit5-dev qt5-default xvfb && apt-get clean && rm -rf /var/lib/apt/lists/* && mkdir -p /app
---> Using cache
---> 38c1313e574d
Step 4 : WORKDIR /app
---> Using cache
---> 75a023d99fce
Step 5 : COPY Gemfile Gemfile.lock ./
---> Using cache
---> c39c81496a6b
Step 6 : ENV QMAKE /usr/bin/qmake
---> Using cache
---> 3226bf5f4e63
Step 7 : RUN bundle install --retry 20
---> Using cache
---> 91cb9908d53a
Step 8 : COPY . ./
---> 7330a8f5ba7c
Removing intermediate container bd55b7deddaf
Step 9 : EXPOSE 3000
---> Running in 76e6418e2b3f
---> 81427ffb31f5
Removing intermediate container 76e6418e2b3f
Step 10 : CMD bundle exec rails server
---> Running in c2a90c3c59f6
---> 15ab02b3ab8d
Removing intermediate container c2a90c3c59f6
Successfully built 15ab02b3ab8d
[Pipeline] dockerFingerprintFrom
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Run test suite)
[Pipeline] sh
[planet-healthcare-pipeline] Running shell script
+ docker run -d -p 5432:5432 -e POSTGRES_PASSWORD=postgres -e POSTGRES_DB=phc_portal_test postgres:9.5
[Pipeline] dockerFingerprintRun
[Pipeline] sh
[planet-healthcare-pipeline] Running shell script
+ docker inspect -f . planethealthcare/portal_app
.
[Pipeline] withDockerContainer
$ docker run -t -d -u 500:500 --link 85511ce90ce11c24818ae63bbbf7ab47745be7d96807d450b4adebd4c3196c5e:postgres -p 3000:3000 -e RAILS_ENV=test -w /home/ec2-user/jenkins/workspace/planet-healthcare-pipeline -v /home/ec2-user/jenkins/workspace/planet-healthcare-pipeline:/home/ec2-user/jenkins/workspace/planet-healthcare-pipeline:rw -v /home/ec2-user/jenkins/workspace/planet-healthcare-pipeline#tmp:/home/ec2-user/jenkins/workspace/planet-healthcare-pipeline#tmp:rw -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** --entrypoint cat planethealthcare/portal_app
[Pipeline] {
[Pipeline] echo
running tests...
[Pipeline] sh
[planet-healthcare-pipeline] Running shell script
+ rails db:migrate
/home/ec2-user/jenkins/workspace/planet-healthcare-pipeline#tmp/durable-32785ba4/script.sh: 2: /home/ec2-user/jenkins/workspace/planet-healthcare-pipeline#tmp/durable-32785ba4/script.sh: rails: not found
[Pipeline] }
$ docker stop 3acf37726ce1061d2e0f6e8d0cec882c707b42e710916636b17aaece4f516f2d
$ docker rm -f 3acf37726ce1061d2e0f6e8d0cec882c707b42e710916636b17aaece4f516f2d
[Pipeline] // withDockerContainer
[Pipeline] sh
[planet-healthcare-pipeline] Running shell script
+ docker stop 85511ce90ce11c24818ae63bbbf7ab47745be7d96807d450b4adebd4c3196c5e
85511ce90ce11c24818ae63bbbf7ab47745be7d96807d450b4adebd4c3196c5e
+ docker rm -f 85511ce90ce11c24818ae63bbbf7ab47745be7d96807d450b4adebd4c3196c5e
85511ce90ce11c24818ae63bbbf7ab47745be7d96807d450b4adebd4c3196c5e
[Pipeline] }
[Pipeline] // stage
[Pipeline] mail
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 127
Finished: FAILURE
And here's the pipeline:
node('docker') {
currentBuild.result = "SUCCESS"
try{
def git_commit = ""
def workspace = pwd()
def APPLICATION_YML
def image
stage("Download source and capture commit ID") {
sh "which ansible"
sh "ansible --version"
// Download source
git branch: 'develop', credentialsId: 'b96345a1-543c-4ccd-9a86-deca7203625c', url: 'git#bitbucket.org:planetgroup/planethealthcareportal.git'
// Get the commit ID
sh 'git rev-parse --verify HEAD > GIT_COMMIT'
git_commit = readFile('GIT_COMMIT').take(7)
echo "Current commit ID: ${git_commit}"
}
stage("Copy application.yml to environment"){
// write the application.yml to a file
withCredentials([[$class: 'FileBinding', credentialsId: '67dbd2e7-008f-4463-89a6-9645060e8ec8', variable: 'APPLICATION_YML']]) {
sh "sudo cp ${env.APPLICATION_YML} config/application.yml"
}
}
stage("Build image"){
image = docker.build "planethealthcare/portal_app"
}
stage("Run test suite"){
// start postgres
def postgres95 = docker.image('postgres:9.5')
postgres95.withRun("-p 5432:5432 -e POSTGRES_PASSWORD=postgres -e POSTGRES_DB=phc_portal_test"){ postgres ->
image.inside("--link ${postgres.id}:postgres -p 3000:3000 -e RAILS_ENV=test") {
echo "running tests..."
sh "rails db:migrate"
sh "rspec --tag ~pending"
sh "cucumber"
}
}
}
stage("Push to ECR registry"){
docker.withRegistry('https://0000000000.dkr.ecr.us-east-1.amazonaws.com', 'ecr:dham'){
image.push "${git_commit}"
image.push 'latest'
}
}
stage("Deploy app"){
// run the playbook
ansiblePlaybook([
colorized: true,
credentialsId: 'planet-healthcare',
installation: 'ansible-2-2-0',
inventory: 'staging',
playbook: 'deploy.yml',
extras: "--extra-vars 'app_build_id=${git_commit}''"
])
}
}
catch(err) {
currentBuild.result = "FAILURE"
mail body: "project build error: ${err}\n\n\n ${currentBuild.description}" ,
subject: 'project build failed',
to: 'me#example.com'
throw err
}
}
It's failing in the "Run test suite" stage--it can't find rails to run rails db:migrate, even though I know it's in the container.
Why would setting PATH on the agent affect a script that happens inside a Docker container?
Do you execute which ansible in your script? It searches only defined PATHs.
And it seems /home/ec2-user/ansible/bin is not in /usr/local/bin:/bin:/usr/bin (from your output).
You may go to agent-node's settings in Jenkins and add PATH environment variable with $PATH:/home/ec2-user/ansible/bin value.

Resources