Browse Source

ci: support multi-platform docker image build

pull/81/head
jxlwqq 4 years ago
parent
commit
d266c118bf
  1. 46
      .github/workflows/release.yml
  2. 1
      helper/.goreleaser.yml
  3. 9
      helper/Dockerfile-release
  4. 8
      helper/build-repo.sh

46
.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 }}

1
helper/.goreleaser.yml

@ -7,6 +7,7 @@ builds:
- windows
- darwin
goarch:
- arm64
- amd64
id: 'dtm'
dir: .

9
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

8
helper/build-repo.sh

@ -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
Loading…
Cancel
Save