gitlab-jh 配置 CI 的问题

在运行有 gitlab-jh 的服务器上启用 runner 没有效果

要确保 runner 允许 untagged 的任务。gitlab-runner status 的确是会报错,但是不用管,对结果没有影响。

Runner 的配置

gitlab-runner 容器中的配置文件是 /etc/gitlab-runner/config.toml

concurrent = 4
check_interval = 0

[session_server]
  session_timeout = 1800

[runners]
  name = "xxx"
  url = "http://xxx.xxx.xxx.xxx:xxxx" # 要改成内网的,不然太慢,还受到外部服务器的 nginx 传输限制
  token = "xxxxxxxx"                  # 要改
  executor = "docker"
  [runners.custom_build_dir]
  [runners.cache]
    [runners.cache.s3]
    [runners.cache.gcs]
    [runners.cache.azure]
  [runners.docker]
    tls_verify = false
    image = "ubuntu" # default runner 其实不用改,因为 CI 任务可以指定不同的镜像
    privileged = false
    disable_entrypoint_overwrite = false
    oom_kill_disable = false
    disable_cache = false
    volumes = ["/cache"]           # 这里做路径映射,如果需要只读,用 ro 而不是 r
    shm_size = 0
    gpus = "all"                   # 没有这个就不能运行 GPU
    pull_policy = "if-not-present" # 没有这个每次都会检查镜像是否最新,对国内网络不友好

git fetch 的时候遇到 lock 文件存在

没有解决。删了也没成功。就把下载模式改成 git clone 了。

CI 文件中有向无环图的写法

默认情况下,CI 是按照阶段进行的,如果一个阶段中有不被允许失败的任务失败了,则整个阶段失败,当前阶段会继续运行完,但之后的阶段将无法运行。为了增加灵活性,Gitlab 提供了 needs 配置项,它允许我们将按阶段的任务依赖关系改造成有向无环图。

needs 只能依赖上一阶段或者本阶段的(GitLab 14.2),上上阶段是不能依赖的!

  • 如果 needs 为空,则不依赖任何任务。
  • 如果 needs 未被定义,则依赖上一阶段所有任务。

Tip

dependencies 配置项用来指明当前的任务需要哪些前面任务的结果(artifacts)。如果没有 dependencies,则会获得以前所有阶段的产品。如果定义了 needs,也要检查一下 dependencies 是否也需要被定义?

只对 merge request 跑 CI

Configuring pipelines for merge requests

总是花大量时间拉取远程镜像

https://forum.gitlab.com/t/runner-cant-use-local-docker-images/5507/6?u=87tjbg9

You should set pull_policy = "if-not-present" in your /etc/gitlab-runner/config.toml. By default, the runner always tries to pull the image from DockerHub.

官方文档 https://docs.gitlab.com/runner/executors/docker.html#configure-how-runners-pull-images

报错说上传 artifacts 太大

  • 将 gitlab 中的 nginx 配置的 client_max_body_size 改大。
  • 将 gitlab 管理员区域的最大 artifact 大小改大(可以按照实例、组、工程来改,但都只有管理员可以操作)。
  • 将 gitlab 中的 timeout 时间从 60s 改成 600s(puma['worker_timeout'] = 600)。文件太大在低带宽情况下传输慢,容易超时。

配置完成之后不要 gitlab-ctl restart,用 gitlab-ctl reconfigure 会更快一些。

如果要经过公网服务器中转,上传还受到公网服务器的 nginx 配置的限制。但是这个在本地服务器侧是调整不了的。

docker - I have changed my gitlab default url from 127.0.0.1 but upload artifacts still posts to it - Stack Overflow 修改 /etc/gitlab-runner/config.toml 中的 url,然后 gitlab-runner restart。改成了 http://172.17.0.1:xxxx,也就是本地 ip + 本地 nginx 端口(而不是远程的),注意,不能是 127.0.0.1,因为容器相当于另外一台机器。

docker exec -it gitlab-runner-1 /bin/bash
apt update && apt install iproute2 -y
ip route|awk '/default/ { print $3 }' # 查看宿主机的 ip

错误:fatal: shallow file has changed since we read it

这个是多个 runners 的并发问题。网上说要用不同的配置。

如果只要增大并发数,只用一个 runner(而不是创建多个),修改 concurrency 配置即可。

再用 gitlab-runner restart 重新加载一下配置。之前有错可能是因为几个 runners 都获得了一样的 runners 配置,有一样的名字、token 等?