<p>这里是 <a href="https://github.com/jolicode/JoliCi">如何使用 Docker 在本地运行 Travis 构建</a>的一个例子,我正在寻找与 GitLab 类似的东西。</p> 使用 GitLab CI 在本地运行测试?

请注意,此过程将使用您自己的计算机使用 docker 容器运行测试。此 不是用于定义自定义运行程序。要做到这一点,只需进入回购的 CI/CD 设置并阅读那里的文档。如果你想确保你的跑步者被执行,而不是来自 gitlab.com,那么给你的跑步者添加一个自定义的和唯一的标签,确保它只运行带标签的作业,并且标记所有你想让你的跑步者负责的作业。

151407 次浏览

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

我不想把我的笔记本变成一个构建“运行器”,我只是想利用 Docker 和 .gitlab-ci.yml在本地运行测试(也就是说,它们都是预先配置好的)。这样做的另一个优点是,我确信在本地和 CI 上使用相同的环境。

几个月前,使用 gitlab-runner就可以做到这一点:

gitlab-runner exec docker my-job-name

请注意,您需要在计算机上同时安装 码头gitlab-runner才能使其工作。

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

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

使用 gitlab-runner在本地进行测试:

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

下面是我目前使用 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 的运行器选项(如变量、卷、网络等)。

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

由于评论中的混乱,我在这里粘贴了 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
[...]
   

GLOBAL OPTIONS:
--debug          debug mode [$DEBUG]
[...]

由于评论中的混乱,我在这里粘贴了 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
[...]
   

GLOBAL OPTIONS:
--debug          debug mode [$DEBUG]
[...]

如您所见,exec命令指向 execute a build locally

如您所见,exec命令指向 execute a build locally

即使 有一个问题不赞成当前的 gitlab-runner exec行为,它的 结果被重新考虑了和一个新版本的更大的功能将取代目前的 exec 功能。

即使 有一个问题不赞成当前的 gitlab-runner exec行为,它的 结果被重新考虑了和一个新版本的更大的功能将取代目前的 exec 功能。

请注意,此过程将使用您自己的计算机使用 docker 容器运行测试。此 不是用于定义自定义运行程序。要做到这一点,只需进入回购的 CI/CD 设置并阅读那里的文档。如果你想确保你的跑步者被执行,而不是来自 gitlab.com,那么给你的跑步者添加一个自定义的和唯一的标签,确保它只运行带标签的作业,并且标记所有你想让你的跑步者负责的作业。

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

GitLab 运行器似乎还不能在 Windows 上运行,而且还有 解决这个悬而未决的问题

然后执行。

在这种情况下,我想在我的工作中构建一个 docker 容器,所以我创建了一个脚本‘ build’:

#!/bin/bash


docker build --pull -t myimage:myversion .

因此,与此同时,我将脚本代码移到 bash 脚本中,这样我就可以轻松地在本地运行 映射到码头集装箱并执行它。

在这种情况下,我想在我的工作中构建一个 docker 容器,所以我创建了一个脚本‘ build’:

#!/bin/bash


docker build --pull -t myimage:myversion .

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

image: docker:latest


services:
- docker:dind


before_script:
- apk add bash


build:
stage: build
script:
- chmod 755 build
- build

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

image: docker:latest


services:
- docker:dind


before_script:
- apk add bash


build:
stage: build
script:
- chmod 755 build
- build

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

$containerId = docker run --privileged -d -v ${PWD}:/src docker:dind

安装 bash 如果不存在:

docker exec $containerId apk add bash

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

$containerId = docker run --privileged -d -v ${PWD}:/src docker:dind

安装 bash 如果不存在:

docker exec $containerId apk add bash

设置 bash 脚本的权限:

docker exec -it $containerId chmod 755 /src/build

设置 bash 脚本的权限:

docker exec -it $containerId chmod 755 /src/build

执行脚本:

docker exec -it --workdir /src $containerId bash -c 'build'

执行脚本:

docker exec -it --workdir /src $containerId bash -c 'build'

然后停止容器:

docker stop $containerId

然后停止容器:

docker stop $containerId

最后清理集装箱:

docker container rm $containerId

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

$containerId = docker run --privileged -d -v ${PWD}:/src docker:dind

安装 bash 如果不存在:

docker exec $containerId apk add bash

我目前正在制作一个本地的 gitlab 运行器。

设置 bash 脚本的权限:

docker exec -it $containerId chmod 755 /src/build

执行脚本:

docker exec -it --workdir /src $containerId bash -c 'build'
虽然还处于早期阶段,但最终它将变得非常重要。

然后停止容器:

