From 5d695bfad3b35bb1ff6067b8e64cec5ee33520ce Mon Sep 17 00:00:00 2001 From: Cairry <115769353+Cairry@users.noreply.github.com> Date: Mon, 17 Feb 2025 09:45:21 +0800 Subject: [PATCH] Optimized workflows ci and dockerfile --- .github/workflows/release-ci.yml | 53 +++++++++++++++++++++++ .github/workflows/release-drafter.yml | 34 --------------- .github/workflows/{ci.yml => test-ci.yml} | 27 ++---------- Dockerfile | 11 +++-- 4 files changed, 62 insertions(+), 63 deletions(-) create mode 100644 .github/workflows/release-ci.yml delete mode 100644 .github/workflows/release-drafter.yml rename .github/workflows/{ci.yml => test-ci.yml} (53%) diff --git a/.github/workflows/release-ci.yml b/.github/workflows/release-ci.yml new file mode 100644 index 0000000..03109ce --- /dev/null +++ b/.github/workflows/release-ci.yml @@ -0,0 +1,53 @@ +name: CI + +on: + push: + tags: + - v* + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 + + - name: Inject slug/short variables + uses: rlespinasse/github-slug-action@v4 + + - name: Set up Docker Buildx + id: buildx + uses: docker/setup-buildx-action@v3 + with: + driver: docker-container + + - name: Available platforms + run: echo ${{ steps.buildx.outputs.platforms }} + + - name: Login to Docker Hub + uses: docker/login-action@v3 + with: + username: ${{ secrets.DOCKERHUB_USER }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + + - name: Set env variables + id: set_env + run: | + echo "TAG_NAME=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV + + - name: Build and push + uses: docker/build-push-action@v6 + with: + context: . + file: ./Dockerfile + platforms: linux/amd64,linux/arm64 + push: true + build-args: | + VERSION=${{ env.TAG_NAME }} + tags: | + cairry/watchalert:latest + cairry/watchalert:${{ env.TAG_NAME }} \ No newline at end of file diff --git a/.github/workflows/release-drafter.yml b/.github/workflows/release-drafter.yml deleted file mode 100644 index b927876..0000000 --- a/.github/workflows/release-drafter.yml +++ /dev/null @@ -1,34 +0,0 @@ -name: Release Drafter - -on: - push: - # branches to consider in the event; optional, defaults to all - branches: - - master - # pull_request event is required only for autolabeler - pull_request: - # Only following types are handled by the action, but one can default to all as well - types: [opened, reopened, synchronize] - # pull_request_target event is required for autolabeler to support PRs from forks - # pull_request_target: - # types: [opened, reopened, synchronize] - -permissions: - contents: read - -jobs: - update_release_draft: - permissions: - contents: write # for release-drafter/release-drafter to create a github release - pull-requests: write # for release-drafter/release-drafter to add label to PR - runs-on: ubuntu-latest - steps: - # (Optional) GitHub Enterprise requires GHE_HOST variable set - #- name: Set GHE_HOST - # run: | - # echo "GHE_HOST=${GITHUB_SERVER_URL##https:\/\/}" >> $GITHUB_ENV - - # Drafts your next Release notes as Pull Requests are merged into "master" - - uses: release-drafter/release-drafter@v6 - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file diff --git a/.github/workflows/ci.yml b/.github/workflows/test-ci.yml similarity index 53% rename from .github/workflows/ci.yml rename to .github/workflows/test-ci.yml index dc11fc3..e3743d6 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/test-ci.yml @@ -3,18 +3,12 @@ name: CI on: push: branches: - - master + - '*/*' jobs: build: runs-on: ubuntu-latest - strategy: - matrix: - arch: - - amd64 - - arm64 - steps: - name: Checkout code uses: actions/checkout@v3 @@ -40,32 +34,19 @@ jobs: username: ${{ secrets.DOCKERHUB_USER }} password: ${{ secrets.DOCKERHUB_TOKEN }} - - name: Login to Aliyun Hub - uses: docker/login-action@v3 - with: - registry: registry.cn-hangzhou.aliyuncs.com - username: ${{ secrets.OPSRE_ALIHUB_USERNAME }} - password: ${{ secrets.OPSRE_ALIHUB_TOKEN }} - - name: Set env variables id: set_env run: | echo "BRANCH_NAME=${GITHUB_REF#refs/heads/}" >> $GITHUB_ENV - echo "SHORT_SHA=${GITHUB_SHA:0:4}" >> $GITHUB_ENV - echo "DATE=$(TZ=Asia/Shanghai date +%Y-%m-%d.%H-%M-%S)" >> $GITHUB_ENV - name: Build and push uses: docker/build-push-action@v6 with: context: . file: ./Dockerfile - platforms: linux/${{ matrix.arch }} - push: ${{ github.event_name != 'pull_request' }} + platforms: linux/amd64,linux/arm64 + push: true build-args: | VERSION=${{ env.DATE }} - TARGETARCH=${{ matrix.arch }} tags: | - cairry/watchalert:latest - cairry/watchalert:${{ env.BRANCH_NAME }}.${{ env.DATE }}.${{ env.SHORT_SHA }} - registry.cn-hangzhou.aliyuncs.com/opsre/watchalert:latest - registry.cn-hangzhou.aliyuncs.com/opsre/watchalert:${{ env.BRANCH_NAME }}.${{ env.DATE }}.${{ env.SHORT_SHA }} \ No newline at end of file + cairry/watchalert:${{ env.BRANCH_NAME }} \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 2a42c80..b63e360 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,6 @@ -FROM --platform=$BUILDPLATFORM golang:1.21.9-alpine3.19 AS build +FROM golang:1.21.9-alpine3.19 AS build ARG VERSION -ARG TARGETARCH ENV GOPROXY=https://goproxy.cn,direct @@ -13,13 +12,13 @@ RUN sed -i "s/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g" /etc/apk/repositories && apk upgrade && apk add --no-cache --virtual .build-deps \ ca-certificates upx -RUN CGO_ENABLED=0 GOOS=linux GOARCH=$TARGETARCH go build --ldflags="-X main.Version=${VERSION}" -o watchAlert . \ - && chmod +x watchAlert +RUN CGO_ENABLED=0 go build --ldflags="-X main.Version=${VERSION}" -o w8t . \ + && chmod +x w8t FROM alpine:3.19 -COPY --from=build /root/watchAlert /app/watchAlert +COPY --from=build /root/w8t /app/w8t WORKDIR /app -ENTRYPOINT ["/app/watchAlert"] +ENTRYPOINT ["/app/w8t"]