Skip to content

Commit

Permalink
Improve cache invalidation job (#886)
Browse files Browse the repository at this point in the history
  • Loading branch information
blotus authored Dec 15, 2023
1 parent dbe024c commit b7329ba
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions .github/workflows/update-index.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,18 @@ jobs:
invalidate-cache:
runs-on: ubuntu-latest
needs: build
#Only invalidate cache on master or vX branches
#Branches that don't match this pattern are only used for dev, so we can manually invalidate if needed
#We should avoid naming dev branches with something starting with v :D
if: |
startsWith('refs/heads/v', github.ref) || github.ref == 'refs/heads/master'
permissions:
id-token: write
contents: read
steps:
- uses: actions/checkout@v1
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.CF_AWS_ROLE }}
role-session-name: github-action
aws-region: eu-west-1
mask-aws-account-id: true
- name: Get branch name
run: echo "version=${GITHUB_REF#refs/heads/}" >> $GITHUB_ENV
- name: Get changed files
Expand All @@ -77,8 +73,17 @@ jobs:
echo "changed_files=${changed_files}" >> $GITHUB_ENV
- name: Invalidate cache
run: |
aws cloudfront create-invalidation --distribution-id ${{ secrets.CF_DISTRIBUTION_ID }} --paths "/${{ env.version }}/.index.json"
create_invalidation() {
#$1 is not quotted on purpose, so it can be expanded to multiple arguments
aws cloudfront create-invalidation --distribution-id ${{ secrets.CF_DISTRIBUTION_ID }} --paths $1
}
PATHS="/${{ env.version }}/.index.json"
IFS=',' read -ra FILE <<< "${{ env.changed_files }}"
for i in "${FILE[@]}"; do
aws cloudfront create-invalidation --distribution-id ${{ secrets.CF_DISTRIBUTION_ID }} --paths "/${{ env.version }}/$i"
PATHS="$PATHS /${{ env.version }}/$i"
done
echo "Invalidating paths: $PATHS"
for ((i=0; i < 3; i++)); do
create_invalidation "$PATHS" && break || echo "Invalidation failed, retrying in 5 seconds..."
sleep 5
done

0 comments on commit b7329ba

Please sign in to comment.