docker stop $containerId

最后清理集装箱:

docker container rm $containerId
Gitlab 似乎没有时间做这个,给你。

其思想是将检查命令保留在 .gitlab-ci.yml之外。我使用 Makefile来运行类似于 make check的程序,而我的 .gitlab-ci.yml运行的 make命令与我在提交之前在本地使用的检查各种事情的 make命令相同。
这样,您将拥有一个包含所有/大部分命令的位置(Makefile) ,而 .gitlab-ci.yml将只包含与 CI 相关的内容。

Https://github.com/firecow/gitlab-runner-local

我上传了

$ sudo apt-get install gitlab-runner
$ gitlab-runner exec shell build

我在 Windows 上使用 VSCodeWSL

Yaml

image: node:10.19.0 # https://hub.docker.com/_/node/
# image: node:latest


cache:
# untracked: true
key: project-name
# key: ${CI_COMMIT_REF_SLUG} # per branch
# key:
#   files:
#     - package-lock.json # only update cache when this file changes (not working) @jkr
paths:
- .npm/
- node_modules
- build


stages:
- prepare # prepares builds, makes build needed for testing
- test # uses test:build specifically @jkr
- build
- deploy


# before_install:


before_script:
- npm ci --cache .npm --prefer-offline


prepare:
stage: prepare
needs: []
script:
- npm install


test:
stage: test
needs: [prepare]
except:
- schedules
tags:
- linux
script:
- npm run build:dev
- npm run test:cicd-deps
- npm run test:cicd # runs puppeteer tests @jkr
artifacts:
reports:
junit: junit.xml
paths:
- coverage/


build-staging:
stage: build
needs: [prepare]
only:
- schedules
before_script:
- apt-get update && apt-get install -y zip
script:
- npm run build:stage
- zip -r build.zip build
# cache:
#   paths:
#     - build
#   <<: *global_cache
#   policy: push
artifacts:
paths:
- build.zip


deploy-dev:
stage: deploy
needs: [build-staging]
tags: [linux]
only:
- schedules
#   # - branches@gitlab-org/gitlab
before_script:
- apt-get update && apt-get install -y lftp
script:
# temporarily using 'verify-certificate no'
# for more on verify-certificate @jkr: https://www.versatilewebsolutions.com/blog/2014/04/lftp-ftps-and-certificate-verification.html
# variables do not work with 'single quotes' unless they are "'surrounded by doubles'"
- lftp -e "set ssl:verify-certificate no; open mediajackagency.com; user $LFTP_USERNAME $LFTP_PASSWORD; mirror --reverse --verbose build/ /var/www/domains/dev/clients/client/project/build/; bye"
# environment:
#   name: staging
#   url: http://dev.mediajackagency.com/clients/client/build
#   # url: https://stg2.client.co
when: manual
allow_failure: true


build-production:
stage: build
needs: [prepare]
only:
- schedules
before_script:
- apt-get update && apt-get install -y zip
script:
- npm run build
- zip -r build.zip build
# cache:
#   paths:
#     - build
#   <<: *global_cache
#   policy: push
artifacts:
paths:
- build.zip


deploy-client:
stage: deploy
needs: [build-production]
tags: [linux]
only:
- schedules
# - master
before_script:
- apt-get update && apt-get install -y lftp
script:
- sh deploy-prod
environment:
name: production
url: http://www.client.co
when: manual
allow_failure: true

Yaml

image: node:10.19.0 # https://hub.docker.com/_/node/
# image: node:latest


cache:
# untracked: true
key: project-name
# key: ${CI_COMMIT_REF_SLUG} # per branch
# key:
#   files:
#     - package-lock.json # only update cache when this file changes (not working) @jkr
paths:
- .npm/
- node_modules
- build


stages:
- prepare # prepares builds, makes build needed for testing
- test # uses test:build specifically @jkr
- build
- deploy


# before_install:


before_script:
- npm ci --cache .npm --prefer-offline


prepare:
stage: prepare
needs: []
script:
- npm install


test:
stage: test
needs: [prepare]
except:
- schedules
tags:
- linux
script:
- npm run build:dev
- npm run test:cicd-deps
- npm run test:cicd # runs puppeteer tests @jkr
artifacts:
reports:
junit: junit.xml
paths:
- coverage/


build-staging:
stage: build
needs: [prepare]
only:
- schedules
before_script:
- apt-get update && apt-get install -y zip
script:
- npm run build:stage
- zip -r build.zip build
# cache:
#   paths:
#     - build
#   <<: *global_cache
#   policy: push
artifacts:
paths:
- build.zip


