Skip to content

Commit 7e65d8e

Browse files
fix: add build-time environment variables for Docker build (#23)
* fix: add build-time environment variables for Docker build The Docker build process was failing because the Nuxt build requires environment variables (DATABASE_URL, JWT_SECRET, etc.) during build time for GraphQL schema generation and other build processes. - Add ARG declarations in Dockerfile for required environment variables - Set ENV variables in build stage using ARG values - Add build-args to GitHub Actions workflow with dummy values - This resolves the "exit code: 1" error in Docker Hub publishing workflow Fixes the build failure in GitHub Actions workflow run. * feat: update Dockerfile and workflows to include AUTO_MIGRATE variable and SDK dependency * feat: enhance Docker workflow to support multi-architecture builds for AMD64 and ARM64 * fix: update build-arm64 job to use ubuntu-latest instead of ubuntu-24.04-arm64 * fix: remove pull request conditions for Docker image builds and manifest creation
1 parent 6fd0894 commit 7e65d8e

File tree

5 files changed

+130
-23
lines changed

5 files changed

+130
-23
lines changed

.dockerignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,7 @@ components.json
5454
graphql.config.ts
5555

5656
# Development utilities
57-
nitroping
57+
# nitroping # SDK needed for build
58+
59+
# Include SDK for build
60+
sdk

.github/workflows/docker-publish.yml

Lines changed: 103 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,13 @@ on:
66
- main
77
tags:
88
- 'v*'
9-
pull_request:
10-
branches:
11-
- main
129

1310
env:
1411
REGISTRY: docker.io
1512
IMAGE_NAME: productdevbook/nitroping
1613

1714
jobs:
18-
build:
15+
build-amd64:
1916
runs-on: ubuntu-latest
2017
permissions:
2118
contents: read
@@ -29,7 +26,6 @@ jobs:
2926
uses: docker/setup-buildx-action@v3
3027

3128
- name: Log in to Docker Hub
32-
if: github.event_name != 'pull_request'
3329
uses: docker/login-action@v3
3430
with:
3531
username: ${{ secrets.DOCKER_USERNAME }}
@@ -41,27 +37,115 @@ jobs:
4137
with:
4238
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
4339
tags: |
44-
type=ref,event=branch
45-
type=ref,event=pr
46-
type=semver,pattern={{version}}
47-
type=semver,pattern={{major}}.{{minor}}
48-
type=semver,pattern={{major}}
49-
type=raw,value=latest,enable={{is_default_branch}}
40+
type=ref,event=branch,suffix=-amd64
41+
type=semver,pattern={{version}},suffix=-amd64
42+
type=semver,pattern={{major}}.{{minor}},suffix=-amd64
43+
type=semver,pattern={{major}},suffix=-amd64
44+
type=raw,value=latest-amd64,enable={{is_default_branch}}
45+
46+
- name: Build and push AMD64 Docker image
47+
uses: docker/build-push-action@v5
48+
with:
49+
context: .
50+
file: ./Dockerfile
51+
target: production
52+
platforms: linux/amd64
53+
push: true
54+
tags: ${{ steps.meta.outputs.tags }}
55+
labels: ${{ steps.meta.outputs.labels }}
56+
cache-from: type=gha,scope=amd64
57+
cache-to: type=gha,mode=max,scope=amd64
58+
build-args: |
59+
DATABASE_URL=postgresql://dummy:dummy@localhost:5432/dummy
60+
JWT_SECRET=0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef
61+
ENCRYPTION_KEY=0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef
62+
WEBHOOK_SECRET=0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef
63+
AUTO_MIGRATE=false
64+
65+
build-arm64:
66+
runs-on: ubuntu-latest
67+
permissions:
68+
contents: read
69+
packages: write
70+
71+
steps:
72+
- name: Checkout repository
73+
uses: actions/checkout@v4
74+
75+
- name: Set up Docker Buildx
76+
uses: docker/setup-buildx-action@v3
77+
78+
- name: Log in to Docker Hub
79+
uses: docker/login-action@v3
80+
with:
81+
username: ${{ secrets.DOCKER_USERNAME }}
82+
password: ${{ secrets.DOCKER_PASSWORD }}
5083

51-
- name: Build and push Docker image
84+
- name: Extract metadata
85+
id: meta
86+
uses: docker/metadata-action@v5
87+
with:
88+
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
89+
tags: |
90+
type=ref,event=branch,suffix=-arm64
91+
type=semver,pattern={{version}},suffix=-arm64
92+
type=semver,pattern={{major}}.{{minor}},suffix=-arm64
93+
type=semver,pattern={{major}},suffix=-arm64
94+
type=raw,value=latest-arm64,enable={{is_default_branch}}
95+
96+
- name: Build and push ARM64 Docker image
5297
uses: docker/build-push-action@v5
5398
with:
5499
context: .
55100
file: ./Dockerfile
56101
target: production
57-
platforms: linux/amd64,linux/arm64
58-
push: ${{ github.event_name != 'pull_request' }}
102+
platforms: linux/arm64
103+
push: true
59104
tags: ${{ steps.meta.outputs.tags }}
60105
labels: ${{ steps.meta.outputs.labels }}
61-
cache-from: type=gha
62-
cache-to: type=gha,mode=max
106+
cache-from: type=gha,scope=arm64
107+
cache-to: type=gha,mode=max,scope=arm64
108+
build-args: |
109+
DATABASE_URL=postgresql://dummy:dummy@localhost:5432/dummy
110+
JWT_SECRET=0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef
111+
ENCRYPTION_KEY=0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef
112+
WEBHOOK_SECRET=0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef
113+
AUTO_MIGRATE=false
114+
115+
create-manifest:
116+
runs-on: ubuntu-latest
117+
needs: [build-amd64, build-arm64]
118+
permissions:
119+
contents: read
120+
packages: write
121+
122+
steps:
123+
- name: Set up Docker Buildx
124+
uses: docker/setup-buildx-action@v3
125+
126+
- name: Log in to Docker Hub
127+
uses: docker/login-action@v3
128+
with:
129+
username: ${{ secrets.DOCKER_USERNAME }}
130+
password: ${{ secrets.DOCKER_PASSWORD }}
131+
132+
- name: Extract metadata for manifest
133+
id: meta
134+
uses: docker/metadata-action@v5
135+
with:
136+
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
137+
tags: |
138+
type=ref,event=branch
139+
type=semver,pattern={{version}}
140+
type=semver,pattern={{major}}.{{minor}}
141+
type=semver,pattern={{major}}
142+
type=raw,value=latest,enable={{is_default_branch}}
63143
64-
- name: Test Docker image
65-
if: github.event_name == 'pull_request'
144+
- name: Create and push manifest
66145
run: |
67-
docker run --rm ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.meta.outputs.version }} --help || true
146+
for tag in ${{ steps.meta.outputs.tags }}; do
147+
docker buildx imagetools create \
148+
--tag ${tag} \
149+
${tag}-amd64 \
150+
${tag}-arm64
151+
done

