How to trigger gitlab job if cache empty?

Viewed 365

Background

Consider a dummy gitlab job with a prebuild and build stage.

stages:
  - prebuild
  - build

The sole job of the prebuild stage is to create and cache dependencies for later stages. The prebuild stage is only triggered if a python requirements.txt file changes. The build stage has a cache:policy of pull, meaning it defers to prebuild for the cache. For example, imagine the cache consisted of a shared a single file sharedfile:

prebuild_job:
   cache:
     policy: pull-push
     rules:
       - changes:
         - requirements.txt
     paths:
       - sharedfile
     script:
       - echo "FOOBAR" > sharedfile

build_job:
   cache:
     policy: pull
   paths:
     - sharedfile
   script:
     - cat sharedfile

This does what I want - prebuild is triggered only when requirements.txt changes and build uses its output (in this case sharedfile but in reality a full python virtual env.). This works great until the cache is manually cleared on gitlab via the Clear Runner Caches button, then we have to manually re-run the prebuild step or builds will keep failing as the prebuild was never retriggered after the cache clear.

enter image description here

Question

How can we setup a rule or workaround such that the prebuild_job will know to fire if the cache is empty in addition to its existing change rule?. Something like

prebuild_job:
   cache:
     policy: pull-push
     rules:
       - changes:
         - requirements.txt
       - if: "$CACHE == {}"     #<-- something like this
     paths:
       - sharedfile
     script:
       - echo "FOOBAR" > sharedfile

Workaround

Can add an optional button to retrigger the prebuild phase manually. allow_failure means the button is optional and won't block subsequent stages.

prebuild_job:
   cache:
     policy: pull-push
     rules:
       - changes:
         - requirements.txt
       - if: '"True"'
         when: manual
         allow_failure: true     
       ...
0 Answers
Related