deploy-dev:
stage: deploy
needs: [build-staging]
tags: [linux]
only:
- schedules
#   # - branches@gitlab-org/gitlab
before_script:
- apt-get update && apt-get install -y lftp
script:
# temporarily using 'verify-certificate no'
# for more on verify-certificate @jkr: https://www.versatilewebsolutions.com/blog/2014/04/lftp-ftps-and-certificate-verification.html
# variables do not work with 'single quotes' unless they are "'surrounded by doubles'"
- lftp -e "set ssl:verify-certificate no; open mediajackagency.com; user $LFTP_USERNAME $LFTP_PASSWORD; mirror --reverse --verbose build/ /var/www/domains/dev/clients/client/project/build/; bye"
# environment:
#   name: staging
#   url: http://dev.mediajackagency.com/clients/client/build
#   # url: https://stg2.client.co
when: manual
allow_failure: true


build-production:
stage: build
needs: [prepare]
only:
- schedules
before_script:
- apt-get update && apt-get install -y zip
script:
- npm run build
- zip -r build.zip build
# cache:
#   paths:
#     - build
#   <<: *global_cache
#   policy: push
artifacts:
paths:
- build.zip


deploy-client:
stage: deploy
needs: [build-production]
tags: [linux]
only:
- schedules
# - master
before_script:
- apt-get update && apt-get install -y lftp
script:
- sh deploy-prod
environment:
name: production
url: http://www.client.co
when: manual
allow_failure: true

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

我使用这种基于码头的方法:

- 名字是 Gitlab-Runner

编辑: 2022-10

总是重新开始

docker run --entrypoint bash --rm -w $PWD -v $PWD:$PWD -v /var/run/docker.sock:/var/run/docker.sock gitlab/gitlab-runner:latest -c 'git config --global --add safe.directory "*";gitlab-runner exec docker test'

- v $PWD: $PWD - v/var/run/docker.sock:/var/run/docker.sock Gitlab/gitlab-runner: 最新版本

对于所有 git 版本 > 2.35.2。您必须在容器中添加 safe.directory以避免 fatal: detected dubious ownership in repository at...。对于打补丁的 git 版本 < 2.35.2也是如此。老命令将不再工作。

细节

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

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."

(——总是重新启动) 还是算了

1. 进入 git 目录

cd my-git-project

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

例子 .gitlab-ci.yml

image: alpine


test:
script:
- echo "Hello Gitlab-Runner"

(- v $PWD: $PWD)将工作目录安装到容器的工作目录中-在 Windows 上,你可以将 dir 绑定到一个固定的位置,例如 -v ${PWD}:/opt/myapp。而且 $PWD只能在 powershell工作,而不能在 cmd工作

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

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

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

(——总是重新启动) 还是算了

(gitlab/gitlab-runner: 最新)只是 dockerhub 提供的最新图片。

(- v $PWD: $PWD)将工作目录安装到容器的工作目录中-在 Windows 上,你可以将 dir 绑定到一个固定的位置,例如 -v ${PWD}:/opt/myapp。而且 $PWD只能在 powershell工作,而不能在 cmd工作

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

(gitlab/gitlab-runner: 最新)只是 dockerhub 提供的最新图片。

4、执行

4、执行

避免 fatal: detected dubious ownership in repository at... 更多信息

docker exec -it -w $PWD gitlab-runner git config --global --add safe.directory "*"

避免 fatal: detected dubious ownership in repository at... 更多信息

docker exec -it -w $PWD gitlab-runner git config --global --add safe.directory "*"

实际执行

docker exec -it -w $PWD gitlab-runner gitlab-runner exec docker test
#                ^          ^           ^            ^     ^      ^
#                |          |           |            |     |      |
#               (a)        (b)         (c)          (d)   (e)    (f)

实际执行

docker exec -it -w $PWD gitlab-runner gitlab-runner exec docker test
#                ^          ^           ^            ^     ^      ^
#                |          |           |            |     |      |
#               (a)        (b)         (c)          (d)   (e)    (f)

(a)容器内的工作目录。

(a)容器内的工作目录。

(b)码头集装箱名称

(b)码头集装箱名称

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

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

(d)(e)(f)使用“ docker Execter”运行 gitlab-runner 并运行一个名为“ test”的作业

(d)(e)(f)使用“ docker Execter”运行 gitlab-runner 并运行一个名为“ test”的作业

5. 指纹

...
Executing "step_script" stage of the job script
$ echo "Hello Gitlab-Runner"
Hello Gitlab-Runner
Job succeeded
...

5. 指纹

...
Executing "step_script" stage of the job script
$ echo "Hello Gitlab-Runner"
Hello Gitlab-Runner
Job succeeded
...

注意: 运行程序将只在代码库的 忠诚状态下运行。未提交的更改将被忽略。例外: .gitlab-ci.yml本身并没有被考虑在内。

注意: 运行程序将只在代码库的 忠诚状态下运行。未提交的更改将被忽略。例外: .gitlab-ci.yml本身并没有被考虑在内。

注意: 本地运行有一些限制。

注意: 本地运行有一些限制。

我使用这种基于码头的方法:

编辑: 2022-10

docker run --entrypoint bash --rm -w $PWD -v $PWD:$PWD -v /var/run/docker.sock:/var/run/docker.sock gitlab/gitlab-runner:latest -c 'git config --global --add safe.directory "*";gitlab-runner exec docker test'

我已经编写了一个工具,可以在本地运行所有 GitLab-CI 作业,而无需提交或推送,只需使用 ci-toolbox my_job_name命令即可。

项目的 URL: https://gitlab.com/mbedsys/citbx4gitlab

对于所有 git 版本 > 2.35.2。您必须在容器中添加 safe.directory以避免 fatal: detected dubious ownership in repository at...。对于打补丁的 git 版本 < 2.35.2也是如此。老命令将不再工作。

/密码 >

细节

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."

1. 进入 git 目录

cd my-git-project

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

对于所有 git 版本 > 2.35.2。您必须在容器中添加 safe.directory以避免 fatal: detected dubious ownership in repository at...。对于打补丁的 git 版本 < 2.35.2也是如此。老命令将不再工作。

例子 .gitlab-ci.yml

image: alpine


test:
script:
- echo "Hello Gitlab-Runner"

细节

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."

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

1. 进入 git 目录

cd my-git-project

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

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

例子 .gitlab-ci.yml

image: alpine


test:
script:
- echo "Hello Gitlab-Runner"

(——总是重新启动) 还是算了

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

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

(- v $PWD: $PWD)将工作目录安装到容器的工作目录中-在 Windows 上,你可以将 dir 绑定到一个固定的位置,例如 -v ${PWD}:/opt/myapp。而且 $PWD只能在 powershell工作,而不能在 cmd工作

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

(——总是重新启动) 还是算了

(gitlab/gitlab-runner: 最新)只是 dockerhub 提供的最新图片。

4、执行

(- v $PWD: $PWD)将工作目录安装到容器的工作目录中-在 Windows 上,你可以将 dir 绑定到一个固定的位置,例如 -v ${PWD}:/opt/myapp。而且 $PWD只能在 powershell工作,而不能在 cmd工作

避免 fatal: detected dubious ownership in repository at... 更多信息

docker exec -it -w $PWD gitlab-runner git config --global --add safe.directory "*"

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

实际执行

docker exec -it -w $PWD gitlab-runner gitlab-runner exec docker test
#                ^          ^           ^            ^     ^      ^
#                |          |           |            |     |      |
#               (a)        (b)         (c)          (d)   (e)    (f)

(gitlab/gitlab-runner: 最新)只是 dockerhub 提供的最新图片。

4、执行

(a)容器内的工作目录。

避免 fatal: detected dubious ownership in repository at... 更多信息

docker exec -it -w $PWD gitlab-runner git config --global --add safe.directory "*"

(b)码头集装箱名称

实际执行

docker exec -it -w $PWD gitlab-runner gitlab-runner exec docker test
#                ^          ^           ^            ^     ^      ^
#                |          |           |            |     |      |
#               (a)        (b)         (c)          (d)   (e)    (f)

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

(a)容器内的工作目录。

(b)码头集装箱名称

(d)(e)(f)使用“ docker Execter”运行 gitlab-runner 并运行一个名为“ test”的作业

5. 指纹

...
Executing "step_script" stage of the job script
$ echo "Hello Gitlab-Runner"
Hello Gitlab-Runner
Job succeeded
...

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

(d)(e)(f)使用“ docker Execter”运行 gitlab-runner 并运行一个名为“ test”的作业

5. 指纹

...
Executing "step_script" stage of the job script
$ echo "Hello Gitlab-Runner"
Hello Gitlab-Runner
Job succeeded
...

注意: 运行程序将只在代码库的 忠诚状态下运行。未提交的更改将被忽略。例外: .gitlab-ci.yml本身并没有被考虑在内。

注意: 本地运行有一些限制。

注意: 本地运行有一些限制。

然后在你的项目中,你可以按照其他答案中提到的方式来执行:

docker exec -it -w $PWD runner gitlab-runner exec <commands>..