gitlab-runner配置与注册

Posted w329636271

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了gitlab-runner配置与注册相关的知识,希望对你有一定的参考价值。

(1)启动gitlab-runner

#创建volume
docker volume create gitlab-runner-my-config
#docker启动runner 
docker run -d --name gitlab-my-runner --restart always --net=host \\
    -v /var/run/docker.sock:/var/run/docker.sock \\
    -v gitlab-runner-my-config:/etc/gitlab-runner \\
    gitlab/gitlab-runner:latest
#查看挂载状态
docker inspect gitlab-my-runner
#版本查看
$ docker exec -it gitlab-my-runner gitlab-runner -version 
Version:      15.2.1
Git revision: 32fc1585
Git branch:   15-2-stable
GO version:   go1.17.9
Built:        2022-07-26T21:21:20+0000
OS/Arch:      linux/amd64

(2)注册gitlab-runner 到git服务器上
查看注册token
root登录->菜单->管理员->概览->Runner->注册一个实例runner
路径http://gitlab.example.com/admin/runners

此处的url为gitlab的ip或者域名;registration-token为界面查询到的注册令牌token;

docker exec gitlab-my-runner gitlab-runner register -n \\
       --url http://192.168.188.134/ \\
       --registration-token Tz7Uspg1dZ1sLYsb-qUf \\
       --tag-list runInDk \\
       --executor docker \\
       --docker-image docker \\
       --docker-volumes /root/.m2:/root/.m2 \\
       --docker-volumes /root/.npm:/root/.npm \\
       --docker-volumes /var/run/docker.sock:/var/run/docker.sock \\
       --description "runInDk"

结果如下:

(3)查看注册工程的runner以及修改对应的配置;
root登录->修改运行未标记的作业;这样没有标签的作业也就可以运行;

(4)demo项目的运行

  1. 创建一个空表项目;
  2. 新建一个ci/cd
    ci/cd ->选择分支-》配置流水线->直接默认->点击提交
  3. 结果查看
    查看结果,问题为域名无法解析
Running with gitlab-runner 15.2.1 (32fc1585)
  on runInDk 6x_3_-dC
Preparing the "docker" executor
00:07
Using Docker executor with image docker ...
Pulling docker image docker ...
Using docker image sha256:87452597cd172fae96865ec2275a5c0ee110511a896d3b439aec6a92592fe9d9 for docker with digest docker@sha256:0c4066e2407fb67b5cb609aa2ea380cdd774445e6be1cd651096167963125af5 ...
Preparing environment
00:01
Running on runner-6x3-dc-project-5-concurrent-0 via localhost.localdomain...
Getting source from Git repository
00:02
Fetching changes with git depth set to 20...
Initialized empty Git repository in /builds/gitlab-instance-a7f43c36/test-my-runner/.git/
Created fresh repository.
fatal: unable to access 'http://gitlab.example.com/gitlab-instance-a7f43c36/test-my-runner.git/': Could not resolve host: gitlab.example.com
ERROR: Job failed: exit code 1

解决办法如下:
在容器的/etc/gitlab-runner/config.toml 中添加参数指定http://gitlab.example.com的ip(extra_hosts = [“gitlab.example.com:192.168.188.134”])
因为容器中没有vim工具,所以就在容器外面直接修改配置文件,修改完之后重启容器;步骤如下:

步骤一、查看在本地的映射位置:

docker volume inspect gitlab-runner-my-config

[
    
        "CreatedAt": "2022-08-16T16:01:14+08:00",
        "Driver": "local",
        "Labels": ,
        "Mountpoint": "/var/lib/docker/volumes/gitlab-runner-my-config/_data",
        "Name": "gitlab-runner-my-config",
        "Options": ,
        "Scope": "local"
    
]

步骤二、修改配置新增 extra_hosts = [“gitlab.example.com:192.168.188.134”]

[root@localhost .ssh]# cat /var/lib/docker/volumes/gitlab-runner-my-config/_data/config.toml
concurrent = 1
check_interval = 0

[session_server]
  session_timeout = 1800

[[runners]]
  name = "runInDk"
  url = "http://192.168.188.134/"
  token = "xxxxxx"
  executor = "docker"
  [runners.custom_build_dir]
  [runners.cache]
    [runners.cache.s3]
    [runners.cache.gcs]
    [runners.cache.azure]
  [runners.docker]
    tls_verify = false
    image = "docker"
    privileged = false
    disable_entrypoint_overwrite = false
    oom_kill_disable = false
    disable_cache = false
    volumes = ["/root/.m2:/root/.m2", "/root/.npm:/root/.npm", "/var/run/docker.sock:/var/run/docker.sock", "/cache"]
    shm_size = 0
    extra_hosts = ["gitlab.example.com:192.168.188.134"]

步骤三:容器重启

 docker restart gitlab-my-runner

再次执行查看执行结果。

問題:在老版本的gitlab上会报权限问题;如下

Preparing the "docker" executor
00:09
 ERROR: Failed to remove network for build
 ERROR: Preparation failed: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get "http://%2Fvar%2Frun%2Fdocker.sock/v1.24/info": dial unix /var/run/docker.sock: connect: permission denied (docker.go:863:0s)

解决办法在docker启动时候加上参数 --privileged=true

docker run -d --name gitlab-my-runner --restart always --net=host  --privileged=true \\
    -v /var/run/docker.sock:/var/run/docker.sock \\
    -v gitlab-runner-my-config:/etc/gitlab-runner \\
    gitlab/gitlab-runner:latest

报错 mkdir: cannot create directory ‘/root/.m2/repository’: Permission denied
Can not write to /root/.m2/copy_reference_file.log. Wrong volume permissions? Carrying on …
解决办法:
尝试修改宿主机的 /root/.m2无法解决问题,通过修改config.toml中的 privileged 改为 false,重启服务器,解决问题。

参考 https://blog.csdn.net/weixin_46152207/article/details/125439982

参考:Gitlab runner docker Could not resolve host
https://stackoverflow.com/questions/50325932/gitlab-runner-docker-could-not-resolve-host
https://docs.gitlab.com/runner/install/docker.html
https://docs.gitlab.com/runner/register/index.html#docker

docker exec gitlab-runner gitlab-runner register -n \\
       --url http://10.0.9.xx/ \\
       --registration-token xxxx \\
       --tag-list nicsp\\
       --executor docker \\
       --docker-image docker \\
       --docker-volumes /root/.m2:/root/.m2 \\
       --docker-volumes /root/.npm:/root/.npm \\
       --docker-volumes /var/run/docker.sock:/var/run/docker.sock \\
       --docker-volumes /root/.ssh:/root/.ssh:/root/.ssh:/root/.ssh \\
       --description "nicsp"

以上是关于gitlab-runner配置与注册的主要内容,如果未能解决你的问题,请参考以下文章

gitlab-runner配置与注册

docker中gitlab-runner配置

Gitlab-runner 安装

gitlab-runner安装

Docker注册GitLab-Runner报ERROR: Registering runner... failed。。。。: connect: no route to host

如何从gitlab-runner执行滚动更新