如何识别 GitLab 中的重试管道作业?

Anj*_*jan 4 gitlab gitlab-ci gitlab-ci-runner gitlab-api

我正在使用 GitLab CI/CD。当管道中的作业失败时,我可以手动重试它们。一旦我重试工作,是否有人可以识别工作已重试?

特别是是否有任何 CI 变量显示作业已重试。

Jus*_*lys 5

你是对的,截至 2022 年 12 月,Gitlab CI 中没有预定义变量可以确定是否重试当前作业。我看到有多个公开讨论/建议(问题 27589问题 195618),但没有计划采取任何实际行动。我使用 Gitlab CI 缓存来解决这个问题。.counter我在作业开始时创建一个值为 0 的文件(如果不退出),然后在作业结束时递增它。

这是我的简化解决方案:

jobname:
    stage: stagename
    dependencies: []
    script:
        # Create counter file if it doesn't exist
        - "[ -f .counter ] || echo 0 > .counter"
        - RETRY_NUMBER=`cat .counter`
        # Do whatever you need knowing it is retried
        - if [ $RETRY_NUMBER -gt 0 ]; then echo $RETRY_NUMBER; fi;
        # Increment value and update it to file
        - RETRY_NUMBER=$((RETRY_NUMBER+1))
        - echo $RETRY_NUMBER > .counter
    cache:
        # Pay attention to the key, you may want to to adjust to your needs, but I consider job is retried by having branch or tag identifier as CI_COMMIT_REF_SLUG and commit sha
        - key: $CI_COMMIT_REF_SLUG-$CI_COMMIT_SHORT_SHA-counter
          paths:
              - .counter
          policy: pull-push
Run Code Online (Sandbox Code Playgroud)

我希望在 Gitlab 实现这个简单的事情之前这会有所帮助。