Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache Docker Image #147

Merged
merged 1 commit into from
Feb 12, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,26 @@ jobs:
uses: actions/checkout@v2
- name: Set up Docker
run: docker network create test
- id: cache-docker
uses: actions/cache@v2
with:
path: /tmp/docker-save
key:
docker-save-${{ hashFiles('Dockerfile', 'Gemfile.lock',
'package-lock.json') }}
- name: Load cached Docker image
run: docker load -i /tmp/docker-save/snapshot.tar || true
if: steps.cache-docker.outputs.cache-hit == 'true'
- name: Set up Postgres
run:
docker run -d --name pg --network test -e POSTGRES_USER=test -e
POSTGRES_HOST_AUTH_METHOD=trust -p 5432:5432 postgres:11
- name: Build a new Docker image
run: docker build . --build-arg RAILS_ENV=test -t app:test
run: |
docker build . \
--build-arg RAILS_ENV=test \
-t app:test \
--cache-from app:test
- name: Run the tests
run: |
docker run --name test-container \
Expand All @@ -33,3 +47,8 @@ jobs:
-e DATABASE_CLEANER_ALLOW_REMOTE_DATABASE_URL=true \
-e DATABASE_URL=postgres://test@pg:5432/app_test \
app:test bundle exec rake
- name: Prepare Docker cache
run:
mkdir -p /tmp/docker-save && docker save app:test -o
/tmp/docker-save/snapshot.tar && ls -lh /tmp/docker-save
tahb marked this conversation as resolved.
Show resolved Hide resolved
if: always() && steps.cache-docker.outputs.cache-hit != 'true'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to update the cache even if there's a test failure? Feels like it would be better to keep the cache to those images that pass.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there may be value in sticking with the initial approach.

I'm imaginging the scenario where a pull request adds a new dependency to one of the earlier docker layers. The CI job fails due to application tests which are sourced by one of the last Docker layers. Given this PR is likely to generate another CI run which only changes the application layer, there is value in having a cached dependency layer on hand.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, that's exactly the scenario I envisaged

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess I was worried about broken stuff sitting in the cache and causing bugs (but I guess we should trust Docker). Is the cache isolated to a branch? Will a cache from a branch replace the main cache (and therefore mean everyone not on that branch will have slower builds)? Don't feel super strongly either way, though.