From d266c118bf1da34dfa0463380cd32aaa1ba050d1 Mon Sep 17 00:00:00 2001 From: jxlwqq Date: Mon, 13 Dec 2021 15:26:34 +0800 Subject: [PATCH] ci: support multi-platform docker image build --- .github/workflows/release.yml | 46 ++++++++++++++++++++++++++--------- helper/.goreleaser.yml | 1 + helper/Dockerfile-release | 9 ++++--- helper/build-repo.sh | 8 ------ 4 files changed, 41 insertions(+), 23 deletions(-) delete mode 100755 helper/build-repo.sh diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 82b5c54..d65f219 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -2,7 +2,7 @@ name: Release on: create: tags: - - v* + - 'v*.*.*' jobs: release: @@ -10,7 +10,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Check out code - uses: actions/checkout@v1 + uses: actions/checkout@v2 - name: Validates GO releaser config uses: docker://goreleaser/goreleaser:latest @@ -24,14 +24,36 @@ jobs: env: GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} - - name: Get the tag name - run: echo "TAG=${GITHUB_REF/refs\/tags\//}" >> $GITHUB_ENV + - name: Docker meta + id: meta + uses: docker/metadata-action@v3 + with: + images: | + yedf/dtm + tags: | + type=semver,pattern={{version}} + type=semver,pattern={{major}}.{{minor}} + type=semver,pattern={{major}} + type=sha - - name: Publish docker - env: - host: 'ubuntu@en.dtm.pub' - dest: '/home/ubuntu' - run: | - echo "${{secrets.DEPLOY_KEY}}" > deploy_key - chmod 600 ./deploy_key - ssh -i deploy_key -o StrictHostKeyChecking=no ${{env.host}} 'cd ${{env.dest}}/dtm && git pull && sudo sh helper/build-repo.sh ${{env.TAG}}' + - name: Set up QEMU + uses: docker/setup-qemu-action@v1 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 + + - name: Login to DockerHub + uses: docker/login-action@v1 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + + - name: Build and push + uses: docker/build-push-action@v2 + with: + context: . + file: ./helper/Dockerfile-release + push: true + platforms: linux/amd64,linux/arm64 + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} diff --git a/helper/.goreleaser.yml b/helper/.goreleaser.yml index 39ca3ad..1ce6b3f 100644 --- a/helper/.goreleaser.yml +++ b/helper/.goreleaser.yml @@ -7,6 +7,7 @@ builds: - windows - darwin goarch: + - arm64 - amd64 id: 'dtm' dir: . diff --git a/helper/Dockerfile-release b/helper/Dockerfile-release index db911a7..b922185 100644 --- a/helper/Dockerfile-release +++ b/helper/Dockerfile-release @@ -1,11 +1,14 @@ -FROM golang:1.16.6-alpine3.14 as builder +# syntax=docker/dockerfile:1 +FROM --platform=$TARGETPLATFORM golang:1.16.6-alpine3.14 as builder +ARG TARGETARCH +ARG TARGETOS WORKDIR /app/dtm # RUN go env -w GOPROXY=https://mirrors.aliyun.com/goproxy/,direct EXPOSE 8080 COPY . . -RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" app/main.go && apk add upx && upx main +RUN CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH go build -ldflags="-s -w" app/main.go -FROM alpine:3.14 as runner +FROM --platform=$TARGETPLATFORM alpine:3.14 COPY --from=builder /app/dtm/main /app/dtm/ ENV IS_DOCKER=1 WORKDIR /app/dtm diff --git a/helper/build-repo.sh b/helper/build-repo.sh deleted file mode 100755 index 9182cb5..0000000 --- a/helper/build-repo.sh +++ /dev/null @@ -1,8 +0,0 @@ -set -x -if [ x$1 == x ]; then - echo please specify version - exit 1 -fi - -docker build -f helper/Dockerfile-release -t yedf/dtm:latest . && docker push yedf/dtm:latest -docker tag yedf/dtm:latest yedf/dtm:$1 && docker push yedf/dtm:$1 \ No newline at end of file