部署 GitLab Runner

添加 helm 仓库

helm repo add gitlab https://charts.gitlab.io
helm search repo gitlab/gitlab-runner
# NAME                    CHART VERSION   APP VERSION     DESCRIPTION
# gitlab/gitlab-runner    0.42.0          15.1.0          GitLab Runner

修改可配置项

生成 values.yaml 文件

helm show values gitlab/gitlab-runner > values.yaml

修改 values.yaml 文件

gitlabUrl: 'https://git.todoit.tech/'
runnerRegistrationToken: 'd6eDLsZVeEEc2myAD1us' # token 如何获得,请参考下文
unregisterRunners: true
concurrent: 2
checkInterval: 5

rbac:
  create: true
  rules:
    - apiGroups: [''] #"" indicates the core API group
      resources: ['*']
      verbs: ['*']
    - apiGroups: ['networking.k8s.io']
      resources: ['ingresses']
      verbs: ['*']
    - apiGroups: ['apps']
      resources: ['deployments']
      verbs: ['*']
  clusterWideAccess: true
  serviceAccountName: gitlab-runner

runners: # 下面配置 minio 作为 GitLab Runner 共享缓存的 s3 替换方案,minio 具体配置参考下文
  config: |
    [[runners]]
      [runners.kubernetes]
        namespace = "{{.Release.Namespace}}"
        image = "ubuntu:18.04"
      [runners.cache]
        Type = "s3"
        Shared = true
        [runners.cache.s3]
          AccessKey = "minio"
          SecretKey = "minio123"
          BucketName = "gitlab"
          ServerAddress = "minio.todoit.tech"
  tags: 'k8s'
  helpers:
    cpuLimit: 200m
    memoryLimit: 256Mi
    cpuRequests: 100m
    memoryRequests: 128Mi
    # 不要忘了,runner 是跑在 arm64 架构上
    image: 'gitlab/gitlab-runner-helper:arm64-latest'
  serviceAccountName: gitlab-runner

如果你想知道最终生成的模版,可以使用 helm template 命令。

helm template -f values.yaml --namespace gitlab gitlab/gitlab-runner > gitlab-runner.yaml

如何获取 runnerRegistrationToken

README-2022-06-28-15-34-15

为 GitLab 配置 Minio

README-2022-06-28-16-24-29

部署 GitLab Runner

以下命令可以部署或更新 GitLab Runner

helm install -f ./values.yaml gitlab-runner gitlab/gitlab-runner -n gitlab --version 0.42.0

查看部署状态

helm status gitlab-runner -n gitlab

登录 GitLab,应该可以看到成功注册了 Runner

README-2022-06-28-15-54-44

测试

新建一个项目 ci-test,在项目中添加一个名为 .gitlab-ci.yml 的文件

# 设置执行镜像
image: busybox:latest

# 整个pipeline有两个stage
stages:
  - build
  - test

# 定义全局缓存,缓存的key来自分支信息,缓存位置是vendor文件夹
cache:
  key: ${CI_COMMIT_REF_SLUG}
  paths:
    - vendor/

before_script:
  - export
  - echo "Before script section"

after_script:
  - echo "After script section"

build1:
  stage: build
  tags:
    - k8s
  script:
    - echo "将内容写入缓存"
    - mkdir -p vendor
    - echo "build" > vendor/hello.txt

test1:
  stage: test
  tags:
    - k8s
  script:
    - echo "从缓存读取内容"
    - cat vendor/hello.txt

将该项目推送到 GitLab,Pipeline 就会被执行,缓存会被使用,如图:

README-2022-06-28-20-59-21

卸载

通过以下命令,可以卸载刚刚注册的 GitLab Runner

helm delete gitlab-runner -n gitlab

参考:

上次更新: