From fa79c42caab68b41401175c49a233e6ab370f9da Mon Sep 17 00:00:00 2001 From: Daven Quinn Date: Sat, 25 May 2024 02:25:35 -0500 Subject: [PATCH 1/6] Improve layer caching in Dockerfile --- Dockerfile | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 5342ec36..d852a8aa 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,14 +6,38 @@ RUN apt-get update && apt-get install -y rsync ENV NODE_ENV=production WORKDIR /usr/src/app -COPY . ./ +COPY .yarn/releases .yarn/releases +COPY .yarnrc.yml yarn.lock package.json ./ + +# Copy only the elements needed for a Yarn install to take advantage of +# Docker's layer caching +# This is complex because we are working with multiple workspaces. + +# Copy package JSON files with wildcards +# This scoops up all package.json files in the directory and subdirectories +# to deal with Yarn workspaces. However it requires BUILDKIT to be enabled, +# which is done by setting DOCKER_BUILDKIT=1 in the environment +RUN --mount=type=bind,target=/docker-context \ + cd /docker-context/; \ + find . -name "package.json" -mindepth 0 -maxdepth 5 -exec cp --parents "{}" /usr/src/app/ \; + +RUN yarn install --immutable # Load the cache from the previous build RUN --mount=type=cache,target=/yarn-cache \ - rsync -a /yarn-cache/ .yarn/cache \ + rsync -a /yarn-cache/ .yarn/cache/ \ && yarn install --immutable \ && rsync -a .yarn/cache/ /yarn-cache +# # Remove rsync +RUN apt-get remove -y rsync + +# # Now we can run the full copy command + +COPY . ./ + +ENV NODE_ENV=production + RUN yarn run build EXPOSE 3000 From e227e233b4c669d6a9ec06a04b6f2cc41d5414a1 Mon Sep 17 00:00:00 2001 From: Daven Quinn Date: Sat, 25 May 2024 02:29:07 -0500 Subject: [PATCH 2/6] Add note for layer caching --- .github/workflows/build-dev.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/build-dev.yaml b/.github/workflows/build-dev.yaml index 35426aaf..5247576b 100644 --- a/.github/workflows/build-dev.yaml +++ b/.github/workflows/build-dev.yaml @@ -42,5 +42,6 @@ jobs: push: true tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} + # Layer caching setup cache-from: type=gha cache-to: type=gha,mode=max From a9200dbadbbe893915661bf2f4d94d9e0dd14177 Mon Sep 17 00:00:00 2001 From: Daven Quinn Date: Sat, 25 May 2024 03:01:22 -0500 Subject: [PATCH 3/6] Merged development and production image building into a single task --- .../{build-dev.yaml => build-image.yaml} | 15 ++++--- .github/workflows/build-prod.yaml | 43 ------------------- 2 files changed, 9 insertions(+), 49 deletions(-) rename .github/workflows/{build-dev.yaml => build-image.yaml} (84%) delete mode 100644 .github/workflows/build-prod.yaml diff --git a/.github/workflows/build-dev.yaml b/.github/workflows/build-image.yaml similarity index 84% rename from .github/workflows/build-dev.yaml rename to .github/workflows/build-image.yaml index 5247576b..50383f4a 100644 --- a/.github/workflows/build-dev.yaml +++ b/.github/workflows/build-image.yaml @@ -1,8 +1,10 @@ -name: Build Development +name: Build image on: push: branches: ["main"] + tags: + - v[0-9]+.[0-9]+.[0-9]+ # Semver Release pull_request: branches: [main] @@ -19,15 +21,17 @@ jobs: uses: docker/metadata-action@v4 with: images: hub.opensciencegrid.org/macrostrat/web + # New: apply the 'latest' tag to tagged releases on the default branch tags: | - type=semver,pattern={{version}} + type=raw,value=sha-{{sha}} type=raw,value=latest-itb + type=raw,value=latest-itb-{{date 'YYYYMMDDHHmmss'}} type=ref,event=pr,suffix=-{{date 'YYYYMMDDHHmmss'}} type=ref,event=branch,suffix=-{{date 'YYYYMMDDHHmmss'}} type=ref,event=tag,suffix=-{{date 'YYYYMMDDHHmmss'}} - type=raw,value=latest-itb-{{date 'YYYYMMDDHHmmss'}} - type=raw,value=sha-{{sha}} - - name: Set up Docker Buildx + type=semver,pattern={{version}} + type=semver,value=latest,enable={{is_default_branch}} + - name: Set up Docker BuildX uses: docker/setup-buildx-action@v2 - name: Login to OSG DockerHub uses: docker/login-action@v2 @@ -42,6 +46,5 @@ jobs: push: true tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} - # Layer caching setup cache-from: type=gha cache-to: type=gha,mode=max diff --git a/.github/workflows/build-prod.yaml b/.github/workflows/build-prod.yaml deleted file mode 100644 index e77f6127..00000000 --- a/.github/workflows/build-prod.yaml +++ /dev/null @@ -1,43 +0,0 @@ -name: Build Production - -on: - push: - tags: - - v[0-9]+.[0-9]+.[0-9]+ # Semver Release - -jobs: - docker: - runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@v3 - with: - submodules: "recursive" - - name: Docker meta - id: meta - uses: docker/metadata-action@v4 - with: - images: hub.opensciencegrid.org/macrostrat/web - tags: | - type=ref,event=pr,suffix=-{{date 'YYYYMMDDHHmmss'}} - type=ref,event=branch,suffix=-{{date 'YYYYMMDDHHmmss'}} - type=semver,pattern={{version}} - type=raw,value=latest,enable={{is_default_branch}} - type=raw,value=sha-{{sha}} - - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v2 - - name: Login to OSG DockerHub - uses: docker/login-action@v2 - with: - registry: hub.opensciencegrid.org - username: ${{ vars.HARBOR_CLI_NAME }} - password: ${{ secrets.HARBOR_CLI_SECRET }} - - name: Build and push - uses: docker/build-push-action@v5 - with: - context: . - push: true - tags: ${{ steps.meta.outputs.tags }} - labels: ${{ steps.meta.outputs.labels }} - cache-from: type=gha - cache-to: type=gha,mode=max From 72cc2f6b5ff2a1ca08a52f8f46fce4433310b0fb Mon Sep 17 00:00:00 2001 From: Daven Quinn Date: Sat, 25 May 2024 03:02:32 -0500 Subject: [PATCH 4/6] Remove redundant local website build CI --- .github/workflows/test-build.yaml | 21 --------------------- 1 file changed, 21 deletions(-) delete mode 100644 .github/workflows/test-build.yaml diff --git a/.github/workflows/test-build.yaml b/.github/workflows/test-build.yaml deleted file mode 100644 index 9aaa1c77..00000000 --- a/.github/workflows/test-build.yaml +++ /dev/null @@ -1,21 +0,0 @@ -name: Test Website Build - -on: - push: - branches: [main] - pull_request: - branches: [main] - -jobs: - test-build: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - with: - submodules: "recursive" - - uses: actions/setup-node@v4 - with: - node-version: 20 - cache: "npm" - - run: yarn install - - run: yarn build From 6030c2f6e58758e2c9ea9380cb3858fd6b0739e7 Mon Sep 17 00:00:00 2001 From: Daven Quinn Date: Sat, 25 May 2024 03:19:52 -0500 Subject: [PATCH 5/6] Small update to workflow --- .github/workflows/build-image.yaml | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/.github/workflows/build-image.yaml b/.github/workflows/build-image.yaml index 50383f4a..498a7a74 100644 --- a/.github/workflows/build-image.yaml +++ b/.github/workflows/build-image.yaml @@ -4,7 +4,7 @@ on: push: branches: ["main"] tags: - - v[0-9]+.[0-9]+.[0-9]+ # Semver Release + - v[0-9]+.[0-9]+.[0-9]+ # Semver Release (non-prerelease) pull_request: branches: [main] @@ -21,16 +21,19 @@ jobs: uses: docker/metadata-action@v4 with: images: hub.opensciencegrid.org/macrostrat/web - # New: apply the 'latest' tag to tagged releases on the default branch + # New: apply the 'latest' tag to non-prerelease semver tags tags: | type=raw,value=sha-{{sha}} type=raw,value=latest-itb + type=match,value=latest,pattern=v\d+.\d+.\d+ type=raw,value=latest-itb-{{date 'YYYYMMDDHHmmss'}} type=ref,event=pr,suffix=-{{date 'YYYYMMDDHHmmss'}} type=ref,event=branch,suffix=-{{date 'YYYYMMDDHHmmss'}} type=ref,event=tag,suffix=-{{date 'YYYYMMDDHHmmss'}} type=semver,pattern={{version}} - type=semver,value=latest,enable={{is_default_branch}} + type=semver,pattern={{version}}-{{date 'YYYYMMDDHHmmss'}} + flavor: | + latest=false - name: Set up Docker BuildX uses: docker/setup-buildx-action@v2 - name: Login to OSG DockerHub From ca50775a8a3e96b4f208ba80bce4c6da75f3c8de Mon Sep 17 00:00:00 2001 From: Daven Quinn Date: Sat, 25 May 2024 03:30:54 -0500 Subject: [PATCH 6/6] Fix semver output again --- .github/workflows/build-image.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build-image.yaml b/.github/workflows/build-image.yaml index 498a7a74..07cbd73b 100644 --- a/.github/workflows/build-image.yaml +++ b/.github/workflows/build-image.yaml @@ -25,7 +25,7 @@ jobs: tags: | type=raw,value=sha-{{sha}} type=raw,value=latest-itb - type=match,value=latest,pattern=v\d+.\d+.\d+ + type=raw,value=latest,enable=${{ startsWith(github.ref, 'refs/tags/v') && !contains(github.ref, '-')}} type=raw,value=latest-itb-{{date 'YYYYMMDDHHmmss'}} type=ref,event=pr,suffix=-{{date 'YYYYMMDDHHmmss'}} type=ref,event=branch,suffix=-{{date 'YYYYMMDDHHmmss'}}