有时候我们希望可以在检索 Git 代码库和任何 submodules 之前执行一下命令操作,接下来为大家介绍下通过 hooks:pre_get_sources_script 可以满足该场景的使用。
文章目录
- 1. 开启 feature flag
- 2. 使用方式
- 方式1在 ci job 中定义 hooks.pre_get_sources_script
- 方式2:在 runner 中配置参数 pre_get_sources_script
1. 开启 feature flag
此功能 15.6 版本引入,参考 hookspre_get_sources_script。
# 进入到rails控制台
gitlab-rails console
# 查询flag
Feature.enabled?(:ci_hooks_pre_get_sources_script)
# 开启
Feature.enable(:ci_hooks_pre_get_sources_script)
2. 使用方式
方式1在 ci job 中定义 hooks.pre_get_sources_script
image: busybox:latestbuild1:stage: buildscript:- echo "Do your build here"test1:stage: testhooks:pre_get_sources_script:- echo 'hello job1 pre_get_sources_script'script: echo 'hello job1 script'
执行结果:
Running with gitlab-runner 16.2.1 (674e0e29)on dc 2TczXVop, system ID: s_881ce15226e3
Resolving secrets
00:00
Preparing the "docker" executor
00:01
Using Docker executor with image busybox:latest ...
Using locally found image version due to "if-not-present" pull policy
Using docker image sha256:fc9db2894f4e4b8c296b8c9dab7e18a6e78de700d21bc0cfaf5c78484226db9c for busybox:latest with digest busybox@sha256:3fbc632167424a6d997e74f52b878d7cc478225cffac6bc977eedfe51c7f4e79 ...
Preparing environment
00:01
Running on runner-2tczxvop-project-3-concurrent-0 via gitlab-31-200...
Getting source from Git repository
00:01
$ echo 'hello job1 pre_get_sources_script'
hello job1 pre_get_sources_script
Fetching changes with git depth set to 20...
Reinitialized existing Git repository in /builds/ops/maven-demo/.git/
Checking out b09407d2 as detached HEAD (ref is master)...
Skipping Git submodules setup
Executing "step_script" stage of the job script
00:00
Using docker image sha256:fc9db2894f4e4b8c296b8c9dab7e18a6e78de700d21bc0cfaf5c78484226db9c for busybox:latest with digest busybox@sha256:3fbc632167424a6d997e74f52b878d7cc478225cffac6bc977eedfe51c7f4e79 ...
$ echo 'hello job1 script'
hello job1 script
Job succeeded
方式2:在 runner 中配置参数 pre_get_sources_script
参考:Runner Advanced configuration,这里以 docker runner executer 为例:
concurrent = 1
check_interval = 0
shutdown_timeout = 0[session_server]session_timeout = 1800[[runners]]name = "dc"url = "http://192.168.31.100/"id = 3token = "2TczXVopZ1fic7GeQ2BS"token_obtained_at = 2023-11-29T16:31:40Ztoken_expires_at = 0001-01-01T00:00:00Zexecutor = "docker"pre_get_sources_script = "eval \"$CI_PRE_CLONE_SCRIPT\""[runners.cache]MaxUploadedArchiveSize = 0[runners.docker]pull_policy = ["if-not-present"]tls_verify = falseimage = "alpine:latest"privileged = falsedisable_entrypoint_overwrite = falseoom_kill_disable = falsedisable_cache = falsevolumes = ["/cache"]shm_size = 0
执行结果:
Running with gitlab-runner 16.2.1 (674e0e29)on dc 2TczXVop, system ID: s_881ce15226e3
Resolving secrets
00:00
Preparing the "docker" executor
00:01
Using Docker executor with image busybox:latest ...
Using locally found image version due to "if-not-present" pull policy
Using docker image sha256:fc9db2894f4e4b8c296b8c9dab7e18a6e78de700d21bc0cfaf5c78484226db9c for busybox:latest with digest busybox@sha256:3fbc632167424a6d997e74f52b878d7cc478225cffac6bc977eedfe51c7f4e79 ...
Preparing environment
00:01
Running on runner-2tczxvop-project-3-concurrent-0 via gitlab-31-200...
Getting source from Git repository
00:01
$ eval "$CI_PRE_CLONE_SCRIPT"
CI_PRE_CLONE_SCRIPT
test
Fetching changes with git depth set to 20...
Reinitialized existing Git repository in /builds/ops/maven-demo/.git/
Checking out 5e4a8b55 as detached HEAD (ref is master)...
Skipping Git submodules setup
Executing "step_script" stage of the job script
00:00
Using docker image sha256:fc9db2894f4e4b8c296b8c9dab7e18a6e78de700d21bc0cfaf5c78484226db9c for busybox:latest with digest busybox@sha256:3fbc632167424a6d997e74f52b878d7cc478225cffac6bc977eedfe51c7f4e79 ...
$ echo 'hello job1 script'
hello job1 script
Job succeeded