How to identify retry pipeline jobs in GitLab?
Asked Answered
U

1

6

I am using GitLab CI/CD. When the jobs get failed in a pipeline i can retry them manually. Once i retry the job, is there anyway someone can identify job is retried?

Particularly is there any CI variables showing job is retried.

Univalve answered 23/11, 2022 at 14:8 Comment(0)
F
7

You are right, as of Dec 2022 there is no predefined variable in Gitlab CI which will allows determine that current job is retried. I see there are multiple open discussions/suggestions (issue 27589, issue 195618) with no real actions planned. I use Gitlab CI cache to workaround this. I create a file .counter (if doesn't exit) with value 0 in the beginning of job and then increment it at the end of job.

Here is my simplified solution:

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

I hope that helps until Gitlab implements this simple thing.

Foggy answered 1/12, 2022 at 10:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.