使用GitLab CI在本地运行测试? [英] Use GitLab CI to run tests locally?

查看:273
本文介绍了使用GitLab CI在本地运行测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果在GitLab CI上配置了GitLab项目,是否可以在本地运行构建?

If a GitLab project is configured on GitLab CI, is there a way to run the build locally?

我不想将笔记本电脑变成构建运行器",我只想利用Docker和.gitlab-ci.yml在本地运行测试(即,它们都已预先配置).这样做的另一个优点是,我确定我在本地和CI上都使用了相同的环境.

I don't want to turn my laptop into a build "runner", I just want to take advantage of Docker and .gitlab-ci.yml to run tests locally (i.e. it's all pre-configured). Another advantage of that is that I'm sure that I'm using the same environment locally and on CI.

这里是如何使用Docker在本地运行Travis构建的示例,我正在寻找与GitLab类似的东西. /p>

Here is an example of how to run Travis builds locally using Docker, I'm looking for something similar with GitLab.

推荐答案

几个月前,可以使用gitlab-runner:

gitlab-runner exec docker my-job-name

请注意,您同时需要 docker

Note that you need both docker and gitlab-runner installed on your computer to get this working.

您还需要在.gitlab-ci.yml文件中定义的image键.否则将无法正常工作.

You also need the image key defined in your .gitlab-ci.yml file. Otherwise won't work.

这是我当前使用gitlab-runner在本地进行测试的行:

Here's the line I currently use for testing locally using gitlab-runner:

gitlab-runner exec docker test --docker-volumes "/home/elboletaire/.ssh/id_rsa:/root/.ssh/id_rsa:ro"

注意:您可以避免在默认情况下在/etc/gitlab-runner/config.toml中添加带有键设置的--docker-volumes. 有关详细信息,请参阅官方文档.另外,使用gitlab-runner exec docker --help查看所有基于docker的运行器选项(如变量,卷,网络等).

Note: You can avoid adding a --docker-volumes with your key setting it by default in /etc/gitlab-runner/config.toml. See the official documentation for more details. Also, use gitlab-runner exec docker --help to see all docker-based runner options (like variables, volumes, networks, etc.).

由于注释中的混乱,我在此处粘贴了gitlab-runner --help结果,因此您可以看到gitlab-runner可以在本地进行构建:

Due to the confusion in the comments, I paste here the gitlab-runner --help result, so you can see that gitlab-runner can make builds locally:

   gitlab-runner --help
NAME:
   gitlab-runner - a GitLab Runner

USAGE:
   gitlab-runner [global options] command [command options] [arguments...]
   
VERSION:
   1.1.0~beta.135.g24365ee (24365ee)
   
AUTHOR(S):
   Kamil Trzciński <ayufan@ayufan.eu> 
   
COMMANDS:
   exec         execute a build locally
   [...]
   
GLOBAL OPTIONS:
   --debug          debug mode [$DEBUG]
   [...]

如您所见,exec命令用于execute a build locally.

即使仍然存在弃用当前gitlab-runner exec行为的问题,它最终被重新考虑,并且具有更多功能的版本将替换当前的exec功能.

Even though there was an issue to deprecate the current gitlab-runner exec behavior, it ended up being reconsidered and a new version with greater features will replace the current exec functionality.

请注意,此过程是使用您自己的计算机使用docker容器运行测试. 不是来定义自定义跑步者.为此,只需转到您的存储库的CI/CD设置并阅读那里的文档.如果您想确保执行者而不是gitlab.com上的执行者,请向其添加一个自定义且唯一的标记,确保其仅运行带标记的作业,并标记您希望由其负责的所有作业.

Note that this process is to use your own machine to run the tests using docker containers. This is not to define custom runners. To do so, just go to your repo's CI/CD settings and read the documentation there. If you wanna ensure your runner is executed instead of one from gitlab.com, add a custom and unique tag to your runner, ensure it only runs tagged jobs and tag all the jobs you want your runner to be responsible of.

这篇关于使用GitLab CI在本地运行测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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