在 Docker 容器中运行 Jenkins 测试从代码库中的 dockerfile 构建 [英] Running Jenkins tests in Docker containers build from dockerfile in codebase

查看:35
本文介绍了在 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 提取 repo,构建容器并运行 shell 脚本来执行测试
  • 测试完成后,Jenkins 从其中一个容器中检索结果(通过共享卷)并处理结果.

我不希望 Jenkins 在容器中.

I do not want Jenkins to be in a container.

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

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 slave.我希望一切都尽可能动态,并尽可能减少对 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:

docker daemon 是否在此主机上运行?

Is the docker daemon running on this host?

一切都很好.

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

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"的容器中构建.运行后,您可以启动其他容器进行集成测试,或者将其与docker-compose"结合,将其安装在派生的 Jenkins 映像上.

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 构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