Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Fixed and improved Docker build caching. #7988

Merged
merged 1 commit into from
May 18, 2023
Merged

Fixed and improved Docker build caching. #7988

merged 1 commit into from
May 18, 2023

Conversation

barankyle
Copy link
Member

@barankyle barankyle commented May 17, 2023

Summary

Builder build process caching was not working because it was a multi-stage build.
The default inline cache only caches the final stage of a multi-stage build, which
almost always results in cache misses on the earlier stages. All builds in the builder
were not being cached at all.

Converted builds to explicitly use buildx. buildx build has support for automatically
pushing tags, so separate steps for tagging and pushing images have been removed.

In order to cache multi-stage builds, buildx has an option to cache everything. This
has to be done with a cache target of something other than inline cache (which includes
cache in the image). There is an option to use a Docker repository, but currently this
is not supported by ECR. For now, using S3 as the cache target (another built-in option)
for service images, and GH Actions cache for builder image.
An issue has been opened to switch to the repo for the service images once it's supported.

Commented out running npm install when each project is installed. This appeared to be causing
issues where some packages, particularly semver, were not fully installed properly by the
end, leading to errors when those packages were referenced. There is now a single npm
install after installing all projects.

References

closes #insert number here

Checklist

  • If this PR is still a WIP, convert to a draft
  • When this PR is ready, mark it as "Ready for review"
  • ensure all checks pass
  • Changes have been manually QA'd
  • Changes reviewed by at least 2 approved reviewer

QA Steps

List any additional steps required to QA the changes of this PR, as well as any supplemental images or videos.

scripts/record-build-error.ts Fixed Show fixed Hide fixed
scripts/record-build-error.ts Fixed Show fixed Hide fixed
@barankyle barankyle force-pushed the docker-build-cache branch 2 times, most recently from 58f6be6 to c2014ae Compare May 17, 2023 20:11
@barankyle barankyle marked this pull request as ready for review May 17, 2023 20:11
@barankyle barankyle requested review from HexaField and hanzlamateen and removed request for HexaField May 17, 2023 20:12
@@ -71,8 +71,8 @@
const buildErrors = fs.readFileSync(`${options.service}-build-error.txt`).toString()
const builderRun = fs.readFileSync('builder-run.txt').toString()
if (options.isDocker) {
const hasError = /ERROR:/.test(buildErrors)
if (hasError) {
const cacheMissRegex = new RegExp(`${options.service}:latest_${process.env.RELEASE_NAME}: not found`)

Check failure

Code scanning / CodeQL

Regular expression injection

This regular expression is constructed from a [environment variable](1).
scripts/run-builder.sh Outdated Show resolved Hide resolved
Builder build process caching was not working because it was a multi-stage build.
The default inline cache only caches the final stage of a multi-stage build, which
almost always results in cache misses on the earlier stages. All builds in the builder
were not being cached at all.

Converted builds to explicitly use buildx. buildx build has support for automatically
pushing tags, so separate steps for tagging and pushing images have been removed.

In order to cache multi-stage builds, buildx has an option to cache everything. This
has to be done with a cache target of something other than inline cache (which includes
cache in the image). There is an option to use a Docker repository, but currently this
is not supported by ECR. For now, using S3 as the cache target (another built-in option)
for service images, and GH Actions cache for builder image.
An issue has been opened to switch to the repo for the service images once it's supported.

Commented out running npm install when each project is installed. This appeared to be causing
issues where some packages, particularly semver, were not fully installed properly by the
end, leading to errors when those packages were referenced. There is now a single npm
install after installing all projects.
Copy link
Member

@hanzlamateen hanzlamateen left a comment

Choose a reason for hiding this comment

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

Looks good

- name: restore package.json
run: mv package.jsonmoved package.json
- name: Expose GitHub Runtime
uses: crazy-max/ghaction-github-runtime@v2
Copy link
Member

@hanzlamateen hanzlamateen May 18, 2023

Choose a reason for hiding this comment

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

crazy-max/ghaction-github-runtime@v2 is used to access STORAGE_AWS_ACCESS_KEYs?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's actually to access some GitHub Actions environment variables used for caching Docker cache within GH Actions.

Copy link
Member Author

Choose a reason for hiding this comment

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

It's actually to access some GitHub Actions environment variables used for caching Docker cache within GH Actions.

@barankyle barankyle added this pull request to the merge queue May 18, 2023
Merged via the queue into dev with commit 50a30a5 May 18, 2023
@barankyle barankyle deleted the docker-build-cache branch May 18, 2023 17:15
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants