极狐gitlab ci job拉取代码之前执行命令

有时候我们希望可以在检索 Git 代码库和任何 submodules 之前执行一下命令操作,接下来为大家介绍下通过 hooks: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:latest

build1:
  stage: build
  script:
    - echo "Do your build here"

test1:
  stage: test
  hooks:
    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 = 3
  token = "2TczXVopZ1fic7GeQ2BS"
  token_obtained_at = 2023-11-29T16:31:40Z
  token_expires_at = 0001-01-01T00:00:00Z
  executor = "docker"
  pre_get_sources_script = "eval \"$CI_PRE_CLONE_SCRIPT\""
  [runners.cache]
    MaxUploadedArchiveSize = 0
  [runners.docker]
    pull_policy = ["if-not-present"]
    tls_verify = false
    image = "alpine:latest"
    privileged = false
    disable_entrypoint_overwrite = false
    oom_kill_disable = false
    disable_cache = false
    volumes = ["/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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值