Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run tests inside Docker container with Jenkins

We want to give it a try to setup CI/CD with Jenkins for our project. The project itself has Elasticsearch and PostgreSQL as runtime dependencies and Webdriver for acceptance testing. In dev environment, everything is set up within one docker-compose.yml file and we have acceptance.sh script to run acceptance tests.

After digging documentation I found that it's potentially possible to build CI with following steps:

  1. dockerize project
  2. pull project from git repo
  3. somehow pull docker-compose.yml and project Dockerfile - either:
    • put it in the project repo
    • put it in separate repo (this is how it's done now)
    • put somewhere on a server and jut copy it over
  4. execute docker-compose up
  5. project's Dockerfile will have ONBUILT section to run tests. Unit tests are run through mix tests and acceptance through scripts/acceptance.sh. It'll be cool to run them in parallel.
  6. shutdown docker-compose, clean up containers

Because this is my first experience with Jenkins a series of questions arise:

  • Is this a viable strategy?
  • How to connect tests output with Jenkins?
  • How to run and shut down docker-compose?
  • Do we need/want to write a pipeline for that? Will we need/want pipeline when we will get to the CD on the next stage?

Thanks

like image 942
lessless Avatar asked Jul 28 '16 10:07

lessless


People also ask

Can Jenkins run Docker container?

To run a Docker image, you must have Docker installed. Docker provides detailed instructions for installation on Linux, macOS, and Windows. Note that while recent versions of Windows gained native support for running Docker images, Jenkins only provides Linux based Docker images.

Can I run Docker inside a container?

To run docker inside docker, all you have to do it just run docker with the default Unix socket docker. sock as a volume. Just a word of caution: If your container gets access to docker. sock , it means it has more privileges over your docker daemon.


1 Answers

Is this a viable strategy?

Yes it is. I think it would be better to include the docker-compose.yml and Dockerfile in the project repo. That way any changes are tied to the version of code that uses the changes. If it's in an external repo it becomes a lot harder to change (unless you pin the git sha somehow , like using a submodule).

project's Dockerfile will have ONBUILD section to run tests

I would avoid this. Just set a different command to run the tests in a container, not at build time.

How to connect tests output with Jenkins?

Jenkins just uses the exit status from the build steps, so as long as the test script exits with a non-zero code on failure and a zero code on success that's all you need. Test output that is printed to stdout/stderr will be visible from jenkins console.

How to run and shut down docker-compose?

I would recommend this to run Compose:

docker-compose pull # if you use images from the hub, pull the latest version
docker-compose up --build -d

In a post-build step to shutdown:

docker-compose down --volumes

Do we need/want to write a pipeline for that?

No, I think just a single job is fine. Get it working with a simple setup first, and then you can figure out what you need to split into different jobs.

like image 92
dnephin Avatar answered Oct 15 '22 18:10

dnephin