Dockerfile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,21 @@ RUN pnpm install --frozen-lockfile
1010

1111
FROM base AS build
1212

13+
# Build arguments for environment variables needed during build
14+
ARG DATABASE_URL
15+
ARG JWT_SECRET
16+
ARG ENCRYPTION_KEY
17+
ARG WEBHOOK_SECRET
18+
ARG AUTO_MIGRATE
19+
20+
# Set environment variables for build
1321
ENV NODE_OPTIONS="--max-old-space-size=4096"
22+
ENV DATABASE_URL=$DATABASE_URL
23+
ENV JWT_SECRET=$JWT_SECRET
24+
ENV ENCRYPTION_KEY=$ENCRYPTION_KEY
25+
ENV WEBHOOK_SECRET=$WEBHOOK_SECRET
26+
ENV AUTO_MIGRATE=$AUTO_MIGRATE
27+
1428
RUN pnpm run build
1529

1630
FROM base AS dev

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"jsonwebtoken": "^9.0.2",
4646
"lucide-vue-next": "^0.539.0",
4747
"nitro-graphql": "^1.1.3",
48-
"nitroping": "link:./sdk",
48+
"nitroping": "^0.1.0",
4949
"nuxt": "^4.0.3",
5050
"pinia": "^3.0.3",
5151
"postgres": "^3.4.7",

pnpm-lock.yaml

Lines changed: 8 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)