Skip job if cache exists

I had to skip several jobs in cache cache exists.
So I did this trick:

lint_code:
executor: my-executor
steps:
  - skip_if_cache_exists:
      skiptype: "linting"
  - run: do job stuff
  - save_cache_flag:
      skiptype: "linting"

and the commands that Im using above: skip_if_cache_exists, save_cache_flag

commands:
  skip_if_cache_exists:
description: |
  a command to exit the job for selected branch
parameters:
  skiptype:
    description: type of job to skip
    type: string
steps:
  - restore_cache:
      key: skipcheck-<<parameters.skiptype>>-{{ .Environment.CIRCLE_BRANCH }}-{{ .Environment.CIRCLE_SHA1 }}
  - run: 
      name: if cache exists exit
      command: |
        FILE=~/cachedflags/job.<<parameters.skiptype>>.flag
        if test -f "$FILE"; then
            echo "$FILE exist"
            circleci step halt
        else
            echo "$FILE doesnt exist"
        fi
save_cache_flag:
  description: |
  a command that will create the cache
  parameters:
    skiptype:
      description: type of job to skip
      type: string
  steps:
    - run:
        name: create job flag file
        command: mkdir -p ~/cachedflags/ && touch ~/cachedflags/job.<<parameters.skiptype>>.flag
  - save_cache:
      key: skipcheck-<<parameters.skiptype>>-{{ .Environment.CIRCLE_BRANCH }}-{{ .Environment.CIRCLE_SHA1 }}
      paths:
        - ~/cachedflags/job.<<parameters.skiptype>>.flag
1 Like