简体   繁体   中英

Jenkins using Docker: How to run tests?

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?

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.

Recommend to go for Option 2

Use jenkins job trigger to run jobs in your jenkins slave node instead of your jenkins container.

And use jenkins docker plugin to manage your docker containers which are DB, phpunit, treat them as jenkins slave node, it will be much easier.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM