gitlab-runner 配置使用

下载地址:https://packages.gitlab.com/runner/gitlab-runner/packages/el/7/gitlab-runner-13.7.0-1.x86_64.rpm

1.下载安装

rpm
wget https://packages.gitlab.com/runner/gitlab-runner/packages/el/7/gitlab-runner-13.7.0-1.x86_64.rpm/download.rpm

[root@bogon Trash]# rpm -ivh gitlab-runner-13.7.0-1.x86_64.rpm
[root@bogon Trash]# systemctl status gitlab-runner

2.GitLab Runner注册

2.1获取注册令牌

新建项目--设置--CI/CD--Runner

image.png

2.2 注册

[root@bogon ~]# gitlab-runner register
Runtime platform                                    arch=amd64 os=linux pid=56291 revision=943fc252 version=13.7.0
Running in system-mode.

Enter the GitLab instance URL (for example, https://gitlab.com/):
http://10.xx.xx.xx/
Enter the registration token:
rRg*****Rh2
Enter a description for the runner:
[bogon]: test runner        #描述,自定义
Enter tags for the runner (comma-separated):
my-test    #tag  类型名
Registering runner... succeeded                     runner=rRgrxDRm
Enter an executor: docker, docker-ssh, parallels, shell, ssh, custom, virtualbox, docker+machine, docker-ssh+machine, kubernetes:
shell    # 执行环境的类型
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!

注册后生成新的配置文件

[root@bogon ~]# cat /etc/gitlab-runner/config.toml
concurrent = 1
check_interval = 0

[session_server]
  session_timeout = 1800

[[runners]]
  name = "test runner"
  url = "http://10.xx.xx.xx/"
  token = "GG_x******UzabB"
  executor = "shell"
  [runners.custom_build_dir]
  [runners.cache]
    [runners.cache.s3]
    [runners.cache.gcs]
    [runners.cache.azure]

查看已注册的runner
[root@bogon ~]# gitlab-runner list

无法删除 runner
在使用 gitlab-runner unregister 删除 runner 时,提示 Error: unregistering runner from gitlab forbidden。使用 gitlab-runner verify 检测 runner 是否已连接至 gitlab ,然后gitlab-runner verify --delete在注册列表中删除它们。

deploy参考:https://blog.csdn.net/zyy247796143/article/details/123842374

3. 项目根目录下创建文件 .gitlab-ci.yml 并配置

stages:
  - build
  - cleanup
  - deploy

build:
  stage: build
  only:
    - master
  variables:
    VERSION: 1.0.10
  script:
    - echo "开始build..."
    - bash create_file.sh

.gitlab-ci.yml 配置参考: https://zhuanlan.zhihu.com/p/480549460

==========================================
创建runner 后任务被阻塞,提示

This job is stuck because the project doesn't have any runners online assigned to it.

settings-CICD--Runner 下勾选
Indicates whether this runner can pick jobs without tags.
(指示此runner是否可以选择无标记的作业)
或在.gitlab-ci.yml中配置tags

注:注册runners时可以指定多个标签 如:120-vm, test2 ;job可以通过标签精准选择runners

 选择标签有130-local和test1 的runners
job:
  tags:
    - 130-local
    - test1

===============================================
报错2

fatal: git fetch-pack: expected shallow list
fatal: The remote end hung up unexpectedly
ERROR: Job failed: exit status 1

git 版本过低,更新git

配置repo源:
yum install http://opensource.wandisco.com/centos/7/git/x86_64/wandisco-git-release-7-1.noarch.rpm
安装:yum -y install git

4. Install runner for container

官网: https://docs.gitlab.com/runner/install/docker.html

Option 1: Use local system volume mounts to start the Runner container

docker run -d --name gitlab-runner --restart always \
  -v /srv/gitlab-runner/config:/etc/gitlab-runner \
  -v /var/run/docker.sock:/var/run/docker.sock \
  gitlab/gitlab-runner:latest

runner注册,类型:docker

 进入到容器内执行
root@93b95372b6b9:/# gitlab-runner register
Runtime platform                                    arch=amd64 os=linux pid=170 revision=d540b510 version=15.9.1
Running in system-mode.

Enter the GitLab instance URL (for example, https://gitlab.com/):
http://10.xx.xx.120/
Enter the registration token:
pUy9rf5s_4v-uK1x-VRf
Enter a description for the runner:
[93b95372b6b9]: 130 docker
Enter tags for the runner (comma-separated):
130-docker
Enter optional maintenance note for the runner:
docker
WARNING: Support for registration tokens and runner parameters in the 'register' command has been deprecated in GitLab Runner 15.6 and will be replaced with support for authentication tokens. For more information, see https://gitlab.com/gitlab-org/gitlab/-/issues/380872
Registering runner... succeeded                     runner=pUy9rf5s
Enter an executor: docker, parallels, shell, virtualbox, docker+machine, docker-ssh+machine, kubernetes, custom, docker-ssh, ssh, instance:
docker
Enter the default Docker image (for example, ruby:2.7):
docker:latest
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!

Configuration (with the authentication token) was saved in "/etc/gitlab-runner/config.toml"

修改runner 配置文件:

[root@bogon ~]# cat  /srv/gitlab-runner/config/config.toml
concurrent = 1
check_interval = 0
shutdown_timeout = 0

[session_server]
  session_timeout = 1800

[[runners]]
  name = "130-docker"
  url = "http://10.xx.xx.120/"
  id = 10
  token = "euP******hqv_"
  token_obtained_at = 2023-02-24T02:47:50Z
  token_expires_at = 0001-01-01T00:00:00Z
  executor = "docker"
  [runners.cache]
    MaxUploadedArchiveSize = 0
  [runners.docker]
    tls_verify = false
    image = "docker:latest"
    privileged = false
    disable_entrypoint_overwrite = false
    oom_kill_disable = false
    disable_cache = false
    volumes = ["/var/run/docker.sock:/var/run/docker.sock","/cache"]  # 修改这行
    shm_size = 0

注:volumes = ["/var/run/docker.sock:/var/run/docker.sock","/cache"]
个人理解:exector使用docker,会在gitlab-runner容器中运行docker容器,上面volumes会将宿主机sock文件挂载docker容器内;同理如果docker容器内要执行命令kubelet、helm 连接到k8s集群,将kubeconfig做同样的挂载配置,选择参数 --kubeconfig

docker run -d --name gitlab-runner-k8s --restart always \
-v /srv/gitlab-runner/config:/etc/gitlab-runner \
-v /srv/k8s-config:/etc/k8s-config \
-v /var/run/docker.sock:/var/run/docker.sock \
gitlab/gitlab-runner:latest

配置.gitlab-ci.yml

variables:
  GLOBAL_VAR: "A global variable"
stages:
  - docker_info

job1:
  stage: docker_info
  variables:
    JOB_VAR: "A job variable"
  only:
    - /^master/
  tags:
    - 130-docker-first
  script:
    #- export
    - docker info

docker参考:https://zhuanlan.zhihu.com/p/597132392

gitlab ci文件拆分

将每个项目中的.gitlab-ci.yml 统一保存到template项目中

  1. 新建项目zdq-test/gitlab-ci-template

gitlab-ci-template/
├── .git
├── README.md
└── templates
└── .gitlab-ci-shell.yml

  1. 要在同一个GitLab实例上包含来自另一个私有项目的文件,请使用include:file。只能将include:file与include:project结合使用。使用相对于根目录(/)的完整路径。
# tag
include:
  - project: 'zdq-test/gitlab-ci-template'
    ref: v1.0
    file: '/templates/.gitlab-ci-shell.yml'
    
# branch
include:
  - project: 'zdq-test/gitlab-ci-template'
    ref: main
    file: '/templates/.gitlab-ci-shell.yml'

# include:local 引用当前项目下*.yml文件
include:
  - local: 'template/.gitlab-ci-docker.yml'

预设变量

stages:
  - docker_info

job1:
  stage: docker_info
  tags:
    - 130-docker
  variables:
    JOB_VAR: "A job variable"
  only:
    - master
  script:
    - export

官网:https://docs.gitlab.com/13.12/ee/ci/yaml/#include

rules使用

stages:
  - docker_info
job2:
  stage: docker_info
  tags:
    - 130-docker
  script:
    - echo "Variables are '$GLOBAL_VAR' and '$JOB_VAR'"
  rules:
    - if: '$DOMAIN == "example.com"'
      when: manual
    - changes:  # 下面文件有改变就执行when的条件
      - Dockerfile
      when: manual
    - exists:      # 下面文件存在就执行该job
      - Jenkinsfile

参考https://blog.csdn.net/clover661/article/details/123411439
参考文档:
//www.greatytc.com/p/92d5a3dc5aeb
https://mp.weixin.qq.com/mp/appmsgalbum?__biz=MzU2NjU5MjYwNw%3D%3D&action=getalbum&album_id=1320875769996050433#wechat_redirect

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容