From fc1eaf9a9de39fe1eec57a1089e833c211ced18b Mon Sep 17 00:00:00 2001 From: Meta Date: Thu, 28 Apr 2022 14:11:35 +0800 Subject: [PATCH] chore: add docker workflow --- .github/workflows/docker.yaml | 59 +++++++++++++++++++++++++++++++++++ Dockerfile | 47 +++++++++++++++++++++------- check_amd64.sh | 28 +++++++++++++++++ 3 files changed, 122 insertions(+), 12 deletions(-) create mode 100644 .github/workflows/docker.yaml create mode 100644 check_amd64.sh diff --git a/.github/workflows/docker.yaml b/.github/workflows/docker.yaml new file mode 100644 index 000000000..936337856 --- /dev/null +++ b/.github/workflows/docker.yaml @@ -0,0 +1,59 @@ +name: Docker + +on: + push: + tags: [ 'v*.*.*' ] + workflow_dispatch: + +env: + REGISTRY: docker.io + IMAGE_NAME: '{{ env.DOCKERHUB_ACCOUNT }}/{{ env.DOCKERHUB_REPO }}' + +jobs: + build: + runs-on: ubuntu-latest + permissions: + contents: read + + steps: + - name: Checkout repository + uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - name: Set up QEMU + uses: docker/setup-qemu-action@v1 + + - name: Setup Docker buildx + uses: docker/setup-buildx-action@v1 + + # Extract metadata (tags, labels) for Docker + # https://github.com/docker/metadata-action + - name: Extract Docker metadata + id: meta + uses: docker/metadata-action@v3 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + + - name: Log into registry ${{ env.REGISTRY }} + if: github.event_name != 'pull_request' + uses: docker/login-action@v1 + with: + registry: ${{ env.REGISTRY }} + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + + # Build and push Docker image with Buildx (don't push on PR) + # https://github.com/docker/build-push-action + - name: Build and push Docker image + id: build-and-push + uses: docker/build-push-action@v2 + with: + context: . + push: ${{ github.event_name != 'pull_request' }} + platforms: | + linux/386 + linux/amd64 + linux/arm64/v8 + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 6e67d5d4c..498fbd443 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,18 +1,41 @@ FROM golang:alpine as builder -RUN apk add --no-cache make git && \ - wget -O /Country.mmdb https://github.com/Dreamacro/maxmind-geoip/releases/latest/download/Country.mmdb -WORKDIR /clash-src -COPY --from=tonistiigi/xx:golang / / -COPY . /clash-src -RUN go mod download && \ - make docker && \ - mv ./bin/clash-docker /clash +ARG TARGETOS +ARG TARGETARCH +RUN apk add --no-cache make git && \ + mkdir /clash-config && \ + wget -O /clash-config/Country.mmdb https://raw.githubusercontent.com/Loyalsoldier/geoip/release/Country.mmdb && \ + wget -O /clash-config/geosite.dat https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geosite.dat && \ + wget -O /clash-config/geoip.dat https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geoip.dat + +WORKDIR /clash-src +COPY . /clash-src +RUN go mod download +RUN /bin/ash -c 'set -ex && \ + if [ "$TARGETARCH" == "amd64" ]; then \ + GOOS=$TARGETOS GOARCH=$TARGETARCH GOAMD64=v1 make docker && \ + mv ./bin/Clash.Meta-docker ./bin/clash-amd64v1 && \ + GOOS=$TARGETOS GOARCH=$TARGETARCH GOAMD64=v2 make docker && \ + mv ./bin/Clash.Meta-docker ./bin/clash-amd64v2 && \ + GOOS=$TARGETOS GOARCH=$TARGETARCH GOAMD64=v3 make docker && \ + mv ./bin/Clash.Meta-docker ./bin/clash-amd64v3 && \ + ln -s clash-amd64v3 ./bin/clash-amd64v4 && \ + mv check_amd64.sh ./bin/ && \ + printf "#!/bin/sh\\nsh ./check_amd64.sh\\nexec ./clash-amd64v\$? \$@" > ./bin/clash && \ + chmod +x ./bin/check_amd64.sh ./bin/clash; \ + else \ + GOOS=$TARGETOS GOARCH=$TARGETARCH make docker && \ + mv ./bin/Clash.Meta-docker ./bin/clash; \ + fi' FROM alpine:latest -LABEL org.opencontainers.image.source="https://github.com/Dreamacro/clash" +LABEL org.opencontainers.image.source="https://github.com/MetaCubeX/Clash.Meta" RUN apk add --no-cache ca-certificates tzdata -COPY --from=builder /Country.mmdb /root/.config/clash/ -COPY --from=builder /clash / -ENTRYPOINT ["/clash"] + +VOLUME ["/root/.config/clash/"] +EXPOSE 7890/tcp + +COPY --from=builder /clash-config/ /root/.config/clash/ +COPY --from=builder /clash-src/bin/ / +ENTRYPOINT [ "/clash" ] \ No newline at end of file diff --git a/check_amd64.sh b/check_amd64.sh new file mode 100644 index 000000000..068296d33 --- /dev/null +++ b/check_amd64.sh @@ -0,0 +1,28 @@ +#!/bin/sh +flags=$(grep '^flags\b'