From 41ced2ea05f188de573b8b39299db5b92e2f9449 Mon Sep 17 00:00:00 2001 From: Hintay Date: Sun, 8 Jan 2023 00:27:48 +0800 Subject: [PATCH] feat: multi platform docker build --- .github/workflows/build.yml | 43 +++++++++++++++++++++++++------------ Dockerfile | 8 ++++--- demo.Dockerfile | 7 ++++-- 3 files changed, 39 insertions(+), 19 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 4a5057c6..a3795abd 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -106,10 +106,6 @@ jobs: - name: Checkout uses: actions/checkout@v3 - - name: Get the version - id: get_version - run: echo "VERSION=${GITHUB_REF/refs\/tags\//}" >> $GITHUB_OUTPUT - - name: Set up Go uses: actions/setup-go@v3 with: @@ -131,10 +127,12 @@ jobs: export _NAME=nginx-ui-$(jq ".$GOOS[\"$GOARCH$GOARM\"].name" -r < .github/build/build_info.json) export _ARCH=$(jq ".$GOOS[\"$GOARCH$GOARM\"].arch" -r < .github/build/build_info.json) export _ABI=$(jq ".$GOOS[\"$GOARCH$GOARM\"].abi // \"\"" -r < .github/build/build_info.json) - echo "GOOS: $GOOS, GOARCH: $GOARCH, GOARM: $GOARM, ABI: $_ABI, RELEASE_NAME: $_NAME" + export _ARTIFACT=nginx-ui-$GOOS-$GOARCH$(if [[ "$GOARM" ]]; then echo "v$GOARM"; fi) + echo "GOOS: $GOOS, GOARCH: $GOARCH, GOARM: $GOARM, ABI: $_ABI, RELEASE_NAME: $_NAME, ARTIFACT_NAME: $_ARTIFACT" echo "ARCH_NAME=$_ARCH" >> $GITHUB_ENV echo "ABI=$_ABI" >> $GITHUB_ENV echo "DIST=$_NAME" >> $GITHUB_ENV + echo "ARTIFACT=$_ARTIFACT" >> $GITHUB_ENV - name: Install musl cross compiler if: env.GOOS == 'linux' @@ -176,7 +174,7 @@ jobs: - name: Archive backend artifacts uses: actions/upload-artifact@v3 with: - name: ${{ env.DIST }} + name: ${{ env.ARTIFACT }} path: dist/nginx-ui - name: Prepare publish @@ -191,49 +189,66 @@ jobs: with: files: ${{ env.DIST }}.tar.gz + docker-build: + runs-on: ubuntu-latest + needs: build + env: + PLATFORMS: linux/amd64,linux/arm64,linux/arm/v7,linux/arm/v6,linux/arm/v5 + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Get the version + id: get_version + run: echo "VERSION=${GITHUB_REF/refs\/tags\//}" >> $GITHUB_OUTPUT + + - name: Download artifacts + uses: actions/download-artifact@v3 + with: + path: ./dist + + - name: Prepare Artifacts + run: chmod +x ./dist/nginx-ui-*/nginx-ui + - name: Set up Docker Buildx - if: env.GOOS == 'linux' && env.GOARCH == 'amd64' id: buildx uses: docker/setup-buildx-action@v2 - name: Login to DockerHub - if: env.GOOS == 'linux' && env.GOARCH == 'amd64' uses: docker/login-action@v2 with: username: ${{ secrets.DOCKERHUB_USER }} password: ${{ secrets.DOCKERHUB_TOKEN }} - name: Prepare Dockerfile - if: github.event_name == 'release' && env.GOOS == 'linux' && env.GOARCH == 'amd64' + if: github.event_name == 'release' run: | cp ./Dockerfile ./dist cp -rp ./resources ./dist - name: Build and push - if: github.event_name == 'release' && env.GOOS == 'linux' && env.GOARCH == 'amd64' + if: github.event_name == 'release' uses: docker/build-push-action@v3 with: context: ./dist file: ./dist/Dockerfile - platforms: linux/amd64 + platforms: ${{ env.PLATFORMS }} push: ${{ github.event_name != 'pull_request' }} tags: | uozi/nginx-ui:latest uozi/nginx-ui:${{ steps.get_version.outputs.VERSION }} - name: Prepare Demo Dockerfile - if: env.GOOS == 'linux' && env.GOARCH == 'amd64' run: | cp ./demo.Dockerfile ./dist cp -rp ./resources ./dist - name: Build and push demo - if: env.GOOS == 'linux' && env.GOARCH == 'amd64' uses: docker/build-push-action@v3 with: context: ./dist file: ./dist/demo.Dockerfile - platforms: linux/amd64 + platforms: ${{ env.PLATFORMS }} push: ${{ github.event_name != 'pull_request' }} tags: | uozi/nginx-ui-demo:latest diff --git a/Dockerfile b/Dockerfile index 10a15654..1be8ed9f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,7 @@ -# CGO_ENABLED=1 GOOS=linux CC=x86_64-unknown-linux-gnu-gcc CXX=x86_64-unknown-linux-gnu-g++ GOARCH=amd64 go build -ldflags "-X 'github.com/0xJacky/Nginx-UI/server/settings.buildTime=$(date +%s)'" -o nginx-ui -v main.go -FROM --platform=linux/amd64 uozi/nginx-ui-base:latest +FROM --platform=$TARGETPLATFORM uozi/nginx-ui-base:latest +ARG TARGETOS +ARG TARGETARCH +ARG TARGETVARIANT WORKDIR /app EXPOSE 80 443 @@ -7,7 +9,7 @@ COPY resources/docker/start.sh /app/start.sh COPY resources/docker/nginx.conf /usr/etc/nginx/nginx.conf COPY resources/docker/nginx-ui.conf /usr/etc/nginx/conf.d/nginx-ui.conf COPY resources/docker/nginx-ui.conf /etc/nginx/conf.d/nginx-ui.conf -COPY nginx-ui /app/nginx-ui +COPY nginx-ui-$TARGETOS-$TARGETARCH$TARGETVARIANT/nginx-ui /app/nginx-ui RUN cd /app && chmod a+x /app/start.sh \ && rm -f /etc/nginx/conf.d/default.conf \ diff --git a/demo.Dockerfile b/demo.Dockerfile index 2448990a..d839a12e 100644 --- a/demo.Dockerfile +++ b/demo.Dockerfile @@ -1,5 +1,8 @@ # CGO_ENABLED=1 GOOS=linux CC=x86_64-unknown-linux-gnu-gcc CXX=x86_64-unknown-linux-gnu-g++ GOARCH=amd64 go build -ldflags "-X 'github.com/0xJacky/Nginx-UI/server/settings.buildTime=$(date +%s)'" -o nginx-ui -v main.go -FROM --platform=linux/amd64 uozi/nginx-ui-base:latest +FROM --platform=$TARGETPLATFORM uozi/nginx-ui-base:latest +ARG TARGETOS +ARG TARGETARCH +ARG TARGETVARIANT WORKDIR /app EXPOSE 80 @@ -9,7 +12,7 @@ COPY resources/demo/demo.db /etc/nginx-ui/database.db COPY resources/docker/nginx.conf /etc/nginx/nginx.conf COPY resources/docker/nginx-ui.conf /etc/nginx/conf.d/nginx-ui.conf COPY resources/docker/start.sh /app/start.sh -COPY nginx-ui /app/nginx-ui +COPY nginx-ui-$TARGETOS-$TARGETARCH$TARGETVARIANT/nginx-ui /app/nginx-ui RUN cd /app && chmod a+x start.sh && rm -f /etc/nginx/conf.d/default.conf