Skip to content

Add option to avoid duplicate jobs with same git hash

Description

I'd like to be able to set the option that a job should only run once for a given git hash. Currently we get duplicate jobs when pushing both commits and tags, as seen in the picture below:

multiple-jobs

If git is set to use push.followTags, annotated tags will be pushed at the same time as other pushes. Even if the tags are pushed at the same time as the commits, Gitlab CI adds first the commits and then the tags to the job queue.

There are (at least) two ways to solve this:

  • explicitly look to see if any git tags point to HEAD when adding jobs to the queue
    • if there are any, only add the jobs for the tagged commits
  • cache results to ensure that any given point in a git tree is only added to the queue once
    • for subsequent jobs (e.g. in the case where a non-tagged commit has been pushed, and a tag is later added), job stages may be skipped with reference to the previous job with same git hash

Proposal

Add an option cache_results to jobs in the .gitlab-ci.yml file to avoid running the same job for the more than once per unique git hash.

E.g.:

build:
  stage: build
  # the build stage should only run for tagged releases, or when triggered
  # manually
  only:
    - tags
    - triggers
  script:
    - lein uberjar
  # Cache the results of the jobs to avoid running jobs multiple times for the same git hash.
  cache_results: true

Links / references

A bit of related discussion from the Gitlab forums: https://forum.gitlab.com/t/eliminating-duplicate-ci-builds-on-pushes-with-tags/4092/4