Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins using Docker: How to run tests?

Tags:

docker

jenkins

I am creating a Jenkins test environment using Docker for CI. I have a container with Jenkins installed and all the jobs moved from my previous Jenkins. Now I am stuck with this issue where I need to run tests that require DB and PHPUnit.

I don't want to install these in my Jenkins container as I have dedicated containers for DB and PHPUnit. So my question is, how can I trigger the Jenkins job to execute the tests in the Docker containing the necessary prerequisites?

I have two options but not sure if they are feasible.

Option 1:

When you run the job in Jenkins, trigger docker run [container with all dependencies][script to run the test] But I'm not sure if we can trigger docker run from inside a container.

Option 2:

Create a new container and install Jenkins slave on that. Add that container in the master Jenkins and run the test on the slave. Make sure the slave has links to the database and PHPUnit containers. Is this possible?

like image 342
Karthik Avatar asked Aug 04 '14 07:08

Karthik


People also ask

Can Docker be used for testing?

Docker Hub can automatically test changes to your source code repositories using containers. You can enable Autotest on any Docker Hub repository to run tests on each pull request to the source code repository to create a continuous integration testing service.

Can we use Jenkins and Docker together?

Combining Jenkins and Docker together can bring improved speed and consistency to your automation tasks, which is why we've collected some hopefully helpful resources on this page to get you started!


1 Answers

I'm not sure I will answer your question but in Jenkins Declarative Pipeline https://jenkins.io/doc/book/pipeline/syntax/ you may easily run docker containers on which you may want to execute your technology specific steps like

php --version

If your Jenkins is running as a Docker container, you may want to extend this image with Docker client and that allows to connect to host Docker and spawn new Docker containers as Jenkins container siblings not childs. For that you need to point docker.sock to that from host assigning volumes on docker run like this:

docker run -v /var/run/docker.sock:/var/run/docker.sock 

Full description of such solution you may find in this blog post: https://jpetazzo.github.io/2015/09/03/do-not-use-docker-in-docker-for-ci/

Simple pipeline for running docker with php and running php --version looks like this.

pipeline {
    agent { docker 'php' }
    stages {
        stage('build') {
            steps {
                sh 'php --version'
            }
        }
    }
}

Example found here https://jenkins.io/doc/pipeline/tour/hello-world/

Hope that helps a bit.

like image 176
Łukasz Gawron Avatar answered Oct 02 '22 21:10

Łukasz Gawron