From 171465b550f062aab5cb3dec8476f1452e732d96 Mon Sep 17 00:00:00 2001 From: huoxue1 <3343780376@qq.com> Date: Thu, 5 Oct 2023 12:02:46 +0800 Subject: [PATCH] add ci --- .gitea/workflows/ci.yml | 89 +++++++++++++++++++++++++++++++++++++++++ .goreleaser.yml | 60 +++++++++++++++++++++++++++ Dockerfile | 16 ++++++++ cmd/cmd.go | 1 + cmd/init.go | 6 +++ 5 files changed, 172 insertions(+) create mode 100644 .gitea/workflows/ci.yml create mode 100644 .goreleaser.yml create mode 100644 Dockerfile diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml new file mode 100644 index 0000000..6ebf05c --- /dev/null +++ b/.gitea/workflows/ci.yml @@ -0,0 +1,89 @@ +name: CI + +on: [push, pull_request] + +env: + + BINARY_Name: "min_report" + COMMIT_ID: "${{ github.sha }}" + PR_PROMPT: "::warning:: Build artifact will not be uploaded due to the workflow is trigged by pull request." + LD_FLAGS: "-w -s" + HTTP_PROXY: "http://172.17.0.1:7890" + HTTPS_PROXY: "http://172.17.0.1:7890" + GOPROXY: "https://goproxy.cn,direct" + +jobs: + build: + name: Build binary CI + runs-on: ubuntu-latest + container: + image: ghcr.io/catthehacker/ubuntu:act-latest + # 持久化工具目录 + steps: + - name: Checkout + uses: actions/checkout@v3 + with: + fetch-depth: 0 + - name: setup go + uses: https://github.com/actions/setup-go@v4 + with: + go-version: '1.20' + - name: Fetch all tags + run: git fetch --force --tags + - name: Check snapshot + if: "!startsWith(github.ref, 'refs/tags/')" + id: snapshot + run: echo '::set-output name=ARG::--snapshot' + - name: push release + if: "startsWith(github.ref, 'refs/tags/')" + id: push release + uses: https://huoxue.link:12300/huoxue1/goreleaser-action@master + with: + version: latest + args: release --timeout 5h30m --rm-dist + env: + GITEA_TOKEN: ${{ secrets.TOKEN }} + - name: Run GoReleaser + if: "!startsWith(github.ref, 'refs/tags/')" + uses: https://huoxue.link:12300/huoxue1/goreleaser-action@master + with: + version: latest + args: build --snapshot --timeout 5h30m --clean + env: + GITEA_TOKEN: ${{ secrets.TOKEN }} + - name: Set up QEMU + id: qemu + uses: docker/setup-qemu-action@v2 + - name: Set up Docker Buildx + id: buildx + uses: docker/setup-buildx-action@v2 + - name: Docker meta + id: meta + uses: docker/metadata-action@v4 + with: + images: huoxue.link:12300/huoxue1/min_report + tags: | + type=raw,value=latest + type=ref,event=tag + - name: Docker Login + if: github.event_name != 'pull_request' + uses: docker/login-action@v2 + with: + registry: huoxue.link:12300 + username: ${{ secrets.USERNAME }} + password: ${{ secrets.PASSWORD }} + - name: Build and push + id: docker_build + uses: docker/build-push-action@v3 + with: + context: . + push: ${{ github.event_name != 'pull_request' }} + platforms: linux/amd64,linux/arm64 + tags: ${{ steps.meta.outputs.tags }} + cache-from: type=gha + cache-to: type=gha,mode=max + shm-size: 2g + ulimit: core=0:0 + + + diff --git a/.goreleaser.yml b/.goreleaser.yml new file mode 100644 index 0000000..41c1ef8 --- /dev/null +++ b/.goreleaser.yml @@ -0,0 +1,60 @@ +env: + - GO111MODULE=on +before: + hooks: + - go mod tidy +builds: + + - id: docker + env: + - CGO_ENABLED=0 + - GO111MODULE=on + goos: + - linux + goarch: + - amd64 + - arm64 + mod_timestamp: "{{ .CommitTimestamp }}" + flags: + - -trimpath + ldflags: + - -s -w -X github.com/huoxue1/min-report/cmd.Version=v{{.Version}} -X github.com/huoxue1/min-report/cmd.Commit={{.ShortCommit}} -X github.com/huoxue1/min-report/cmd.Date={{.CommitDate}} + + +checksum: + name_template: "{{ .ProjectName }}_checksums.txt" +changelog: + sort: asc + filters: + exclude: + - "^docs:" + - "^test:" + - fix typo + - Merge pull request + - Merge branch + - Merge remote-tracking + - go mod tidy + +archives: + - id: docker + builds: + - docker + name_template: "{{ .ProjectName }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}" + format_overrides: + - goos: windows + format: zip + + +release: + gitea: + owner: "huoxue1" + name: "min_report" + name_template: "{{.ProjectName}}-v{{.Version}} {{.Env.USER}}" + disable: false + + +gitea_urls: + api: "https://huoxue.link:12300/api/v1" + download: "https://huoxue.link:12300" + +force_token: gitea diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..70e2e59 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,16 @@ +FROM ubuntu:jammy + +ARG TARGETARCH +WORKDIR / + +COPY ./dist/docker_linux_$TARGETARCH*/min_report /min_report + +RUN chmod -R 777 /min_report + +EXPOSE 12700 + +VOLUME /logs + + + +ENTRYPOINT [ "/min" ] \ No newline at end of file diff --git a/cmd/cmd.go b/cmd/cmd.go index 299c7a7..a762256 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -17,6 +17,7 @@ var ( } }, PreRun: func(cmd *cobra.Command, args []string) { + fmt.Println(fmt.Sprintf("build commit: %s, build version: %s, build date: %s", Commit, Version, Date)) config.InitConfig(configFile) module.InitDb(viper.GetString("db.driver"), viper.GetString("db.url")) }, diff --git a/cmd/init.go b/cmd/init.go index 1d619dd..a569ff6 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -1 +1,7 @@ package cmd + +var ( + Version = "" + Commit = "" + Date = "" +)