turnbullpress / dockerbook-code

The code and configuration examples from The Docker Book (http://www.dockerbook.com)
929 stars 580 forks source link

Chapter 5.3.3. Jenkins build creates spec/reports as root; causing subsequent builds to fail #44

Closed rsalgado closed 7 years ago

rsalgado commented 7 years ago

Just wanted to post this in case somebody had the same issue. As the description indicates, I get the following error in Jenkins (this is the relevant part):

...

hudson.plugins.git.GitException: Command "git checkout -f e8699fc5501b124fe2330c7f2f03ff0e08d64276" returned status code 128:
stdout: 
stderr: fatal: cannot unlink 'spec/reports/SPEC-These-are-sample-RSpec-tests.255.xml': Permission denied

...

Because the directory spec/reports/ is being created as root:

jenkins@ec3cb26d902b:~/jobs/Docker_test_job/workspace/spec$ ls -l
total 40
drwxr-xr-x 2 root    root    36864 Jul 26 02:23 reports
-rw-r--r-- 1 jenkins jenkins   110 Jul 26 01:37 sample_spec.rb

I managed to solve it by changing the following line in the execute shell in Jenkins, appending ; chown -R 1000 spec/reports (although I had to run chown manually the first time; also, removing the directory with sudo works too):

# Execute the build inside Docker.
CONTAINER=$(sudo docker run -d -v $MNT:/opt/project/ $IMAGE /bin/bash -c 'cd /opt/project/workspace && rake spec; chown -R 1000 spec/reports')

After that, the subsequent builds work fine. Don't know if there's a better way to deal with this. I assume this problem is caused by the temp container being run as root, but I'm still learning Docker, after all, so I'm not sure.

Thanks;

rsalgado commented 7 years ago

UPDATE Later on in the book, in the section 5.4.1, it is mentioned that:

Then, in the Build Environment section, we tick Delete workspace before build starts . This option cleans up our build environment by deleting the checked-out repository prior to initiating a new set of jobs.

Which effectively avoids this issue at all; maybe it could be mentioned in that section, too.

Sorry for the inconveniences, and thanks anyways.