Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running Jenkins tests in Docker containers build from dockerfile in codebase

I want to deploy a continuous integration platform based on Jenkins. As I have various kinds of projects (PHP / Symfony, node, angular, …) and as I want these tests to run both locally and on Jenkins, I was thinking about using Dockers containers.

The process I’m aiming for is :

  • A merge request is opened on Github / Gitlab
  • A webhook notifies Jenkins of the merge request
  • Jenkins pulls the repo, builds the containers and runs a shell script to execute the tests
  • Once the tests are finished, Jenkins retrieves the results from one of the containers (through a shared volume) and process the results.

I do not want Jenkins to be in a container.

With this kind of process, I’m hoping to be able to run very easily the tests on each developer machine with something like a docker-composer up and then in one of the container ./tests all.

I’m not very familiar with Jenkins. I’ve read a lot of documentation, but most of them suggested to define Jenkins slaves for each kind of projects beforehand. I would like everything to be as dynamic as possible and require as less configuration on Jenkins as possible.

I would appreciate a description of your test process if you have ever implemented something similar. If you think what I’m aiming for is impossible, I would also appreciate if you could explain to me why.

like image 830
Julien Rouvier Avatar asked Feb 12 '17 13:02

Julien Rouvier


People also ask

Can I run Jenkins tests in a docker container?

You should now be able to set up Jenkins in a Docker container and create a pipeline that builds an image and runs tests inside it. Hopefully you will get into the habit of continuous integration, and I am sure it will improve your workflow. by Carl Riis @ carlriis.

How do I run JaCoCo tests in a docker container?

You just need to update the JaCoCo version in your pom.xml to ensure your tests work with JDK v15 or higher with <jacoco.version>0.8.6</jacoco.version>, so we can use the following Docker command to start the container and run tests: $ docker run -it --rm --name springboot-test java-docker ./mvnw test ...

How do I create a repository in Jenkins in Docker?

After selecting the Docker Build and Publish build step, fill in the Repository name with your_Dockerhub_id/desired_name_of_the_repo example kikiodazie/node-docker. Leave the remaining fields blank because even if you add your Docker Hub Registry credentials, Jenkins in Docker will not validate it.

How do I run unit tests inside a docker container?

Running unit tests inside a Docker container is more or less as building a project. First, I copy all my test projects inside the container using the COPY command: Next, I set the label test to true. I will need this label later to identify the right layer of the container to copy the test results out of it.


1 Answers

A setup I suggest is Docker in Docker.

The base is a derived Docker image, which extends the jenkins:2.x image by adding a Docker commandline client. The Jenkins is started as a container with its home folder (a folder e.g. /var/jenkins_home mounted from the Docker host) and the Docker socket file to be able to start Docker containers from Jenkins build jobs.

docker run -d --name jenkins -v /var/jenkins_home:/var/jenkins_home -v /var/run/docker.sock:/var/run/docker.sock ... <yourDerivedJenkinsImage>

To check, if this setup is working just execute following command after starting the Jenkins container:

docker exec jenkins docker version

If the "docker version" output does NOT show:

Is the docker daemon running on this host?

Everythin is fine.

In your build jobs, you could configure the process you mentioned above. Let Jenkins simply check out the repository. The repository should contain your build and test scripts.

Use a freestyle build job with a shell execution. A shell execution could look like this:

docker run --rm --volumes-from jenkins <yourImageToBuildAndTestTheProject> bash $WORKSPACE/<pathToYourProjectWithinTheGitRepository>/build.sh 

This command simply starts a new container (to build and/or test your project) with the volumes from jenkins. Which means that the cloned repository will be available under $WORKSPACE. So if you run "bash $WORKSPACE/<pathToYourProjectWithinTheGitRepository>/build.sh" your project will be built within a container of "yourImageToBuildAndTestTheProject". After running this, you could start other containers for integration tests or combine this with "docker-compose" by installing it on the derived Jenkins image.

Advantages are the minimal configuration affort you have within Jenkins - only the SCM configuration for cloning the GIT repository is required. Since each Jenkins job uses the Docker client directly you could use for each project one or Docker image to build and/or test, WITHOUT further Jenkins configuration.

If you need additional configuration e.g. SSH keys or Maven settings, just put them on the Docker host and start the Jenkins container with the additional volumes, which contain those configuration files.

Using this Docker option within the shell execution of your build jobs:

--volumes-from jenkins

Automatically adds workspace and configuration files to each of your build jobs.

like image 199
gclaussn Avatar answered Sep 28 '22 19:09

gclaussn