Skip to content

Instantly share code, notes, and snippets.

@sdwvit
Created October 30, 2019 19:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sdwvit/b4138e74e1ac5437a7756d2cede7f66a to your computer and use it in GitHub Desktop.
Save sdwvit/b4138e74e1ac5437a7756d2cede7f66a to your computer and use it in GitHub Desktop.
Generate cacheable key for CI depending on versioned files diff
#!/bin/sh
a="$(git diff master... --numstat | awk 'NF==3 {total+=$1+$2} END {printf("%d\n", total)}')" \
if [ "$a" -gt 100 ]
then
echo "bust $(date +%s)" > gitdiff
else
echo "keep" > gitdiff
fi
@sdwvit
Copy link
Author

sdwvit commented Oct 30, 2019

Some parameters:
100 - describes number of lines changed threshold.
gitdiff - filepath for partial key, editable
bust/keep - editable

Usage example:

  terser_cache_key: &terser_cache_key v1-webpack_terser-{{ checksum "gitdiff" }}
  terser_cache_key_backup: &terser_cache_key_backup v1-webpack_terser-

  restore_terser_cache: &restore_terser_cache
    run:
      name: Generate partial key for terser-cache
      command: |
        #!/bin/sh

        a="$(git diff master... --numstat | awk 'NF==3 {total+=$1+$2} END {printf("%d\n", total)}')" \

        if [ "$a" -gt 100 ]
        then
          echo "bust $(date +%s)" > gitdiff
        else
          echo "keep" > gitdiff
        fi
    restore_cache:
      name: Restoring Cache (/tmp/terser)
      keys:
        - *terser_cache_key
        - *terser_cache_key_backup

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment