From 23751440c6a0417c2800d5f0603bf4be999e4b91 Mon Sep 17 00:00:00 2001 From: UUBulb <35923940+uubulb@users.noreply.github.com> Date: Mon, 2 Sep 2024 22:13:13 +0800 Subject: [PATCH] [ci][docker]: use matrix to reduce build time & change base image to busybox (#417) * ci: use matrix to reduce build time * docker: change base image to busybox * reuse workflows * only download ipinfo db on tag pushes * goreleaser --- .github/workflows/release.yml | 191 +++++++++++++++++++------------ .github/workflows/test-on-pr.yml | 43 ------- .github/workflows/test.yml | 40 +++---- .goreleaser.yml | 71 ++++++++++++ Dockerfile | 12 +- cmd/dashboard/main.go | 1 + 6 files changed, 211 insertions(+), 147 deletions(-) delete mode 100644 .github/workflows/test-on-pr.yml create mode 100644 .goreleaser.yml diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 09ca592..0ad35b3 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -4,54 +4,138 @@ on: push: tags: - "v*" - workflow_dispatch: jobs: - release: + build: + strategy: + fail-fast: true + matrix: + goos: [linux, windows] + goarch: [amd64] + include: + - goos: linux + goarch: s390x + - goos: linux + goarch: arm64 + + name: Build artifacts runs-on: ubuntu-latest + container: + image: goreleaser/goreleaser-cross:v1.21 + env: + GOOS: ${{ matrix.goos }} + GOARCH: ${{ matrix.goarch }} + GOARM: ${{ matrix.goarm }} + outputs: + tag_name: ${{ steps.extract_branch.outputs.tag }} steps: + - run: git config --global --add safe.directory /__w/nezha/nezha - uses: actions/checkout@v4 - name: Fetch IPInfo GeoIP Database - env: + if: github.event_name == 'push' && contains(github.ref, 'refs/tags/') + env: IPINFO_TOKEN: ${{ secrets.IPINFO_TOKEN }} run: | rm pkg/geoip/geoip.db - wget -O pkg/geoip/geoip.db https://ipinfo.io/data/free/country.mmdb?token=${IPINFO_TOKEN} + wget -qO pkg/geoip/geoip.db https://ipinfo.io/data/free/country.mmdb?token=${IPINFO_TOKEN} + + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version: "1.21.x" + + - name: Build + uses: goreleaser/goreleaser-action@v6 + with: + distribution: goreleaser + version: '~> v2' + args: build --single-target --clean --skip=validate + + - name: Upload artifacts + uses: actions/upload-artifact@v4 + with: + name: dashboard-${{ env.GOOS }}-${{ env.GOARCH }} + path: | + ./dist/*/* + + release: + runs-on: ubuntu-latest + if: github.event_name == 'push' && contains(github.ref, 'refs/tags/') + needs: build + name: Release + steps: + - name: Download artifacts + uses: actions/download-artifact@v4 + with: + path: ./assets + + - name: Archive and compress binaries + run: | + for file in assets/*/*/*; do + if [ -f "$file" ]; then + chmod +x "$file" + zip -jr "$file.zip" "$file" + fi + done + + - name: Release + uses: ncipollo/release-action@v1 + with: + artifacts: "assets/*/*/*.zip" + generateReleaseNotes: true + + - name: Purge jsdelivr cache + run: | + curl -s https://purge.jsdelivr.net/gh/${{ github.repository_owner }}/nezha@master/script/install.sh + curl -s https://purge.jsdelivr.net/gh/${{ github.repository_owner }}/nezha@master/script/nezha-agent.service + curl -s https://purge.jsdelivr.net/gh/${{ github.repository_owner }}/nezha@master/script/docker-compose.yaml + curl -s https://purge.jsdelivr.net/gh/${{ github.repository_owner }}/nezha@master/script/config.yaml + LOWER_USERNAME=$(echo "${{ github.repository_owner }}" | tr '[:upper:]' '[:lower:]') + curl -s https://purge.jsdelivr.net/gh/$LOWER_USERNAME/nezha@master/script/install.sh + curl -s https://purge.jsdelivr.net/gh/$LOWER_USERNAME/nezha@master/script/nezha-agent.service + curl -s https://purge.jsdelivr.net/gh/$LOWER_USERNAME/nezha@master/script/docker-compose.yaml + curl -s https://purge.jsdelivr.net/gh/$LOWER_USERNAME/nezha@master/script/config.yaml + + - name: Trigger sync + env: + GH_REPO: ${{ github.repository }} + GH_TOKEN: ${{ github.token }} + GH_DEBUG: api + run: | + gh workflow run sync-release.yml + + release-docker: + runs-on: ubuntu-latest + if: github.event_name == 'push' && contains(github.ref, 'refs/tags/') + needs: build + name: Release Docker images + steps: + - uses: actions/checkout@v4 + + - name: Download artifacts + uses: actions/download-artifact@v4 + with: + path: ./assets + + - name: Fix permissions + run: | + chmod -R +x ./assets/* + mkdir dist + mv ./assets/*/*/* ./dist - name: Extract branch name run: | export TAG_NAME=$(echo ${GITHUB_REF#refs/tags/}) echo "tag=$TAG_NAME" >> $GITHUB_OUTPUT id: extract_branch - - - name: xgo build - uses: crazy-max/ghaction-xgo@v2 - with: - xgo_version: latest - go_version: 1.21.x - dest: dist - pkg: cmd/dashboard - prefix: dashboard - targets: linux/amd64,linux/arm64,linux/arm-7,linux/s390x,linux/riscv64,windows/amd64,windows/386,windows/arm64 # linux/386, - v: true - x: false - race: false - tags: timetzdata - ldflags: -s -w --extldflags '-static -fpic' -X github.com/naiba/nezha/service/singleton.Version=${{ steps.extract_branch.outputs.tag }} - buildmode: default - - - name: fix dist - run: | - ls -al dist/ - mv dist/dashboard-linux-arm-7 dist/dashboard-linux-arm - - - name: Log in to the GHCR + + - name: Log into GHCR uses: docker/login-action@master with: registry: ghcr.io username: ${{ github.repository_owner }} - password: ${{ secrets.GITHUB_TOKEN }} + password: ${{ github.token }} - name: Log in to the AliyunCS uses: docker/login-action@master @@ -68,13 +152,13 @@ jobs: - name: Set up image name run: | - GHRC_IMAGE_NAME=$(echo "ghcr.io/${{ github.repository_owner }}/nezha-dashboard" | tr '[:upper:]' '[:lower:]') + GHCR_IMAGE_NAME=$(echo "ghcr.io/${{ github.repository_owner }}/nezha-dashboard" | tr '[:upper:]' '[:lower:]') if [ ${{ github.repository_owner }} = "naiba" ] then ALI_IMAGE_NAME=$(echo "registry.cn-shanghai.aliyuncs.com/naibahq/nezha-dashboard") else ALI_IMAGE_NAME=$(echo "registry.cn-shanghai.aliyuncs.com/${{ github.repository_owner }}/nezha-dashboard" | tr '[:upper:]' '[:lower:]') fi - echo "::set-output name=GHRC_IMAGE_NAME::$GHRC_IMAGE_NAME" - echo "::set-output name=ALI_IMAGE_NAME::$ALI_IMAGE_NAME" + echo "GHCR_IMAGE_NAME=$GHCR_IMAGE_NAME" >> $GITHUB_OUTPUT + echo "ALI_IMAGE_NAME=$ALI_IMAGE_NAME" >> $GITHUB_OUTPUT id: image-name - name: Build dasbboard image And Push @@ -82,49 +166,10 @@ jobs: with: context: . file: ./Dockerfile - platforms: linux/amd64,linux/arm64,linux/arm,linux/s390x,linux/riscv64 # linux/386, + platforms: linux/amd64,linux/arm64,linux/s390x push: true tags: | - ${{ steps.image-name.outputs.GHRC_IMAGE_NAME }}:latest - ${{ steps.image-name.outputs.GHRC_IMAGE_NAME }}:${{ steps.extract_branch.outputs.tag }} + ${{ steps.image-name.outputs.GHCR_IMAGE_NAME }}:latest + ${{ steps.image-name.outputs.GHCR_IMAGE_NAME }}:${{ steps.extract_branch.outputs.tag }} ${{ steps.image-name.outputs.ALI_IMAGE_NAME }}:latest ${{ steps.image-name.outputs.ALI_IMAGE_NAME }}:${{ steps.extract_branch.outputs.tag }} - - - name: Compress dist files - run: | - for file in dist/*; do - if [ -f "$file" ]; then - zip -r "$file.zip" "$file" - fi - done - - - name: Release - uses: ncipollo/release-action@v1 - with: - artifacts: "dist/*.zip" - generateReleaseNotes: true - - - name: Purge jsdelivr cache - run: | - curl -s https://purge.jsdelivr.net/gh/${{ github.repository_owner }}/nezha@master/script/install.sh - curl -s https://purge.jsdelivr.net/gh/${{ github.repository_owner }}/nezha@master/script/nezha-agent.service - curl -s https://purge.jsdelivr.net/gh/${{ github.repository_owner }}/nezha@master/script/docker-compose.yaml - curl -s https://purge.jsdelivr.net/gh/${{ github.repository_owner }}/nezha@master/script/config.yaml - LOWER_USERNAME=$(echo "${{ github.repository_owner }}" | tr '[:upper:]' '[:lower:]') - curl -s https://purge.jsdelivr.net/gh/$LOWER_USERNAME/nezha@master/script/install.sh - curl -s https://purge.jsdelivr.net/gh/$LOWER_USERNAME/nezha@master/script/nezha-agent.service - curl -s https://purge.jsdelivr.net/gh/$LOWER_USERNAME/nezha@master/script/docker-compose.yaml - curl -s https://purge.jsdelivr.net/gh/$LOWER_USERNAME/nezha@master/script/config.yaml - - - name: Trigger sync - if: ${{ env.SYNCED == 0 }} - uses: actions/github-script@v6 - with: - github-token: ${{ secrets.GITHUB_TOKEN }} - script: | - await github.rest.actions.createWorkflowDispatch({ - owner: 'naiba', - repo: 'nezha', - workflow_id: 'sync-release.yml', - ref: 'master' - }) diff --git a/.github/workflows/test-on-pr.yml b/.github/workflows/test-on-pr.yml deleted file mode 100644 index bd811d9..0000000 --- a/.github/workflows/test-on-pr.yml +++ /dev/null @@ -1,43 +0,0 @@ -name: Run Tests on PR - -on: - pull_request: - branches: - - master -jobs: - tests: - runs-on: ubuntu-latest - env: - GO111MODULE: on - steps: - - uses: actions/checkout@v4 - - uses: actions/setup-go@v4 - with: - go-version: "^1.21.3" - - name: Unit test - run: | - go test -v ./... - - name: Run Gosec Security Scanner - uses: securego/gosec@master - with: - args: --exclude=G104,G402 ./... - - name: xgo build - uses: crazy-max/ghaction-xgo@v2 - with: - xgo_version: latest - go_version: 1.21.x - dest: dist - pkg: cmd/dashboard - prefix: dashboard - targets: linux/amd64,linux/arm64,linux/arm-7,linux/s390x,linux/riscv64,windows/amd64,windows/386,windows/arm64 # linux/386, - v: true - x: false - race: false - tags: timetzdata - ldflags: -s -w --extldflags '-static -fpic' -X github.com/naiba/nezha/service/singleton.Version=test - buildmode: default - - name: fix dist - run: | - ls -al dist/ - mv dist/dashboard-linux-arm-7 dist/dashboard-linux-arm - diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index bd06ea5..bbf5b3c 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -10,40 +10,36 @@ on: - "go.sum" - "resource/**" - ".github/workflows/test.yml" + pull_request: + branches: + - master jobs: tests: - runs-on: ubuntu-latest + strategy: + fail-fast: true + matrix: + os: [ubuntu, windows, macos] + + runs-on: ${{ matrix.os }}-latest env: GO111MODULE: on steps: - uses: actions/checkout@v4 - - uses: actions/setup-go@v4 + + - uses: actions/setup-go@v5 with: - go-version: "^1.21.3" + go-version: "1.21.x" + - name: Unit test run: | go test -v ./... + + - name: Build test + run: go build -v ./cmd/dashboard + - name: Run Gosec Security Scanner + if: runner.os == 'Linux' uses: securego/gosec@master with: args: --exclude=G104,G402 ./... - - name: xgo build - uses: crazy-max/ghaction-xgo@v2 - with: - xgo_version: latest - go_version: 1.21.x - dest: dist - pkg: cmd/dashboard - prefix: dashboard - targets: linux/amd64,linux/arm64,linux/arm-7,linux/s390x,linux/riscv64,windows/amd64,windows/386,windows/arm64 # linux/386, - v: true - x: false - race: false - tags: timetzdata - ldflags: -s -w --extldflags '-static -fpic' -X github.com/naiba/nezha/service/singleton.Version=test - buildmode: default - - name: fix dist - run: | - ls -al dist/ - mv dist/dashboard-linux-arm-7 dist/dashboard-linux-arm diff --git a/.goreleaser.yml b/.goreleaser.yml new file mode 100644 index 0000000..2957834 --- /dev/null +++ b/.goreleaser.yml @@ -0,0 +1,71 @@ +version: 2 +before: + hooks: + - go mod tidy -v +builds: + - id: linux_arm64 + env: + - CGO_ENABLED=1 + - CC=aarch64-linux-gnu-gcc + ldflags: + - -s -w + - -X github.com/naiba/nezha/service/singleton.Version={{.Version}} + - -extldflags "-static -fpic" + flags: + - -trimpath + goos: + - linux + goarch: + - arm64 + main: ./cmd/dashboard + binary: dashboard-{{ .Os }}-{{ .Arch }} + - id: linux_amd64 + env: + - CGO_ENABLED=1 + - CC=x86_64-linux-gnu-gcc + ldflags: + - -s -w + - -X github.com/naiba/nezha/service/singleton.Version={{.Version}} + - -extldflags "-static -fpic" + flags: + - -trimpath + goos: + - linux + goarch: + - amd64 + main: ./cmd/dashboard + binary: dashboard-{{ .Os }}-{{ .Arch }} + - id: linux_s390x + env: + - CGO_ENABLED=1 + - CC=s390x-linux-gnu-gcc + ldflags: + - -s -w + - -X github.com/naiba/nezha/service/singleton.Version={{.Version}} + - -extldflags "-static -fpic" + flags: + - -trimpath + goos: + - linux + goarch: + - s390x + main: ./cmd/dashboard + binary: dashboard-{{ .Os }}-{{ .Arch }} + - id: windows_amd64 + env: + - CGO_ENABLED=1 + - CC=x86_64-w64-mingw32-gcc + ldflags: + - -s -w + - -X github.com/naiba/nezha/service/singleton.Version={{.Version}} + - -extldflags "-static -fpic" + flags: + - -trimpath + goos: + - windows + goarch: + - amd64 + main: ./cmd/dashboard + binary: dashboard-{{ .Os }}-{{ .Arch }} +snapshot: + version_template: "dashboard" diff --git a/Dockerfile b/Dockerfile index 9531a8b..e18b88c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,16 +1,8 @@ -# latest 镜像还没有 riscv64 https://hub.docker.com/_/alpine/tags -FROM alpine:edge +FROM busybox:stable-musl ARG TARGETOS ARG TARGETARCH -RUN apk update && \ - apk upgrade --no-cache && \ - apk add --no-cache tzdata && \ - cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \ - echo 'Asia/Shanghai' >/etc/timezone && \ - rm -rf /var/cache/apk/* - COPY ./script/entrypoint.sh /entrypoint.sh RUN chmod +x /entrypoint.sh @@ -19,4 +11,6 @@ COPY dist/dashboard-${TARGETOS}-${TARGETARCH} ./app VOLUME ["/dashboard/data"] EXPOSE 80 5555 +ARG TZ=Asia/Shanghai +ENV TZ=$TZ ENTRYPOINT ["/entrypoint.sh"] \ No newline at end of file diff --git a/cmd/dashboard/main.go b/cmd/dashboard/main.go index d05af21..b59b320 100644 --- a/cmd/dashboard/main.go +++ b/cmd/dashboard/main.go @@ -5,6 +5,7 @@ import ( "fmt" "log" "time" + _ "time/tzdata" "github.com/naiba/nezha/cmd/dashboard/controller" "github.com/naiba/nezha/cmd/dashboard/rpc"