本文介绍了在Docker容器中运行Jenkins测试,从代码库中的dockerfile构建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想部署基于Jenkins的持续集成平台.由于我有各种项目(PHP/Symfony,node,angular等),并且我希望这些测试在本地和Jenkins上运行,所以我在考虑使用Dockers容器.

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.

我想要的过程是:

  • 在Github/Gitlab上打开合并请求
  • 一个Webhook通知Jenkins合并请求
  • Jenkins提取仓库,构建容器并运行Shell脚本以执行测试
  • 测试完成后,Jenkins从一个容器中检索结果(通过共享卷)并处理结果.

我不希望詹金斯(Jenkins)放在容器中.

I do not want Jenkins to be in a container.

通过这种过程,我希望能够使用docker-composer up之类的东西然后在其中一个容器./tests all中,非常容易地在每个开发人员机器上运行测试.

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.

我对詹金斯不太熟悉.我阅读了很多文档,但是大多数文档建议为每种项目预先定义Jenkins奴隶.我希望一切都尽可能地动态,并要求在Jenkins上进行尽可能少的配置.

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.

推荐答案

我建议的设置是Docker中的Docker.

A setup I suggest is Docker in Docker.

基础是派生的Docker映像,它扩展了 jenkins:2.x 通过添加Docker命令行客户端图像.Jenkins作为具有其起始文件夹(从Docker主机安装的文件夹/var/jenkins_home)和Docker套接字文件的容器启动,以便能够从Jenkins构建作业启动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>

要检查此设置是否有效,只需在启动Jenkins容器后执行以下命令:

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

docker exec jenkins docker version

如果"docker version"输出未显示:

If the "docker version" output does NOT show:

一切都很好.

在构建作业中,您可以配置上述过程.让詹金斯简单地检出存储库.该存储库应包含您的构建和测试脚本.

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.

在执行Shell时使用自由式构建作业. Shell执行可能看起来像这样:

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 

此命令仅使用jenkins的卷启动一个新容器(以构建和/或测​​试您的项目).这意味着克隆的存储库将在$ WORKSPACE下可用.因此,如果您运行"bash $ WORKSPACE/< pathToYourProjectWithinTheGitRepository>/build.sh",则您的项目将在"yourImageToBuildAndTestTheProject"容器中构建.运行此命令后,您可以启动其他容器进行集成测试,或者通过将其安装在派生的Jenkins映像上将其与"docker-compose"结合使用.

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.

优势是您在Jenkins中所需的最小配置工作-仅需要用于克隆GIT存储库的SCM配置.由于每个Jenkins作业都直接使用Docker客户端,因此您可以在每个项目中使用一个或Docker映像来构建和/或测​​试,没有进一步的Jenkins配置.

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.

如果您需要其他配置,例如SSH密钥或Maven设置,只需将它们放在Docker主机上,然后使用包含这些配置文件的其他卷启动Jenkins容器即可.

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.

在构建作业的Shell执行中使用此Docker选项:

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.

这篇关于在Docker容器中运行Jenkins测试,从代码库中的dockerfile构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 05:19