Skip to content
This repository was archived by the owner on Jun 28, 2025. It is now read-only.

Commit 7462009

Browse files
authored
Merge pull request #21 from vim/add-build-push-gh-action
Add build push GitHub Actions
2 parents 49a4bc4 + 8627801 commit 7462009

File tree

4 files changed

+143
-46
lines changed

4 files changed

+143
-46
lines changed

.github/workflows/build-push-cms.yml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
name: Build and Push CMS Docker Image
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
- development
8+
paths:
9+
- cms/**
10+
workflow_dispatch:
11+
12+
jobs:
13+
push_to_registries:
14+
name: Push Docker image to GHCR
15+
runs-on: ubuntu-latest
16+
permissions:
17+
packages: write
18+
contents: read
19+
steps:
20+
- name: Check out the repo
21+
uses: actions/checkout@v4
22+
23+
- name: Log in to the Container registry
24+
uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1
25+
with:
26+
registry: ghcr.io
27+
username: ${{ github.actor }}
28+
password: ${{ secrets.GITHUB_TOKEN }}
29+
30+
- name: Extract metadata (tags, labels) for Docker
31+
id: meta
32+
uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7
33+
with:
34+
tags: |
35+
type=ref,event=branch
36+
type=ref,event=pr
37+
type=semver,pattern={{version}}
38+
images: |
39+
ghcr.io/${{ github.repository }}/cms
40+
41+
- name: Build and push Docker images
42+
uses: docker/build-push-action@3b5e8027fcad23fda98b2e3ac259d8d67585f671
43+
with:
44+
context: ./cms
45+
file: ./cms/Dockerfile.prod
46+
push: true
47+
tags: ${{ steps.meta.outputs.tags }}
48+
labels: ${{ steps.meta.outputs.labels }}

.github/workflows/build-push-web.yml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
name: Build and Push Web Docker Image
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
- development
8+
paths:
9+
- web/**
10+
workflow_dispatch:
11+
12+
jobs:
13+
push_to_registries:
14+
name: Push Docker image to GHCR
15+
runs-on: ubuntu-latest
16+
permissions:
17+
packages: write
18+
contents: read
19+
steps:
20+
- name: Check out the repo
21+
uses: actions/checkout@v4
22+
23+
- name: Log in to the Container registry
24+
uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1
25+
with:
26+
registry: ghcr.io
27+
username: ${{ github.actor }}
28+
password: ${{ secrets.GITHUB_TOKEN }}
29+
30+
- name: Extract metadata (tags, labels) for Docker
31+
id: meta
32+
uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7
33+
with:
34+
tags: |
35+
type=ref,event=branch
36+
type=ref,event=pr
37+
type=semver,pattern={{version}}
38+
images: |
39+
ghcr.io/${{ github.repository }}/web
40+
41+
- name: Build and push Docker images
42+
uses: docker/build-push-action@3b5e8027fcad23fda98b2e3ac259d8d67585f671
43+
with:
44+
context: ./web
45+
file: ./web/Dockerfile.prod
46+
push: true
47+
tags: ${{ steps.meta.outputs.tags }}
48+
labels: ${{ steps.meta.outputs.labels }}

cms/Dockerfile.prod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM node:18-alpine as build
1+
FROM node:18.18-alpine3.17 as build
22
RUN apk update && apk add --no-cache build-base gcc autoconf automake zlib-dev libpng-dev vips-dev git > /dev/null 2>&1
33
ARG NODE_ENV=production
44
ENV NODE_ENV=${NODE_ENV}
@@ -12,7 +12,7 @@ WORKDIR /opt/app
1212
COPY . .
1313
RUN npm run build
1414

15-
FROM node:18-alpine
15+
FROM node:18.18-alpine3.17
1616
RUN apk add --no-cache vips-dev
1717
ARG NODE_ENV=production
1818
ENV NODE_ENV=${NODE_ENV}

web/Dockerfile.prod

Lines changed: 45 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,67 @@
11
FROM node:18-alpine AS base
22

3-
# Step 1. Rebuild the source code only when needed
4-
FROM base AS builder
5-
3+
# Install dependencies only when needed
4+
FROM base AS deps
5+
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
6+
RUN apk add --no-cache libc6-compat
67
WORKDIR /app
78

89
# Install dependencies based on the preferred package manager
910
COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./
10-
# Omit --production flag for TypeScript devDependencies
1111
RUN \
12-
if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
13-
elif [ -f package-lock.json ]; then npm ci; \
14-
elif [ -f pnpm-lock.yaml ]; then yarn global add pnpm && pnpm i; \
15-
# Allow install without lockfile, so example works even without Node.js installed locally
16-
else echo "Warning: Lockfile not found. It is recommended to commit lockfiles to version control." && yarn install; \
17-
fi
18-
19-
COPY src ./src
20-
COPY public ./public
21-
COPY next.config.js .
22-
COPY tsconfig.json .
23-
24-
# Environment variables must be present at build time
25-
# https://github.com/vercel/next.js/discussions/14030
26-
ARG ENV_VARIABLE
27-
ENV ENV_VARIABLE=${ENV_VARIABLE}
28-
ARG NEXT_PUBLIC_ENV_VARIABLE
29-
ENV NEXT_PUBLIC_ENV_VARIABLE=${NEXT_PUBLIC_ENV_VARIABLE}
30-
31-
ENV NEXT_TELEMETRY_DISABLED 1
32-
33-
# Build Next.js based on the preferred package manager
34-
RUN \
35-
if [ -f yarn.lock ]; then yarn build; \
36-
elif [ -f package-lock.json ]; then npm run build; \
37-
elif [ -f pnpm-lock.yaml ]; then pnpm build; \
38-
else yarn build; \
39-
fi
12+
if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
13+
elif [ -f package-lock.json ]; then npm ci; \
14+
elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm i --frozen-lockfile; \
15+
else echo "Lockfile not found." && exit 1; \
16+
fi
4017

4118

42-
# Step 2. Production image, copy all the files and run next
43-
FROM base AS runner
19+
# Rebuild the source code only when needed
20+
FROM base AS builder
21+
WORKDIR /app
22+
COPY --from=deps /app/node_modules ./node_modules
23+
COPY . .
24+
25+
# Next.js collects completely anonymous telemetry data about general usage.
26+
# Learn more here: https://nextjs.org/telemetry
27+
# Uncomment the following line in case you want to disable telemetry during the build.
28+
# ENV NEXT_TELEMETRY_DISABLED 1
4429

30+
RUN \
31+
if [ -f yarn.lock ]; then yarn run build; \
32+
elif [ -f package-lock.json ]; then npm run build; \
33+
elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm run build; \
34+
else echo "Lockfile not found." && exit 1; \
35+
fi
36+
37+
# Production image, copy all the files and run next
38+
FROM base AS runner
4539
WORKDIR /app
4640

41+
ENV NODE_ENV production
42+
# Uncomment the following line in case you want to disable telemetry during runtime.
43+
# ENV NEXT_TELEMETRY_DISABLED 1
44+
4745
RUN addgroup --system --gid 1001 nodejs
4846
RUN adduser --system --uid 1001 nextjs
49-
USER nextjs
5047

5148
COPY --from=builder /app/public ./public
5249

53-
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
50+
# Set the correct permission for prerender cache
51+
RUN mkdir .next
52+
RUN chown nextjs:nodejs .next
53+
54+
# Automatically leverage output traces to reduce image size
55+
# https://nextjs.org/docs/advanced-features/output-file-tracing
56+
# COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
5457
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
5558

56-
# Environment variables must be redefined at run time
57-
ARG ENV_VARIABLE
58-
ENV ENV_VARIABLE=${ENV_VARIABLE}
59-
ARG NEXT_PUBLIC_ENV_VARIABLE
60-
ENV NEXT_PUBLIC_ENV_VARIABLE=${NEXT_PUBLIC_ENV_VARIABLE}
59+
USER nextjs
6160

62-
# Uncomment the following line to disable telemetry at run time
63-
ENV NEXT_TELEMETRY_DISABLED 1
61+
EXPOSE 3000
6462

63+
ENV PORT 3000
6564

66-
CMD ["node", "server.js"]
65+
# server.js is created by next build from the standalone output
66+
# https://nextjs.org/docs/pages/api-reference/next-config-js/output
67+
CMD HOSTNAME="0.0.0.0" node server.js

0 commit comments

Comments
 (0)