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

Docker & CI build: improve speed by leveraging caching #4545

Merged
merged 29 commits into from
Aug 21, 2023
Merged

Conversation

derhuerst
Copy link
Member

@derhuerst derhuerst commented Apr 21, 2022

Still missing:

  • Mount .yarn/cache into node-modules the Docker build.
  • Try caching the node-modules stage, and check if it is actually faster than building from scratch. – much faster, I didn't expect that
  • Check if the CI runs as intended.
  • Investigate why yarn build is a lot slower than before. 🤔 ( ~5m before, 10-20m after) – due to the upgraded favicons-webpack-plugin
  • My addition of --immutable to yarn install causes the build to fail, as currently both on v3 and improve-ci-speed, the checked-in yarn.lock does not reflect the workspace packages' versions. cc @vesameskanen

Proposed Changes

This PR changes the Dockerfile & GitHub Actions dev pipeline to be faster.

While an un-cached (different yarn.lock and/or different digitransit-*/** and/or different commit hash) build is slightly slower, a cached one is is much faster.

Review

For an unknown reason, [email protected] crashed during the Docker build, and favicons-webpack-plugin@^4 froze, so I used favicons-webpack-plugin@^3.

When looking at the changes, I recommend to review each commit individually, as I tried to make them as atomic and sensible as possible. Some of them also contain explanations.

Unfortunately, because the GitHub Actions system seems to lack support for YAML anchors, I added a lot of duplicated code in .github/workflows/dev-pipeline.yml. The benefit of the repeated "use cache if exists, otherwise rebuild" patterns is that each step is effectively idempotent – it works regardless of wether the cache is available and if a previous step populated it –, with the downside being very repetitive code. Once YAML anchors are supported, we could DRY it a lot.

@@ -3,6 +3,8 @@ on:
push:
branches:
- v2
# todo: remove
Copy link
Member Author

Choose a reason for hiding this comment

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

This needs to be removed before merging!

.github/workflows/scripts/build_and_push_dev.sh Outdated Show resolved Hide resolved
@derhuerst derhuerst force-pushed the improve-ci-speed branch 21 times, most recently from 5023ad2 to 0352591 Compare April 21, 2022 14:16
publish-npm:
if: github.ref == 'refs/heads/v2'
# todo: re-add this before merging!
# if: github.ref == 'refs/heads/v2'
Copy link
Member Author

Choose a reason for hiding this comment

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

This needs to be removed before merging!

PUBLISHED_PACKAGES: ${{ steps.log-parse.outputs.published-packages }}
shell: bash
# todo: re-add this before merging!
# - name: Publish shared components to npm
Copy link
Member Author

Choose a reason for hiding this comment

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

This needs to be removed before merging!

.github/workflows/dev-pipeline.yml Outdated Show resolved Hide resolved
@derhuerst derhuerst force-pushed the improve-ci-speed branch 7 times, most recently from c228f58 to 93620c3 Compare April 21, 2022 15:17
- use 1.4 syntax to support `COPY --link` commands
- set NODE_ENV=production
- remove $WORK build arg
…ching

- If e.g. `test` has been added in a command/step, removing it in a
subsequent command/step does not have a positive effect on the image size.
Rather, we add it .dockerignore to stop adding it in the first place.
- node_modules/.cache & /tmp/Relay* are populated by Webpack plugins.
app/server.js reads .entrypoints.main.assets in order to generate an appropriate
manifest and `preload` tags, but it doesn't need the rest. This is why we turn
all stats off via `all: false` and then selectively turn `entrypoints` on.

Before, stats.json was 82mb, now it is ~9kb.
Even if a commit has changed `yarn.lock`, we can use an old `.yarn/cache` directory, so that we don't have to fetch 99% of all dependencies again.
This probably improves favicons-webpack-plugin's performance, as node_modules won't be resolved for resolving the path.
This speeds up the webpack build within the Docker image build significantly.
@optionsome optionsome merged commit 031fe85 into v3 Aug 21, 2023
@derhuerst derhuerst deleted the improve-ci-speed branch August 21, 2023 14:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants