使用GitLab CI在本地运行测试?

Mat*_*oli 58 gitlab-ci

如果在GitLab CI上配置了GitLab项目,有没有办法在本地运行构建?

我不想把我的笔记本电脑变成一个构建"跑步者",我只是想利用Docker并.gitlab-ci.yml在本地运行测试(即它都是预配置的).这样做的另一个好处是我确信我在本地和CI上使用相同的环境.

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

elb*_*ire 60

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

gitlab-runner exec docker my-job-name
Run Code Online (Sandbox Code Playgroud)

请注意,您需要使用dockergitlab-runner在计算机上安装才能使其正常工作.

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

这是我目前用于本地测试的线路gitlab-runner:

gitlab-runner exec docker test --docker-volumes "/home/elboletaire/.ssh/id_rsa:/root/.ssh/id_rsa:ro"
Run Code Online (Sandbox Code Playgroud)

编辑:您可以避免--docker-volumes在默认情况下使用您的密钥设置添加一个/etc/gitlab-runner/config.toml.有关详细信息,请参阅官方文档.

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

   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
   list         List all configured runners
   run          run multi runner service
   register     register a new runner
   install      install service
   uninstall        uninstall service
   start        start service
   stop         stop service
   restart      restart service
   status       get status of a service
   run-single       start single runner
   unregister       unregister specific runner
   verify       verify all registered runners
   artifacts-downloader download and extract build artifacts (internal)
   artifacts-uploader   create and upload build artifacts (internal)
   cache-archiver   create and upload cache artifacts (internal)
   cache-extractor  download and extract cache artifacts (internal)
   help, h      Shows a list of commands or help for one command

GLOBAL OPTIONS:
   --debug          debug mode [$DEBUG]
   --log-level, -l "info"   Log level (options: debug, info, warn, error, fatal, panic)
   --cpuprofile         write cpu profile to file [$CPU_PROFILE]
   --help, -h           show help
   --version, -v        print the version
Run Code Online (Sandbox Code Playgroud)

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

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

  • “它永远不应该是在本地测试事物的方式”为什么?`gitlab-ci.yml` 就像一个预配置的 Docker 容器。正如我在我的问题中指出的那样,Travis 是可能的,而且效果很好:https://github.com/jolicode/JoliCi (4认同)
  • [`gitlab-runner exec`在GitLab 10.0之后被弃用](https://gitlab.com/gitlab-org/gitlab-runner/issues/2710),投票https://gitlab.com/gitlab-org/gitlab -runner/issues/2797在此之前支持替换 (4认同)
  • @KongJinJie,你可以执行`gitlab-runner exec docker --help`,你会看到所有选项。在您的情况下,对于环境变量,就像添加 `--env VARIABLE=value` 作为 gitlab-runner 的参数一样简单 (3认同)
  • 供任何希望使用它在本地测试的人参考 - `gitlab-runner exec` [不支持](https://docs.gitlab.com/runner/commands/#limitations-of-gitlab-runner-exec) Gitlab 的 CI 配置中的一整套内容,也许最值得注意的是 [`extends` 键](https://gitlab.com/gitlab-org/gitlab-runner/-/issues/3794)。许多其他内容不受支持,但也未在该页面上列出!因此,如果您使用这些功能来使 CI 配置更易于维护或可组合,那么“gitlab-runner”不会有帮助。 (3认同)
  • 你是错的老兄...... gitlab跑步者可以在本地执行,与JoliCI完全相同...... (2认同)
  • 感谢您指出@Alfageme,显然在开发出替代品之前他们不会弃用它,请参阅https://gitlab.com/gitlab-org/gitlab-runner/issues/2797#note_42944825 (2认同)

jsc*_*sse 34

我使用这种基于 docker 的方法。

0. 创建一个 git repo 来测试这个答案

mkdir my-git-project
cd my-git-project
git init
git commit --allow-empty -m"Initialize repo to showcase gitlab-runner locally."
Run Code Online (Sandbox Code Playgroud)

1. 进入你的 git 目录

cd my-git-project
Run Code Online (Sandbox Code Playgroud)

2. 创建一个 .gitlab-ci.yml

例子 .gitlab-ci.yml

image: alpine

test:
  script:
    - echo "Hello Gitlab-Runner"
Run Code Online (Sandbox Code Playgroud)

3.创建一个安装了项目目录的docker容器

docker run -d \
  --name gitlab-runner \
  --restart always \
  -v $PWD:$PWD \
  -v /var/run/docker.sock:/var/run/docker.sock \
  gitlab/gitlab-runner:latest
Run Code Online (Sandbox Code Playgroud)

(-d) 在后台运行容器并打印容器 ID

(--restart 总是)或不

(-v $PWD:$PWD) 挂载当前目录到容器当前目录

(-v /var/run/docker.sock:/var/run/docker.sock) 这使容器可以访问主机的 docker 套接字,以便它可以启动“兄弟容器”(例如 Alpine)。

(gitlab/gitlab-runner:latest) 来自 dockerhub 的最新可用镜像。

4. 执行

docker exec -it -w $PWD gitlab-runner gitlab-runner exec docker test
#                           ^           ^            ^     ^      ^
#                           |           |            |     |      |
#                          (a)         (b)          (c)   (d)    (e)
Run Code Online (Sandbox Code Playgroud)

(a) docker 容器的名称

(b) 在docker容器内执行命令“gitlab-runner”

(c)(d)(e) 使用“docker executer”运行 gitlab-runner 并运行名为“test”的作业

5. 印刷品

...
Executing "step_script" stage of the job script
$ echo "Hello Gitlab-Runner"
Hello Gitlab-Runner
Job succeeded
...
Run Code Online (Sandbox Code Playgroud)

注:该选手只能在工作COMMITED您的代码库的状态。未提交的更改将被忽略。例外.gitlab-ci.yml本身并没有被承诺考虑在内。

  • 这里非常有用的例子有助于理解它是如何工作的 (18认同)
  • 对于那些想要使用单个 docker run 命令的人: docker run --rm -w $PWD -v $PWD:$PWD -v /var/run/docker.sock:/var/run/docker.sock gitlab/gitlab-runner:最新的执行docker测试` (4认同)

Gle*_*mas 7

GitLab 运行程序似乎还无法在 Windows 上运行,并且有一个待解决的问题可以解决此问题

因此,与此同时,我将脚本代码移至 bash 脚本,我可以轻松地将其映射到本地运行的 docker 容器并执行。

在本例中,我想在工作中构建一个 docker 容器,因此我创建了一个脚本“build”:

#!/bin/bash

docker build --pull -t myimage:myversion .
Run Code Online (Sandbox Code Playgroud)

在我的 .gitlab-ci.yaml 中,我执行脚本:

image: docker:latest

services:
- docker:dind

before_script:
- apk add bash

build:
stage: build
script:
- chmod 755 build
- build
Run Code Online (Sandbox Code Playgroud)

要使用 powershell 在本地运行脚本,我可以启动所需的映像并将卷与源文件映射:

$containerId = docker run --privileged -d -v ${PWD}:/src docker:dind
Run Code Online (Sandbox Code Playgroud)

如果不存在则安装 bash:

docker exec $containerId apk add bash
Run Code Online (Sandbox Code Playgroud)

设置 bash 脚本的权限:

docker exec -it $containerId chmod 755 /src/build
Run Code Online (Sandbox Code Playgroud)

执行脚本:

docker exec -it --workdir /src $containerId bash -c 'build'
Run Code Online (Sandbox Code Playgroud)

然后停止容器:

docker stop $containerId
Run Code Online (Sandbox Code Playgroud)

最后清理容器:

docker container rm $containerId
Run Code Online (Sandbox Code Playgroud)


Fir*_*cow 7

我目前正在制作一个在本地工作的 gitlab runner。仍处于早期阶段,但最终它将变得非常重要。gitlab 似乎不想/没有时间做这个,所以你去吧。 https://github.com/firecow/gitlab-runner-local


小智 5

如果您使用 docker 映像运行 Gitlab:https://hub.docker.com/r/gitlab/gitlab-ce,则可以通过使用docker.sock卷选项公开本地来运行管道:-v /var/run/docker.sock:/var/run/docker.sock。将此选项添加到 Gitlab 容器将允许您的工作人员访问主机上的 docker 实例。


fru*_*ero 5

另一种方法是在您的电脑和服务器上同时安装一个本地构建工具。所以基本上,你的 .gitlab-ci.yml 基本上会调用你喜欢的构建工具。

这是我与 nuke.build 一起使用的示例 .gitlab-ci.yml:

stages:
    - build
    - test
    - pack

variables:
    TERM: "xterm" # Use Unix ASCII color codes on Nuke

before_script:
    - CHCP 65001  # Set correct code page to avoid charset issues

.job_template: &job_definition
  except:
    - tags

build:
    <<: *job_definition
    stage: build
    script:
        - "./build.ps1"

test:
    <<: *job_definition
    stage: test
    script:
        - "./build.ps1 test"
    variables:
        GIT_CHECKOUT: "false"

pack:
    <<: *job_definition
    stage: pack
    script:
        - "./build.ps1 pack"
    variables:
        GIT_CHECKOUT: "false"
    only:
        - master
    artifacts:
        paths:
            - output/
Run Code Online (Sandbox Code Playgroud)

在 nuke.build 中,我定义了 3 个目标,命名为 3 个阶段(构建、测试、打包)

通过这种方式,您有一个可重现的设置(所有其他东西都用您的构建工具配置),您可以直接测试构建工具的不同目标。

(我可以在需要时调用 .\build.ps1 、 .\build.ps1 test 和 .\build.ps1 pack )