Skip to content

Commit

Permalink
Add daily docker cache workflow to master (#3681)
Browse files Browse the repository at this point in the history
This PR makes the following changes:

- Adds a new workflow action that runs once a day `Monday - Friday` at
5am to build a fresh docker cache
  - This will allow all child branches to have access to this cache
  • Loading branch information
sotojn authored Jul 12, 2024
1 parent 7b08514 commit 3084b64
Showing 1 changed file with 89 additions and 0 deletions.
89 changes: 89 additions & 0 deletions .github/workflows/daily-docker-cache.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
name: Daily Docker Cache

on:
schedule:
- cron: '0 12 * * 1-5' # Monday - Friday at 5am Arizona Time

jobs:
cache-docker-images:
runs-on: ubuntu-latest

# # final check to make sure it runs only on master branch
if: github.ref == 'refs/heads/master'

steps:
- name: Check out code
uses: actions/checkout@v4

- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: 18.19.1
cache: 'yarn'

# we login to docker to avoid docker pull limit rates
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}

- name: Check docker.hub limit start of job
env:
USER: ${{ secrets.DOCKER_USERNAME }}
PASS: ${{ secrets.DOCKER_PASSWORD }}
run: yarn docker:limit

- name: Install and build packages
run: yarn setup
env:
YARN_SETUP_ARGS: "--prod=false --silent"

- name: Create Docker Image List
run: |
yarn docker:listImages
cat ./images/image-list.txt
- name: Restore Docker image cache
id: docker-cache
uses: actions/cache@v4
with:
path: /tmp/docker_cache
key: docker-images-${{ hashFiles('./images/image-list.txt') }}
lookup-only: true

- name: Clear old docker cache
if: ${{steps.docker-cache.outputs.cache-hit == 'true'}}
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
REPO: ${{ github.repository }}
BRANCH: refs/heads/master
run: |
gh extension install actions/gh-actions-cache
echo "Fetching list of cache key"
cacheKeysForPR=$(gh actions-cache list --key "docker-images-" -R $REPO -B $BRANCH -L 100 | cut -f 1 )
## Setting this to not fail the workflow while deleting cache keys.
set +e
echo "Deleting caches..."
for cacheKey in $cacheKeysForPR
do
gh actions-cache delete $cacheKey -R $REPO -B $BRANCH --confirm
done
echo "Done"
- name: Pull and save Docker images
run: yarn docker:saveImages

- name: Update Docker image cache
uses: actions/cache/save@v4
with:
path: /tmp/docker_cache
key: docker-images-${{ hashFiles('./images/image-list.txt') }}

- name: Check docker.hub limit end of job
env:
USER: ${{ secrets.DOCKER_USERNAME }}
PASS: ${{ secrets.DOCKER_PASSWORD }}
run: npm run docker:limit

0 comments on commit 3084b64

Please sign in to comment.