目录
1. gitlab 笔记
1.1. GitLab Runner
GitLab Runner 是一个开源项目, 用于运行您的作业并将结果发送回 GitLab。它与 GitLab CI 一起使用, GitLab CI 是 GitLab 随附的开源持续集成服务, 用于协调作业。
GitLab Runner 是用 Go 编写, 可以作为单个二进制文件运行, 不需要语言特定的要求。
1.1.1. GitLab Runner 的三种类型
- shared: 运行整个平台项目的作业 (gitlab)
- group: 运行特定 group 下的所有项目的作业 (group)
- specific: 运行指定的项目作业 (project)
1.1.2. GitLab Runner 两种状态
- locked: 无法运行项目作业
- paused: 不会运行作业
1.1.3. GitLab Runner 安装
由于目前服务都上容器了, 因此这里只演示采用 docker 安装 GitLab Runner 的方法, 其他的方法可参考官网。
官网地址: https://docs.gitlab.com/runner/install/
$ mkdir -p /data/gitlab-runner/config
$ docker run -itd --restart=always --name gitlab-runner \
-v /data/gitlab-runner/config:/etc/gitlab-runner \
-v /var/run/docker.sock:/var/run/docker.sock gitlab/gitlab-runner:latest
$ docker exec -it gitlab-runner bash
root@24dc60abee0b:/# gitlab-runner -v
Version: 13.8.0
Git revision: 775dd39d
Git branch: 13-8-stable
GO version: go1.13.8
Built: 2021-01-20T13:32:47+0000
OS/Arch: linux/amd64
1.1.4. GitLab Runner 注册
注意: 注册 gitlab-runner 的前提是必须有一个可以使用的 gitlab 仓库
点击用户管理–左边点击 runner, 可以看到界面右边有 gitlab 的地址和 token。这个需要用于后面 runner 的注册使用。这里我们注册一个 share 类型的 runner。
由于 runner 是采用 docker 安装, 因此注册的时候需要进入到 runner 的容器中进行
[root@localhost config]# docker exec -it gitlab-runner bash
root@24dc60abee0b:/# gitlab-runner register
Runtime platform arch=amd64 os=linux pid=86 revision=775dd39d version=13.8.0
Running in system-mode.
Enter the GitLab instance URL (for example, https://gitlab.com/):
http://192.168.50.128/
Enter the registration token:
iqxKz5XTz4w_2RxiSQ5S
Enter a description for the runner:
[24dc60abee0b]: node1.ayunw.cn
Enter tags for the runner (comma-separated):
default
Registering runner... succeeded runner=iqxKz5XT
Enter an executor: docker-ssh+machine, kubernetes, custom, shell, ssh, virtualbox, docker, docker-ssh, parallels, docker+machine:
docker
Enter the default Docker image (for example, ruby:2.6):
docker:19.03.15
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
root@24dc60abee0b:/# gitlab-runner restart
Runtime platform arch=amd64 os=linux pid=98 revision=775dd39d version=13.8.0
Terminated
root@24dc60abee0b:/# gitlab-runner list
Runtime platform arch=amd64 os=linux pid=130 revision=775dd39d version=13.8.0
Listing configured runners ConfigFile=/etc/gitlab-runner/config.toml
node1.ayunw.cn Executor=docker Token=VSVWeipeMirJsJo9znT5 URL=http://192.168.50.128/
runner 注册完成后会在 /etc/gitlab-runner
目录下生成一个 config.toml
的文件。这个就是 runner 的配置文件。因为在安装 runner 的时候我们已经将配置文件的目录通过挂载的形式映射到了宿主机目录: /data/gitlab-runner/config
下, 所以后续如果需要更新 runner 配置文件可以直接在宿主机上进行修改。并且在宿主机上进行修改 runner 配置文件不需要重启 runner。它会每 5 分钟检查一次文件自动获取所有更改。包括该 [[runners]]
部分中定义的任何参数以及全局部分中的大多数参数(除外) listen_address
。
配置如下:
root@24dc60abee0b:/etc/gitlab-runner# cat config.toml
concurrent = 1
check_interval = 0
[session_server]
session_timeout = 1800
[[runners]]
name = "node1.ayunw.cn"
url = "你的 gitlab 访问的 url 地址"
token = "在 gitlab 的 ui 上看到的 token"
executor = "docker"
[runners.custom_build_dir]
[runners.cache]
[runners.cache.s3]
[runners.cache.gcs]
[runners.cache.azure]
[runners.docker]
tls_verify = false
image = "docker:19.03.15"
privileged = false
disable_entrypoint_overwrite = false
oom_kill_disable = false
disable_cache = false
volumes = ["/cache"]
shm_size = 0
注册完成后, 返回 gitlab 的 ui 查看注册的 runner。
可以看到当前的 runner 是锁定的状态。如果需要使用这个 runner, 需要将它解锁。我们可以点击右边的编辑, 然后将 "锁定到当前项目"取消勾选。再将运行未标记的作业勾选上。现在 runner 就可以运行了。
1.1.5. 测试 pipeline
新建一个项目, 然后在项目根目录提交一个 .gitlab-ci.yml
的文件, 内容如下。当提交了以后, 这时候就会触发 pipeline 流水线了。
stages:
- maven
- build
- deploy
maven_job:
stage: maven
tags:
- default
only:
- master
script:
- echo "This is the first maven job"
build_job:
stage: build
tags:
- default
only:
- master
script:
- echo "This is the first build job"
deploy_job:
stage: deploy
tags:
- default
only:
- master
script:
- echo "This is the first deploy job"
至此, gitlab runner 安装完成且整个 pipeline 流水线可以正常运行。
1.2. GitLab Runner - How to allow only one Pipeline run at a time
Set resource_group
in the Job, and give a unique name for all other tasks that should be blocked.
Example from the documentation:
deploy-to-production:
script: deploy
resource_group: production
1.3. Get Gitlab version
https://your.domain.name/help
1.4. Gitlab API
1.4.1. Get project ID
- Method 1: In the project page, existing
Project ID:
. - Methid 2: View HTML source code, will see like this
<input type="hidden" name="project_id" id="project_id" value="335" />
.
1.5. Gitlab 概念
1.5.1. Gitlab detached pipeline
In a basic configuration, GitLab runs a pipeline each time changes are pushed to a branch. If you want the pipeline to run jobs only on commits to a branch that is associated with a merge request, you can use pipelines for merge requests. In the UI, these pipelines are labeled as detached
.
1.6. GitLab .gitlab-ci.yml
sample
# run lint and unit test for most of the branches
ci_lint_and_ut:
stage: deploy
resource_group: production
rules:
- if: $CI_PIPELINE_SOURCE == 'merge_request_event'
script:
- nvm use v16.14.0
- make clean
- make lint AUTO_FIX=off
- export UT_EXCLUDE_INTEGRATION_TESTING="off" # enable integration testing.
- export UT_EXCLUDE_HUGE_INTEGRATION_TESTING="off" # enable huge integration testing.
- make ut DATAWAY_URL="$TESTING_METRIC_DATAWAY"
tags:
- cloudcare-ft