|
1 |
| -name: "Setup Docker Build Environment" |
2 |
| -description: "Set up Node.js, install Foundry, and set up Bun. Runs Yarn install run docker build" |
| 1 | +name: "Build and Push Next App Docker Image" |
| 2 | +description: "Builds the next-app Docker image using Buildx, caches layers with GHCR, tags, and pushes to GHCR." |
| 3 | + |
3 | 4 | inputs:
|
4 |
| - build-nextjs-docker: |
5 |
| - description: "Whether to build Next.js docker image" |
6 |
| - required: false |
7 |
| - default: "true" |
8 |
| - push-nextjs-docker-image: |
9 |
| - description: "Whether to push build to dockerhub" |
10 |
| - required: false |
11 |
| - default: "false" |
12 |
| - dockerhub_username: |
13 |
| - description: "Username to login to dockerhub" |
14 |
| - required: false |
15 |
| - dockerhub_password: |
16 |
| - description: "Password to login to dockerhub" |
| 5 | + build-args: |
| 6 | + description: "Build arguments to pass to docker buildx build (e.g., 'ARG1=value1 ARG2=value2')" |
17 | 7 | required: false
|
| 8 | + default: "" |
| 9 | + ghcr-token: |
| 10 | + description: "Token for authenticating with GHCR" |
| 11 | + required: true |
| 12 | + default: ${{ github.token }} |
| 13 | + image-name: |
| 14 | + description: "Base name for the image in GHCR (e.g., next-app)" |
| 15 | + required: true |
| 16 | + default: "next-app" |
| 17 | + |
| 18 | +outputs: |
| 19 | + image-tag: |
| 20 | + description: "The full GHCR tag of the pushed image (e.g., ghcr.io/owner/repo/next-app:sha)" |
| 21 | + value: ${{ steps.build-and-push.outputs.image-tag }} |
| 22 | + |
18 | 23 | runs:
|
19 | 24 | using: "composite"
|
20 | 25 | steps:
|
21 |
| - - name: Use Node.js |
22 |
| - if: ${{ inputs.build-nextjs-docker == 'true' }} |
23 |
| - uses: actions/setup-node@v4 |
24 |
| - with: |
25 |
| - node-version-file: .node-version |
26 |
| - cache: "yarn" |
27 |
| - - name: Setup Bun |
28 |
| - if: ${{ inputs.build-nextjs-docker == 'true' }} |
29 |
| - uses: oven-sh/setup-bun@v1 |
30 |
| - - name: Install Foundry |
31 |
| - if: ${{ inputs.build-nextjs-docker == 'true' }} |
32 |
| - uses: foundry-rs/foundry-toolchain@v1 |
| 26 | + - name: Login to GitHub Container Registry |
| 27 | + uses: docker/login-action@v3 |
33 | 28 | with:
|
34 |
| - version: stable |
35 |
| - - name: Yarn Install |
36 |
| - if: ${{ inputs.build-nextjs-docker == 'true' }} |
| 29 | + registry: ghcr.io |
| 30 | + username: ${{ github.actor }} |
| 31 | + password: ${{ inputs.ghcr-token }} |
| 32 | + |
| 33 | + - name: Generate Image Tag |
| 34 | + id: generate-tag |
37 | 35 | shell: bash
|
38 |
| - run: yarn install --immutable |
39 |
| - - name: Build Next.js |
40 |
| - if: ${{ inputs.build-nextjs-docker == 'true' }} |
| 36 | + run: | |
| 37 | + SHORT_SHA=$(git rev-parse --short=10 HEAD) |
| 38 | + IMAGE_TAG="ghcr.io/${{ github.repository_owner }}/${{ github.event.repository.name }}/${{ inputs.image-name }}:${SHORT_SHA}" |
| 39 | + echo "IMAGE_TAG=${IMAGE_TAG,,}" >> $GITHUB_OUTPUT # Lowercase for Docker compatibility |
| 40 | + |
| 41 | + - name: Set Environment Variables from Template |
41 | 42 | shell: bash
|
42 | 43 | run: |
|
43 |
| - cp .env.local.template .env.local |
44 |
| - yarn web:docker:build |
45 |
| - rm .env.local |
46 |
| - - uses: docker/login-action@v3 |
47 |
| - if: ${{ inputs.build-nextjs-docker == 'true' }} && ${{ inputs.push-nextjs-docker-image == 'true' }} |
48 |
| - with: |
49 |
| - username: ${{ inputs.dockerhub_username }} |
50 |
| - password: ${{ inputs.dockerhub_password }} |
51 |
| - - name: Push to dockerhub |
| 44 | + if [ -f "./scripts/github-actions-set-env-from-template.sh" ]; then |
| 45 | + chmod +x ./scripts/github-actions-set-env-from-template.sh |
| 46 | + ./scripts/github-actions-set-env-from-template.sh .env.local.template |
| 47 | + else |
| 48 | + echo "Warning: Environment script not found, using default environment variables" |
| 49 | + fi |
| 50 | +
|
| 51 | + - name: Build and Push Docker Image |
| 52 | + id: build-and-push |
52 | 53 | shell: bash
|
53 |
| - if: ${{ inputs.build-nextjs-docker == 'true' }} && ${{ inputs.push-nextjs-docker-image == 'true' }} |
54 |
| - run: yarn web:docker:push |
| 54 | + run: | |
| 55 | + BUILD_ARGS_STRING="" |
| 56 | + if [[ -n "${{ inputs.build-args }}" ]]; then |
| 57 | + for arg in ${{ inputs.build-args }}; do |
| 58 | + BUILD_ARGS_STRING+=" --build-arg $arg" |
| 59 | + done |
| 60 | + fi |
| 61 | + |
| 62 | + # Add environment variables from template as build arguments |
| 63 | + # Only include variables starting with NEXT_PUBLIC_ for the Next.js build |
| 64 | + for var in $(env | grep "^NEXT_PUBLIC_" | cut -d= -f1); do |
| 65 | + BUILD_ARGS_STRING+=" --build-arg ${var}=${!var}" |
| 66 | + done |
| 67 | +
|
| 68 | + echo "Building image: ${{ steps.generate-tag.outputs.IMAGE_TAG }}" |
| 69 | + echo "Using build args:${BUILD_ARGS_STRING}" # Be careful not to expose secrets here |
| 70 | +
|
| 71 | + docker buildx build \ |
| 72 | + --tag ${{ steps.generate-tag.outputs.IMAGE_TAG }} \ |
| 73 | + --cache-from type=gha \ |
| 74 | + --cache-to type=gha,mode=max \ |
| 75 | + --push \ |
| 76 | + -f ./apps/next/Dockerfile \ |
| 77 | + ${BUILD_ARGS_STRING} \ |
| 78 | + . |
| 79 | +
|
| 80 | + echo "image-tag=${{ steps.generate-tag.outputs.IMAGE_TAG }}" >> $GITHUB_OUTPUT |
0 commit comments