Skip to content

Instantly share code, notes, and snippets.

@hidakatsuya
Last active June 11, 2022 08:20
Show Gist options
  • Save hidakatsuya/a20985499939d47ba23e08c0cdeacc62 to your computer and use it in GitHub Desktop.
Save hidakatsuya/a20985499939d47ba23e08c0cdeacc62 to your computer and use it in GitHub Desktop.
Caching a docker image to skip pulling an image in GitHub Action
name: Test
on: push
env:
RUBY_VERSION: '3.1.0'
PLAYWRIGHT_IMAGE_VERSION: latest
CACHE_BUNDLE_PATH: /tmp/bundle
CACHE_PLAYWRIGHT_PATH: /tmp/playwright-image
jobs:
setup-gems:
name: Setup gems
runs-on: ubuntu-latest
timeout-minutes: 3
steps:
- uses: actions/checkout@v3
- uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ env.RUBY_VERSION }}
- name: Cache gem
id: cache-gem
uses: actions/cache@v3
with:
path: ${{ env.CACHE_BUNDLE_PATH }}
key: gems-${{ env.RUBY_VERSION }}-${{ hashFiles('Gemfile.lock') }}
restore-keys: |
gems-${{ env.RUBY_VERSION }}-
- name: Bundle
if: steps.cache-gem.outputs.cache-hit != 'true'
run: |
bundle config path $CACHE_BUNDLE_PATH
bundle install --jobs 4 --retry 3
setup-playwright-image:
name: Setup playwright image
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- name: Cache image
id: cache-image
uses: actions/cache@v3
with:
path: ${{ env.CACHE_PLAYWRIGHT_PATH }}
key: playwright-image-${{ env.PLAYWRIGHT_IMAGE_VERSION }}
- name: Pull and save
if: steps.cache-image.outputs.cache-hit != 'true'
run: |
docker pull ghcr.io/hidakatsuya/playwright-test-server:$PLAYWRIGHT_IMAGE_VERSION
docker save ghcr.io/hidakatsuya/playwright-test-server:$PLAYWRIGHT_IMAGE_VERSION -o $CACHE_PLAYWRIGHT_PATH
test:
name: Test
needs:
- setup-gems
- setup-playwright-image
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- uses: actions/checkout@v3
- name: Restore gem cache
id: restore-gem-cache
uses: actions/cache@v3
with:
path: ${{ env.CACHE_BUNDLE_PATH }}
key: gems-${{ env.RUBY_VERSION }}-${{ hashFiles('Gemfile.lock') }}
restore-keys: |
gems-${{ env.RUBY_VERSION }}-
- name: Ensure cache-hit for gem
if: steps.restore-gem-cache.outputs.cache-hit != 'true'
run: |
echo "Gem cache not found"
exit 1
- name: Restore docker image cache
id: restore-image-cache
uses: actions/cache@v3
with:
path: ${{ env.CACHE_PLAYWRIGHT_PATH }}
key: playwright-image-${{ env.PLAYWRIGHT_IMAGE_VERSION }}
- name: Ensure cache-hit for playwright image
if: steps.restore-image-cache.outputs.cache-hit != 'true'
run: |
echo "Playwright image cache not found"
exit 1
- name: Load playwright image
run: docker load -i $CACHE_PLAYWRIGHT_PATH
- name: Build and setup
run: |
docker-compose run app bin/rails db:prepare
docker-compose up -d
- name: Test
run: docker-compose exec -T app bin/rails test:all
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment