From ea20d92bcd574e9cf00e6cd34f3cba429d03222c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E4=B8=80=E4=B9=8B?= Date: Wed, 4 Oct 2023 15:55:14 +0800 Subject: [PATCH] ci: github action --- .github/workflows/build.yml | 66 ++++++++++----- .github/workflows/release.yml | 152 ++++++++++++++++++++-------------- .github/workflows/test.yml | 28 +++++++ cmd/ccatctl/main.go | 7 +- cmd/cloudcat/main.go | 11 ++- configs/config.go | 2 + 6 files changed, 179 insertions(+), 87 deletions(-) create mode 100644 .github/workflows/test.yml diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 2840a02..9706532 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -1,36 +1,64 @@ -name: GoBuild +# This workflow will build a golang project +# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go + +name: Build on: push: branches: - main - - v* + - release/* + pull_request: branches: - main - - develop/* + - release/* -jobs: +env: + CLOUDCAT_PREFIX: "cloudcat_" + BINARY_SUFFIX: "" + CCATCTL_PREFIX: "ccatctl_" + COMMIT_ID: "${{ github.sha }}" + +jobs: build: runs-on: ubuntu-latest + strategy: + matrix: + # build and publish in parallel: linux/386, linux/amd64, windows/386, windows/amd64, darwin/amd64, darwin/arm64 + goos: [linux, windows, darwin] + goarch: ["386", amd64, arm, arm64] + exclude: + - goos: darwin + goarch: arm + - goos: darwin + goarch: "386" + fail-fast: true steps: - - uses: actions/checkout@v2 - - uses: actions/cache@v2 - with: - path: | - ~/.cache/go-build - ~/go/pkg/mod - key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} - restore-keys: | - ${{ runner.os }}-go- + - uses: actions/checkout@v3 + - name: Set up Go - uses: actions/setup-go@v2 + uses: actions/setup-go@v4 with: - go-version: 1.16 + go-version: '1.21' - - name: Test - run: go test -v ./... + - name: Build binary file + env: + GOOS: ${{ matrix.goos }} + GOARCH: ${{ matrix.goarch }} + run: | + if [ $GOOS = "windows" ]; then export BINARY_SUFFIX="$BINARY_SUFFIX.exe"; fi + export BINARY_NAME="$CLOUDCAT_PREFIX"$GOOS"_$GOARCH$BINARY_SUFFIX" + export CGO_ENABLED=0 + export LD_FLAGS="-w -s -X github.com/scriptscat/cloudcat/configs.Version=${COMMIT_ID::7}" + go build -o "bin/$BINARY_NAME" -trimpath -ldflags "$LD_FLAGS" ./cmd/cloudcat - - name: Build - run: make target + export BINARY_NAME="$CCATCTL_PREFIX"$GOOS"_$GOARCH$BINARY_SUFFIX" + go build -o "bin/$BINARY_NAME" -trimpath -ldflags "$LD_FLAGS" ./cmd/ccatctl + + - name: Upload artifact + uses: actions/upload-artifact@v3 + with: + name: ${{ matrix.goos }}_${{ matrix.goarch }} + path: bin/ diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 9ff3803..8ccc331 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -1,81 +1,107 @@ -name: release +name: Release on: push: tags: - - '*' + - 'v*' workflow_dispatch: -jobs: +env: + CLOUDCAT_PREFIX: "cloudcat_" + BINARY_SUFFIX: "" + CCATCTL_PREFIX: "ccatctl_" + COMMIT_ID: "${{ github.sha }}" +jobs: release: runs-on: ubuntu-latest + strategy: + matrix: + # build and publish in parallel: linux/386, linux/amd64, windows/386, windows/amd64, darwin/amd64, darwin/arm64 + goos: [linux, windows, darwin] + goarch: ["386", amd64, arm, arm64] + exclude: + - goos: darwin + goarch: arm + - goos: darwin + goarch: "386" + fail-fast: true steps: - - uses: actions/checkout@v2 - - uses: actions/cache@v2 - with: - path: | - ~/.cache/go-build - ~/go/pkg/mod - key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} - restore-keys: | - ${{ runner.os }}-go- - - name: Set up Go - uses: actions/setup-go@v2 - with: - go-version: 1.16 + - uses: actions/checkout@v3 + + - name: Set up Go + uses: actions/setup-go@v4 + with: + go-version: '1.21' - - name: Test - run: go test -v ./... + - name: Lint + uses: golangci/golangci-lint-action@v3 + with: + version: latest - - name: Build - run: make target + - name: Tests + run: | + go test $(go list ./...) - - 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 binary file + env: + GOOS: ${{ matrix.goos }} + GOARCH: ${{ matrix.goarch }} + run: | + if [ $GOOS = "windows" ]; then export BINARY_SUFFIX="$BINARY_SUFFIX.exe"; fi + export BINARY_NAME="$CLOUDCAT_PREFIX"$GOOS"_$GOARCH$BINARY_SUFFIX" + export CGO_ENABLED=0 + export LD_FLAGS="-w -s -X github.com/scriptscat/cloudcat/configs.Version=${COMMIT_ID::7}" + go build -o "bin/$BINARY_NAME" -trimpath -ldflags "$LD_FLAGS" ./cmd/cloudcat - - name: Get version - id: get_version - run: echo ::set-output name=VERSION::${GITHUB_REF:10} + export BINARY_NAME="$CCATCTL_PREFIX"$GOOS"_$GOARCH$BINARY_SUFFIX" + go build -o "bin/$BINARY_NAME" -trimpath -ldflags "$LD_FLAGS" ./cmd/ccatctl - - name: Build and push - id: docker_build - uses: docker/build-push-action@v2 - with: - push: true - tags: codfrm/cloudcat:${{ steps.get_version.outputs.VERSION }} + - name: Archive production artifacts + env: + GOOS: ${{ matrix.goos }} + GOARCH: ${{ matrix.goarch }} + run: | + if [ "${{ matrix.goos }}" = "windows" ]; then + zip -j "bin/${CLOUDCAT_PREFIX}${GOOS}_${GOARCH}.zip" "bin/"*"${GOOS}_${GOARCH}${BINARY_SUFFIX}" + else + tar czvf "bin/${CLOUDCAT_PREFIX}${GOOS}_${GOARCH}.tar.gz" -C "bin/" *"${GOOS}_${GOARCH}${BINARY_SUFFIX}" + fi - - name: Create Release - id: create_release - uses: actions/create-release@latest - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - with: - tag_name: ${{ github.ref }} - release_name: ${{ github.ref }} - body: | - 'no description' - draft: false - prerelease: false + - name: Create Release + uses: actions/create-release@latest + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # This token is provided by Actions, you do not need to create your own token + with: + tag_name: ${{ github.ref }} + release_name: ${{ github.ref }} + body: | + 'no description' + draft: false + prerelease: false - - name: Build Target - run: | - VERSION=${{ steps.get_version.outputs.VERSION }} GOOS=linux GOARCH=amd64 make target - tar -zcvf cloudcat-${{ steps.get_version.outputs.VERSION }}-linux-amd64.tar.gz cloudcat-${{ steps.get_version.outputs.VERSION }}-linux-amd64 + - name: Upload Release Asset + uses: actions/upload-release-asset@v1 + if: ${{ matrix.goos != 'windows' }} + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + GOOS: ${{ matrix.goos }} + GOARCH: ${{ matrix.goarch }} + with: + upload_url: ${{ steps.create_release.outputs.upload_url }} + asset_path: ./bin/${CLOUDCAT_PREFIX}${GOOS}_${GOARCH}.tar.gz + asset_name: ${CLOUDCAT_PREFIX}${GOOS}_${GOARCH}.tar.gz + asset_content_type: application/gzip - - name: Upload Release Asset zip - uses: actions/upload-release-asset@v1 - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - with: - upload_url: ${{ steps.create_release.outputs.upload_url }} - asset_path: cloudcat-${{ steps.get_version.outputs.VERSION }}-linux-amd64.tar.gz - asset_name: cloudcat-${{ steps.get_version.outputs.VERSION }}-linux-amd64.tar.gz - asset_content_type: application/tar+gz + - name: Upload Release Asset + uses: actions/upload-release-asset@v1 + if: ${{ matrix.goos == 'windows' }} + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + GOOS: ${{ matrix.goos }} + GOARCH: ${{ matrix.goarch }} + with: + upload_url: ${{ steps.create_release.outputs.upload_url }} + asset_path: ./bin/${CLOUDCAT_PREFIX}${GOOS}_${GOARCH}.zip + asset_name: ${CLOUDCAT_PREFIX}${GOOS}_${GOARCH}.zip + asset_content_type: application/zip \ No newline at end of file diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..523183e --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,28 @@ +# This workflow will build a golang project +# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go + +name: Test + +on: + push: + pull_request: + +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + + - name: Set up Go + uses: actions/setup-go@v4 + with: + go-version: '1.21' + + - name: Lint + uses: golangci/golangci-lint-action@v3 + with: + version: latest + + - name: Tests + run: | + go test $(go list ./...) diff --git a/cmd/ccatctl/main.go b/cmd/ccatctl/main.go index f8b995d..1a7f8f6 100644 --- a/cmd/ccatctl/main.go +++ b/cmd/ccatctl/main.go @@ -6,6 +6,8 @@ import ( "os" "runtime" + "github.com/scriptscat/cloudcat/configs" + "github.com/scriptscat/cloudcat/cmd/ccatctl/command" "github.com/scriptscat/cloudcat/pkg/cloudcat_api" "github.com/scriptscat/cloudcat/pkg/utils" @@ -25,8 +27,9 @@ func init() { func main() { config := "" rootCmd := &cobra.Command{ - Use: "ccatctl", - Short: "ccatctl controls the cloudcat service.", + Use: "ccatctl", + Short: "ccatctl controls the cloudcat service.", + Version: configs.Version, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { config, err := utils.Abs(config) if err != nil { diff --git a/cmd/cloudcat/main.go b/cmd/cloudcat/main.go index 769d62b..1f1f059 100644 --- a/cmd/cloudcat/main.go +++ b/cmd/cloudcat/main.go @@ -5,12 +5,16 @@ import ( "log" "runtime" + "github.com/scriptscat/cloudcat/configs" + "github.com/scriptscat/cloudcat/cmd/cloudcat/server" "github.com/scriptscat/cloudcat/pkg/utils" "github.com/spf13/cobra" ) -var configFile = "~/.cloudcat/config.yaml" +var ( + configFile = "~/.cloudcat/config.yaml" +) func init() { // 判断是否为windows @@ -22,8 +26,9 @@ func init() { func main() { var config string rootCmd := &cobra.Command{ - Use: "cloudcat", - Short: "cloudcat service.", + Use: "cloudcat", + Short: "cloudcat service.", + Version: configs.Version, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { // 转为绝对路径 var err error diff --git a/configs/config.go b/configs/config.go index 27459e6..8abf9d3 100644 --- a/configs/config.go +++ b/configs/config.go @@ -1 +1,3 @@ package configs + +var Version = ""