-
Notifications
You must be signed in to change notification settings - Fork 126
feat(ci): improve container build process v2 #9317
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
Open
eliotlim
wants to merge
12
commits into
develop
Choose a base branch
from
feat/ci/improve-container-build-process-v2
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
a0d67a6
feat(ci): extract base images
eliotlim c3a8722
feat(frontend,backend,ci): streamline frontend and backend build
eliotlim 9b70f64
fix(frontend,backend): env script csp header hash computation
eliotlim 2ec4903
fix(ci): cache built image locally for asset upload step
eliotlim 8c3f530
fix(ci): use datadog-ci binaries instead of npx
eliotlim 9e3eb83
fix(ci): pin datadog-ci version to v5.11.0
eliotlim 35a859e
feat(ci): update runtime-base image packages
eliotlim 1b23e88
fix(ci): use apk no-cache for upgrades
eliotlim dcdd25f
fix(ci): pin action versions
eliotlim 6b5c826
fix(ci): remove extra Dockerfile build commands
eliotlim e2ae34e
chore: apply suggestions from code review
eliotlim 194e69c
fix(frontend): address code review comments 2
eliotlim File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| name: Build Base Docker Images | ||
|
|
||
| on: | ||
| workflow_call: | ||
| inputs: | ||
| force-rebuild: | ||
| description: 'Force rebuild even if images exist' | ||
| required: false | ||
| type: boolean | ||
| default: false | ||
| outputs: | ||
| build-base-image: | ||
| description: 'Full build-base image reference' | ||
| value: ${{ jobs.build-base.outputs.image }} | ||
| runtime-base-image: | ||
| description: 'Full runtime-base image reference' | ||
| value: ${{ jobs.build-base.outputs.runtime-image }} | ||
| workflow_dispatch: | ||
| inputs: | ||
| force-rebuild: | ||
| description: 'Force rebuild even if images exist' | ||
| required: false | ||
| type: boolean | ||
| default: false | ||
|
|
||
| permissions: | ||
| contents: read | ||
| packages: write | ||
|
|
||
| env: | ||
| REGISTRY: ghcr.io | ||
| IMAGE_NAME: ghcr.io/opengovsg/formsg | ||
|
|
||
| jobs: | ||
| build-base: | ||
| name: Build base images | ||
| runs-on: ubuntu-latest | ||
| outputs: | ||
| image: ${{ steps.set-outputs.outputs.build-image }} | ||
| runtime-image: ${{ steps.set-outputs.outputs.runtime-image }} | ||
| steps: | ||
| - name: Checkout | ||
| uses: actions/checkout@v4 | ||
|
|
||
| - name: Compute dependency hash | ||
| id: deps-hash | ||
| run: | | ||
| HASH=$(cat \ | ||
| pnpm-lock.yaml \ | ||
| package.json \ | ||
| pnpm-workspace.yaml \ | ||
| Dockerfile.base \ | ||
| apps/backend/package.json \ | ||
| apps/frontend/package.json \ | ||
| packages/shared/package.json \ | ||
| packages/sdk/package.json \ | ||
| | sha256sum | cut -c1-16) | ||
| echo "hash=$HASH" >> "$GITHUB_OUTPUT" | ||
| echo "Dependency hash: $HASH" | ||
|
|
||
| - name: Log in to GitHub Container Registry | ||
| uses: docker/login-action@c94ce9fb468520275223c153574b00df6fe4bcc9 # v3.7.0 | ||
| with: | ||
| registry: ${{ env.REGISTRY }} | ||
| username: ${{ github.actor }} | ||
| password: ${{ secrets.GITHUB_TOKEN }} | ||
|
|
||
| - name: Check if images already exist | ||
| id: check-exists | ||
| if: ${{ !inputs.force-rebuild }} | ||
| run: | | ||
| BUILD_EXISTS=false | ||
| RUNTIME_EXISTS=false | ||
|
|
||
| if docker manifest inspect "${{ env.IMAGE_NAME }}:build-${{ steps.deps-hash.outputs.hash }}" > /dev/null 2>&1; then | ||
| BUILD_EXISTS=true | ||
| echo "build-base image already exists" | ||
| fi | ||
|
|
||
| if docker manifest inspect "${{ env.IMAGE_NAME }}:runtime-${{ steps.deps-hash.outputs.hash }}" > /dev/null 2>&1; then | ||
| RUNTIME_EXISTS=true | ||
| echo "runtime-base image already exists" | ||
| fi | ||
|
|
||
| if [[ "$BUILD_EXISTS" == "true" && "$RUNTIME_EXISTS" == "true" ]]; then | ||
| echo "skip=true" >> "$GITHUB_OUTPUT" | ||
| echo "Both images exist, skipping build" | ||
| else | ||
| echo "skip=false" >> "$GITHUB_OUTPUT" | ||
| fi | ||
|
|
||
| - name: Set up Docker Buildx | ||
| if: steps.check-exists.outputs.skip != 'true' | ||
| uses: docker/setup-buildx-action@8d2750c68a42422c14e847fe6c8ac0403b4cbd6f # v3.12.0 | ||
|
|
||
| - name: Build and push build-base image | ||
| if: steps.check-exists.outputs.skip != 'true' | ||
| uses: docker/build-push-action@10e90e3645eae34f1e60eeb005ba3a3d33f178e8 # v6.19.2 | ||
| with: | ||
| context: . | ||
| file: Dockerfile.base | ||
| target: build-base | ||
| push: true | ||
| tags: | | ||
| ${{ env.IMAGE_NAME }}:build-${{ steps.deps-hash.outputs.hash }} | ||
| ${{ env.IMAGE_NAME }}:build-latest | ||
| cache-from: | | ||
| type=registry,ref=${{ env.IMAGE_NAME }}:build-latest | ||
|
|
||
| - name: Build and push runtime-base image | ||
| if: steps.check-exists.outputs.skip != 'true' | ||
| uses: docker/build-push-action@10e90e3645eae34f1e60eeb005ba3a3d33f178e8 # v6.19.2 | ||
| with: | ||
| context: . | ||
| file: Dockerfile.base | ||
| target: runtime-base | ||
| push: true | ||
| tags: | | ||
| ${{ env.IMAGE_NAME }}:runtime-${{ steps.deps-hash.outputs.hash }} | ||
| ${{ env.IMAGE_NAME }}:runtime-latest | ||
| cache-from: | | ||
| type=registry,ref=${{ env.IMAGE_NAME }}:runtime-latest | ||
|
|
||
| - name: Set outputs | ||
| id: set-outputs | ||
| run: | | ||
| echo "build-image=${{ env.IMAGE_NAME }}:build-${{ steps.deps-hash.outputs.hash }}" >> "$GITHUB_OUTPUT" | ||
| echo "runtime-image=${{ env.IMAGE_NAME }}:runtime-${{ steps.deps-hash.outputs.hash }}" >> "$GITHUB_OUTPUT" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| name: Build Release Image | ||
|
|
||
| on: | ||
| push: | ||
| tags: ['v*'] | ||
| workflow_dispatch: | ||
| inputs: | ||
| tag: | ||
| description: 'Version tag to build (e.g., v7.4.5)' | ||
| required: true | ||
| type: string | ||
|
|
||
| permissions: | ||
| contents: read | ||
| packages: write | ||
|
|
||
| env: | ||
| IMAGE_NAME: ghcr.io/opengovsg/formsg | ||
|
|
||
| jobs: | ||
| ensure-base-images: | ||
| name: Ensure base images | ||
| uses: ./.github/workflows/build-base-images.yml | ||
| permissions: | ||
| contents: read | ||
| packages: write | ||
|
|
||
| build-release: | ||
| name: Build release image | ||
| needs: ensure-base-images | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: Checkout | ||
| uses: actions/checkout@v4 | ||
|
|
||
| - name: Determine version tag | ||
| id: version | ||
| run: | | ||
| if [[ "${{ github.event_name }}" == "push" ]]; then | ||
| TAG="${GITHUB_REF#refs/tags/}" | ||
| else | ||
| TAG="${{ inputs.tag }}" | ||
| fi | ||
| echo "tag=$TAG" >> "$GITHUB_OUTPUT" | ||
| echo "Building release image for $TAG" | ||
|
|
||
| - name: Log in to GitHub Container Registry | ||
| uses: docker/login-action@c94ce9fb468520275223c153574b00df6fe4bcc9 # v3.7.0 | ||
| with: | ||
| registry: ghcr.io | ||
| username: ${{ github.actor }} | ||
| password: ${{ secrets.GITHUB_TOKEN }} | ||
|
|
||
| - name: Check if release image already exists | ||
| id: check-exists | ||
| run: | | ||
| IMAGE="${{ env.IMAGE_NAME }}:release-${{ steps.version.outputs.tag }}" | ||
| if docker manifest inspect "$IMAGE" > /dev/null 2>&1; then | ||
| echo "skip=true" >> "$GITHUB_OUTPUT" | ||
| echo "Release image already exists: $IMAGE" | ||
| else | ||
| echo "skip=false" >> "$GITHUB_OUTPUT" | ||
| fi | ||
|
|
||
| - name: Set up Docker Buildx | ||
| if: steps.check-exists.outputs.skip != 'true' | ||
| uses: docker/setup-buildx-action@8d2750c68a42422c14e847fe6c8ac0403b4cbd6f # v3.12.0 | ||
|
|
||
| - name: Build and push release image | ||
| if: steps.check-exists.outputs.skip != 'true' | ||
| uses: docker/build-push-action@10e90e3645eae34f1e60eeb005ba3a3d33f178e8 # v6.19.2 | ||
| with: | ||
| context: . | ||
| file: Dockerfile.production | ||
| push: true | ||
| tags: | | ||
| ${{ env.IMAGE_NAME }}:release-${{ steps.version.outputs.tag }} | ||
| build-args: | | ||
| BUILD_BASE_IMAGE=${{ needs.ensure-base-images.outputs.build-base-image }} | ||
| APP_VERSION=${{ steps.version.outputs.tag }} | ||
| DD_RUM_APP_ID=${{ secrets.DD_RUM_APP_ID }} | ||
| DD_RUM_CLIENT_TOKEN=${{ secrets.DD_RUM_CLIENT_TOKEN }} | ||
| cache-from: | | ||
| type=registry,ref=${{ env.IMAGE_NAME }}:build-latest |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| # syntax=docker/dockerfile:1 | ||
|
|
||
| # ============================================================================= | ||
| # build-base: Full build toolchain with pre-installed node_modules | ||
| # ============================================================================= | ||
| FROM node:22.22.2-alpine3.22 AS build-base | ||
| ENV PNPM_HOME="/pnpm" | ||
| ENV PATH="$PNPM_HOME:$PATH" | ||
| RUN corepack enable && corepack prepare pnpm@10.30.3 --activate | ||
|
|
||
| # Install build dependencies (same as Dockerfile.production build stage) | ||
| RUN apk upgrade --no-cache && \ | ||
| apk --no-cache add --virtual native-deps \ | ||
| g++ gcc libgcc libstdc++ linux-headers autoconf automake make nasm python3 git curl \ | ||
| build-base cairo-dev pango-dev giflib-dev libjpeg-turbo-dev librsvg-dev && \ | ||
| pnpm add -g node-gyp@12.2.0 | ||
|
|
||
| WORKDIR /build | ||
|
|
||
| # Copy only dependency-related files for layer caching | ||
| COPY package.json pnpm-lock.yaml pnpm-workspace.yaml ./ | ||
| COPY apps/backend/package.json ./apps/backend/ | ||
| COPY packages/shared/package.json ./packages/shared/ | ||
| COPY packages/sdk/package.json ./packages/sdk/ | ||
| COPY apps/frontend/package.json ./apps/frontend/ | ||
|
|
||
| RUN pnpm install --frozen-lockfile --filter "!services/*" --filter "!packages/react-email-preview/*" | ||
|
|
||
| # ============================================================================= | ||
| # runtime-base: Minimal runtime image with system deps and non-root user | ||
| # ============================================================================= | ||
| FROM node:22.22.2-alpine3.22 AS runtime-base | ||
| ENV PNPM_HOME="/pnpm" | ||
| ENV PATH="$PNPM_HOME:$PATH" | ||
| RUN corepack enable && corepack prepare pnpm@10.30.3 --activate | ||
|
|
||
| RUN apk upgrade --no-cache && \ | ||
| apk add --no-cache \ | ||
| nss \ | ||
| freetype \ | ||
| freetype-dev \ | ||
| harfbuzz \ | ||
| ca-certificates \ | ||
| ttf-freefont \ | ||
| tini \ | ||
| cairo \ | ||
| pango \ | ||
| librsvg \ | ||
| giflib | ||
|
|
||
| # Pre-create non-root user | ||
| RUN addgroup -S formsguser && adduser -S -g formsguser formsguser |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.