简体   繁体   English

Jenkins 使用 Docker:如何运行测试?

[英]Jenkins using Docker: How to run tests?

I am creating a Jenkins test environment using Docker for CI.我正在使用 Docker for CI 创建一个 Jenkins 测试环境。 I have a container with Jenkins installed and all the jobs moved from my previous Jenkins.我有一个安装了 Jenkins 的容器,所有的工作都从我以前的 Jenkins 中转移了。 Now I am stuck with this issue where I need to run tests that require DB and PHPUnit.现在我遇到了这个问题,我需要运行需要 DB 和 PHPUnit 的测试。

I don't want to install these in my Jenkins container as I have dedicated containers for DB and PHPUnit.我不想将这些安装在我的 Jenkins 容器中,因为我有用于 DB 和 PHPUnit 的专用容器。 So my question is, how can I trigger the Jenkins job to execute the tests in the Docker containing the necessary prerequisites?所以我的问题是,如何触发 Jenkins 作业以在包含必要先决条件的 Docker 中执行测试?

I have two options but not sure if they are feasible.我有两个选择,但不确定它们是否可行。

Option 1:选项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.当您在 Jenkins 中运行作业时,触发docker run [具有所有依赖项的容器][运行测试的脚本]但我不确定我们是否可以从容器内部触发 d​​ocker run。

Option 2:选项 2:

Create a new container and install Jenkins slave on that.创建一个新容器并在其上安装 Jenkins slave。 Add that container in the master Jenkins and run the test on the slave.在主 Jenkins 中添加该容器并在从站上运行测试。 Make sure the slave has links to the database and PHPUnit containers.确保从站具有到数据库和 PHPUnit 容器的链接。 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我不确定我会回答您的问题,但在 Jenkins 声明式管道https://jenkins.io/doc/book/pipeline/syntax/ 中,您可以轻松运行 docker 容器,您可能希望在其上执行您的技术特定步骤,例如

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.如果您的 Jenkins 作为 Docker 容器运行,您可能希望使用 Docker 客户端扩展此映像,并允许连接到主机 Docker 并生成新的 Docker 容器作为 Jenkins 容器兄弟而不是子容器。 For that you need to point docker.sock to that from host assigning volumes on docker run like this:为此,您需要将 docker.sock 指向来自主机分配卷的 docker 运行,如下所示:

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/您可以在此博客文章中找到此类解决方案的完整说明: 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.使用 php 运行 docker 并运行php --version简单管道如下所示。

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

Example found here https://jenkins.io/doc/pipeline/tour/hello-world/此处找到的示例https://jenkins.io/doc/pipeline/tour/hello-world/

Hope that helps a bit.希望那些对你有帮助。

Recommend to go for Option 2建议去选项2

Use jenkins job trigger to run jobs in your jenkins slave node instead of your jenkins container.使用 jenkins 作业触发器在 jenkins 从节点而不是 jenkins 容器中运行作业。

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.并使用jenkins docker plugin来管理你的docker容器,即 DB、phpunit,将它们视为 jenkins 从节点,会容易得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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