From 1e3d1b9ed3e91150e36c30120e360de9b3d4a07e Mon Sep 17 00:00:00 2001 From: Alireza Ahmadi Date: Sun, 20 Jul 2025 15:36:53 +0200 Subject: [PATCH] faster docker build --- .github/workflows/docker.yml | 60 ++++++++++++++++++++++++++++++------ Dockerfile.frontend-artifact | 36 ++++++++++++++++++++++ 2 files changed, 86 insertions(+), 10 deletions(-) create mode 100644 Dockerfile.frontend-artifact diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index 0079443..6d49a14 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -6,15 +6,47 @@ on: workflow_dispatch: jobs: - build: + frontend-build: runs-on: ubuntu-22.04 + steps: + - name: Checkout repository + uses: actions/checkout@v4.2.2 + with: + submodules: recursive + - name: Set up Node.js + uses: actions/setup-node@v4 + with: + node-version: 20 + - name: Install dependencies and build frontend + run: | + cd frontend + npm install + npm run build + - name: Upload frontend build artifact + uses: actions/upload-artifact@v4 + with: + name: frontend-dist + path: frontend/dist/ + build: + needs: frontend-build + strategy: + matrix: + platform: + - linux/amd64 + - linux/386 + - linux/arm64/v8 + - linux/arm/v7 + - linux/arm/v6 + runs-on: ubuntu-22.04 steps: - name: Checkout repository uses: actions/checkout@v4.2.2 + - name: Download frontend build artifact + uses: actions/download-artifact@v4 with: - submodules: recursive - + name: frontend-dist + path: frontend_dist - name: Docker meta id: meta uses: docker/metadata-action@v5 @@ -26,31 +58,39 @@ jobs: type=ref,event=branch type=ref,event=tag type=pep440,pattern={{version}} - - name: Set up QEMU uses: docker/setup-qemu-action@v3 - - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 - + with: + install: true + buildkitd-flags: --debug + - name: Cache Docker layers + uses: actions/cache@v4 + with: + path: /tmp/.buildx-cache + key: ${{ runner.os }}-buildx-${{ github.sha }} + restore-keys: | + ${{ runner.os }}-buildx- - name: Login to Docker Hub uses: docker/login-action@v3 with: username: ${{ secrets.DOCKER_HUB_USERNAME }} password: ${{ secrets.DOCKER_HUB_TOKEN }} - - name: Login to GHCR uses: docker/login-action@v3 with: registry: ghcr.io username: ${{ github.repository_owner }} password: ${{ secrets.GITHUB_TOKEN }} - - name: Build and push uses: docker/build-push-action@v6 with: context: . + file: Dockerfile.frontend-artifact push: true - platforms: linux/amd64, linux/arm64/v8, linux/arm/v7, linux/arm/v6, linux/386 + platforms: ${{ matrix.platform }} tags: ${{ steps.meta.outputs.tags }} - labels: ${{ steps.meta.outputs.labels }} \ No newline at end of file + labels: ${{ steps.meta.outputs.labels }} + cache-from: type=local,src=/tmp/.buildx-cache + cache-to: type=local,dest=/tmp/.buildx-cache,mode=max \ No newline at end of file diff --git a/Dockerfile.frontend-artifact b/Dockerfile.frontend-artifact new file mode 100644 index 0000000..d2e9617 --- /dev/null +++ b/Dockerfile.frontend-artifact @@ -0,0 +1,36 @@ +FROM golang:1.24-alpine AS backend-builder +WORKDIR /app +ARG TARGETARCH +ENV CGO_ENABLED=1 +ENV CGO_CFLAGS="-D_LARGEFILE64_SOURCE" +ENV GOARCH=$TARGETARCH + +RUN apk update && apk add --no-cache \ + gcc \ + musl-dev \ + libc-dev \ + make \ + git \ + wget \ + unzip \ + bash + +ENV CC=gcc + +COPY . . +# Copy pre-built frontend files from a known location (provided by workflow artifact) +COPY frontend_dist/ /app/web/html/ + +RUN go build -ldflags="-w -s" \ + -tags "with_quic,with_grpc,with_utls,with_acme,with_gvisor" \ + -o sui main.go + +FROM --platform=$TARGETPLATFORM alpine +LABEL org.opencontainers.image.authors="alireza7@gmail.com" +ENV TZ=Asia/Tehran +WORKDIR /app +RUN apk add --no-cache --update ca-certificates tzdata +COPY --from=backend-builder /app/sui /app/ +COPY entrypoint.sh /app/ +VOLUME [ "s-ui" ] +ENTRYPOINT [ "./entrypoint.sh" ] \ No newline at end of file