Skip to content

Commit 0da7c2f

Browse files
authored
feat: build multi-arch docker images (#5)
1 parent 34a37c7 commit 0da7c2f

File tree

7 files changed

+117
-30
lines changed

7 files changed

+117
-30
lines changed
Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: build-docker-image
1+
name: Build and Push Docker Images
22

33
on:
44
push:
@@ -9,11 +9,13 @@ on:
99
jobs:
1010
build:
1111
runs-on: ubuntu-latest
12-
1312
steps:
1413
- name: Checkout code
1514
uses: actions/checkout@v4
1615

16+
- name: Set up QEMU
17+
uses: docker/setup-qemu-action@v3
18+
1719
- name: Set up Docker Buildx
1820
uses: docker/setup-buildx-action@v3
1921

@@ -23,17 +25,7 @@ jobs:
2325
username: ${{ secrets.DOCKER_USERNAME }}
2426
password: ${{ secrets.DOCKER_PASSWORD }}
2527

26-
- name: Build base image
27-
run: bash scripts/build-base-image.sh
28-
29-
- name: Build nginx image
30-
run: bash scripts/build-nginx.sh
31-
32-
- name: Build standalone image
33-
run: bash scripts/build-standalone.sh
34-
35-
- name: Build static image
36-
run: bash scripts/build-static.sh
37-
38-
- name: Push images to Docker Hub
39-
run: bash scripts/push-images.sh
28+
- name: Build and push multi-arch images
29+
run: |
30+
chmod +x scripts/build-multiarch.sh
31+
bash scripts/build-multiarch.sh

โ€Ždocker/latest/Dockerfile.baseโ€Ž

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
FROM node:20-alpine3.19 AS builder
1+
FROM --platform=$BUILDPLATFORM node:20-alpine3.19 AS builder
22
ENV LANG="en_US.UTF-8"
33
ENV LANGUAGE="en_US.UTF-8"
44
ENV LC_ALL="en_US.UTF-8"
5-
RUN apk add curl
5+
RUN apk add --no-cache curl unzip
66
RUN curl -L "https://github.com/doocs/md/archive/refs/heads/main.zip" -o "main.zip" && unzip "main.zip" && mv "md-main" /app
77
WORKDIR /app
88
COPY ./patch/vite.config.ts /app/vite.config.ts
99
ENV NODE_OPTIONS="--openssl-legacy-provider"
1010
RUN npm i && npm run build
11-
RUN touch /app/dist/.nojekyll
1211

1312
FROM scratch
1413
LABEL MAINTAINER="ylb<[email protected]>"
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
ARG VER_NGX="1.21.6-alpine"
22

3-
FROM "doocs/md:latest-assets" AS assets
4-
FROM "nginx:$VER_NGX"
3+
FROM --platform=$BUILDPLATFORM doocs/md:latest-assets AS assets
4+
FROM --platform=$TARGETPLATFORM nginx:${VER_NGX}
55
LABEL MAINTAINER="ylb<[email protected]>"
6-
COPY --from=assets /app/* /usr/share/nginx/html
6+
COPY --from=assets /app /usr/share/nginx/html
Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
ARG VER_GOLANG=1.17.6-alpine3.15
22
ARG VER_ALPINE=3.15
33

4-
FROM "doocs/md:latest-assets" AS assets
4+
FROM --platform=$BUILDPLATFORM "doocs/md:latest-assets" AS assets
55

6-
FROM "golang:$VER_GOLANG" AS gobuilder
6+
FROM --platform=$BUILDPLATFORM "golang:$VER_GOLANG" AS gobuilder
7+
ARG TARGETARCH
8+
ARG TARGETOS
79
COPY --from=assets /app/* /app/assets/
810
COPY server/main.go /app
911
RUN apk add git bash gcc musl-dev upx
1012
WORKDIR /app
13+
ENV GOOS=$TARGETOS GOARCH=$TARGETARCH
1114
RUN go build -ldflags "-w -s" -o md main.go && \
1215
apk add upx && \
13-
upx -9 -o md.minify md
16+
if [ "$TARGETARCH" = "amd64" ]; then upx -9 -o md.minify md; else cp md md.minify; fi
1417

15-
FROM "alpine:$VER_ALPINE"
18+
FROM --platform=$TARGETPLATFORM "alpine:$VER_ALPINE"
1619
LABEL MAINTAINER="ylb<[email protected]>"
1720
COPY --from=gobuilder /app/md.minify /bin/md
1821
EXPOSE 80
19-
CMD ["md"]
22+
CMD ["md"]

โ€Ždocker/latest/Dockerfile.staticโ€Ž

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
FROM doocs/md:latest-assets AS assets
1+
FROM --platform=$BUILDPLATFORM doocs/md:latest-assets AS assets
22

33
# detail https://github.com/lipanski/docker-static-website/blob/master/Dockerfile
4-
FROM lipanski/docker-static-website
4+
FROM --platform=$TARGETPLATFORM lipanski/docker-static-website
55

66
WORKDIR /home/static
77

8-
COPY --from=assets /app/* /home/static
8+
COPY --from=assets /app /home/static
99

1010
EXPOSE 80
1111

โ€Ždocker/latest/patch/vite.config.tsโ€Ž

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import AutoImport from 'unplugin-auto-import/vite'
88
import Components from 'unplugin-vue-components/vite'
99
import { defineConfig } from 'vite'
1010
import { nodePolyfills } from 'vite-plugin-node-polyfills'
11+
import { VitePluginRadar } from 'vite-plugin-radar'
1112
import vueDevTools from 'vite-plugin-vue-devtools'
1213

1314
// https://vitejs.dev/config/
@@ -27,6 +28,11 @@ export default defineConfig({
2728
// fs: 'memfs',
2829
},
2930
}),
31+
VitePluginRadar({
32+
analytics: {
33+
id: `G-7NZL3PZ0NK`,
34+
}
35+
}),
3036
process.env.ANALYZE === `true` && visualizer({
3137
emitFile: true,
3238
filename: `stats.html`,

โ€Žscripts/build-multiarch.shโ€Ž

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
RELEASE_DIR="./docker"
6+
REPO_NAME="doocs/md"
7+
PLATFORMS="linux/amd64,linux/arm64"
8+
9+
echo "๐Ÿ”ง Multi-arch Docker build started..."
10+
echo "๐Ÿ“ Scanning directory: $RELEASE_DIR"
11+
12+
for app_ver in "$RELEASE_DIR"/*; do
13+
[ -d "$app_ver" ] || continue
14+
15+
tag=$(basename "$app_ver")
16+
env_file="$app_ver/.env"
17+
18+
if [ ! -f "$env_file" ]; then
19+
echo "โš ๏ธ Skipping $tag - missing .env file"
20+
continue
21+
fi
22+
23+
set -a
24+
. "$env_file"
25+
set +a
26+
27+
echo "๐Ÿš€ Building images for version: $tag"
28+
echo " VER_APP: $VER_APP"
29+
echo " VER_NGX: $VER_NGX"
30+
echo " VER_GOLANG: $VER_GOLANG"
31+
echo " VER_ALPINE: $VER_ALPINE"
32+
33+
# ๆž„ๅปบ base ้•œๅƒ
34+
if [ -f "$app_ver/Dockerfile.base" ]; then
35+
echo "๐Ÿ“ฆ Building base image: $REPO_NAME:${VER_APP}-assets"
36+
docker buildx build \
37+
--platform "$PLATFORMS" \
38+
--build-arg VER_APP="$VER_APP" \
39+
-f "$app_ver/Dockerfile.base" \
40+
-t "$REPO_NAME:${VER_APP}-assets" \
41+
--push \
42+
"$app_ver"
43+
fi
44+
45+
# ๆž„ๅปบ nginx ้•œๅƒ
46+
if [ -f "$app_ver/Dockerfile.nginx" ]; then
47+
echo "๐Ÿ“ฆ Building nginx image: $REPO_NAME:${VER_APP}-nginx"
48+
docker buildx build \
49+
--platform "$PLATFORMS" \
50+
--build-arg VER_APP="$VER_APP" \
51+
--build-arg VER_NGX="$VER_NGX" \
52+
-f "$app_ver/Dockerfile.nginx" \
53+
-t "$REPO_NAME:${VER_APP}-nginx" \
54+
--push \
55+
"$app_ver"
56+
fi
57+
58+
# ๆž„ๅปบ standalone ้•œๅƒ
59+
if [ -f "$app_ver/Dockerfile.standalone" ]; then
60+
echo "๐Ÿ“ฆ Building standalone image: $REPO_NAME:${VER_APP}"
61+
docker buildx build \
62+
--platform "$PLATFORMS" \
63+
--build-arg VER_APP="$VER_APP" \
64+
--build-arg VER_NGX="$VER_NGX" \
65+
-f "$app_ver/Dockerfile.standalone" \
66+
-t "$REPO_NAME:${VER_APP}" \
67+
--push \
68+
"$app_ver"
69+
fi
70+
71+
# ๆž„ๅปบ static ้•œๅƒ
72+
if [ -f "$app_ver/Dockerfile.static" ]; then
73+
echo "๐Ÿ“ฆ Building static image: $REPO_NAME:${VER_APP}-static"
74+
docker buildx build \
75+
--platform "$PLATFORMS" \
76+
--build-arg VER_APP="$VER_APP" \
77+
--build-arg VER_NGX="$VER_NGX" \
78+
-f "$app_ver/Dockerfile.static" \
79+
-t "$REPO_NAME:${VER_APP}-static" \
80+
--push \
81+
"$app_ver"
82+
fi
83+
84+
echo "โœ… Completed version: $tag"
85+
done
86+
87+
echo "๐ŸŽ‰ All images built and pushed successfully."

0 commit comments

Comments
ย (0)