在 GitLab 中取消作业后如何执行脚本?

fin*_*tis 6 continuous-integration gitlab gitlab-ci gitlab-ci-runner

我正在寻找一种在 GitLab 中取消作业后清理运行程序的方法。原因是我们经常不得不取消正在运行的作业,因为运行程序有时会卡在测试管道中,我可以想象一些其他场景,您希望取消作业并在之后运行清理脚本。我正在寻找类似的东西after_script,但只是在工作被取消的情况下。我检查了GitLab 关键字参考,但找不到我需要的内容。

我的以下部分gitlab-ci.yaml显示了测试阶段,我想docker-compose down在取消作业时通过调用来优雅地取消该阶段。

我使用的是单个 gitlab-runner。另外,我不使用dind。

test_stage:
  stage: test
  only:
  - master
  - tags
  - merge_requests
  image: registry.gitlab.com/xxxxxxxxxxxxxxxxxxx
  variables:
    HEADLESS: "true"
  script:
  - docker login -u="xxxx" -p="${QUAY_IO_PASSWORD}" quay.io  
  - npm install
  - npm run test
  - npm install wait-on  
  - cd example
  - docker-compose up --force-recreate --abort-on-container-exit --build traefik frontend &
  - cd ..
  - apt install -y iproute2
  - export DOCKER_HOST_IP=$( /sbin/ip route|awk '/default/ { print $3 }' )
  - echo "Waiting for ${DOCKER_HOST_IP}/xxxt"
  - ./node_modules/.bin/wait-on "http://${DOCKER_HOST_IP}/xxx" && export BASE_URL=http://${DOCKER_HOST_IP} && npx codeceptjs run-workers 2
  - cd example
  - docker-compose down
  after_script:
  - cd example && docker-compose down 
  artifacts:
    when: always
    paths:
      - /builds/project/tests/output/
  retry:
    max: 2
    when: always
  tags: [custom-runner]
Run Code Online (Sandbox Code Playgroud)

Jos*_*eia 8

不幸的是,这在 GitLab 中目前是不可能的。他们的仓库中已经打开了几张票证,其中一张是最新的。

截至我发布此内容之日(2022 年 9 月 27 日),至少有 14 个错过的可交付成果。亚搏体育appGitLab继续说它即将到来,但在这张票被打开的六年里从未交付过。

自动取消作业有一些缓解措施,但不幸的是,这对您的情况没有帮助。

根据您的用例,我可以想到两种不同的解决方案:

  1. 创建一个包装脚本来检测测试作业的某些部分何时挂起
  2. 在管道上设置超时(在 GitLab 中您可以访问Settings -> CI/CD -> General Pipelines -> Timeout

这些解决方案都不像 GitLab 自己实现的解决方案那样强大,但它们至少可以防止您的工作永远挂起并堵塞管道中的其他所有内容。