Skip to content

Commit 69f7f7a

Browse files
committed
ci: github action
1 parent 0634a5d commit 69f7f7a

File tree

6 files changed

+180
-88
lines changed

6 files changed

+180
-88
lines changed

.github/workflows/build.yml

+60-19
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,77 @@
1-
name: GoBuild
1+
# This workflow will build a golang project
2+
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go
3+
4+
name: Build
25

36
on:
47
push:
58
branches:
69
- main
7-
- v*
10+
- release/*
11+
812
pull_request:
913
branches:
1014
- main
11-
- develop/*
15+
- release/*
1216

13-
jobs:
17+
env:
18+
CLOUDCAT: "cloudcat"
19+
BINARY_SUFFIX: ""
20+
CCATCTL: "ccatctl"
21+
COMMIT_ID: "${{ github.sha }}"
1422

23+
24+
jobs:
1525
build:
1626
runs-on: ubuntu-latest
27+
strategy:
28+
matrix:
29+
# build and publish in parallel: linux/386, linux/amd64, windows/386, windows/amd64, darwin/amd64, darwin/arm64
30+
goos: [linux, windows, darwin]
31+
goarch: ["386", amd64, arm, arm64]
32+
exclude:
33+
- goos: darwin
34+
goarch: arm
35+
- goos: darwin
36+
goarch: "386"
37+
fail-fast: true
1738
steps:
18-
- uses: actions/checkout@v2
19-
- uses: actions/cache@v2
20-
with:
21-
path: |
22-
~/.cache/go-build
23-
~/go/pkg/mod
24-
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
25-
restore-keys: |
26-
${{ runner.os }}-go-
39+
- uses: actions/checkout@v3
40+
2741
- name: Set up Go
28-
uses: actions/setup-go@v2
42+
uses: actions/setup-go@v4
2943
with:
30-
go-version: 1.16
44+
go-version: '1.21'
45+
46+
- name: Build binary file
47+
env:
48+
GOOS: ${{ matrix.goos }}
49+
GOARCH: ${{ matrix.goarch }}
50+
run: |
51+
if [ $GOOS = "windows" ]; then export BINARY_SUFFIX="$BINARY_SUFFIX.exe"; fi
52+
export CGO_ENABLED=0
53+
export LD_FLAGS="-w -s -X github.com/scriptscat/cloudcat/configs.Version=${COMMIT_ID::7}"
54+
55+
go build -o "bin/${CLOUDCAT}${BINARY_SUFFIX}" -trimpath -ldflags "$LD_FLAGS" ./cmd/cloudcat
56+
go build -o "bin/${CCATCTL}${BINARY_SUFFIX}" -trimpath -ldflags "$LD_FLAGS" ./cmd/ccatctl
3157
32-
- name: Test
33-
run: go test -v ./...
58+
cd bin
59+
if [ "${{ matrix.goos }}" = "windows" ]; then
60+
zip -j "${CLOUDCAT}_${GOOS}_${GOARCH}.zip" "${CCATCTL}.exe" "${CLOUDCAT}.exe"
61+
else
62+
tar czvf "${CLOUDCAT}_${GOOS}_${GOARCH}.tar.gz" "${CCATCTL}" "${CLOUDCAT}"
63+
fi
3464
35-
- name: Build
36-
run: make target
65+
- name: Upload artifact
66+
uses: actions/upload-artifact@v3
67+
if: ${{ matrix.goos != 'windows' }}
68+
with:
69+
name: ${{ matrix.goos }}_${{ matrix.goarch }}
70+
path: bin/*.tar.gz
71+
72+
- name: Upload windows artifact
73+
uses: actions/upload-artifact@v3
74+
if: ${{ matrix.goos == 'windows' }}
75+
with:
76+
name: ${{ matrix.goos }}_${{ matrix.goarch }}
77+
path: bin/*.zip

.github/workflows/release.yml

+77-64
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,94 @@
1-
name: release
1+
name: Release
22

33
on:
44
push:
55
tags:
6-
- '*'
6+
- 'v*'
77
workflow_dispatch:
88

9-
jobs:
9+
env:
10+
CLOUDCAT: "cloudcat"
11+
BINARY_SUFFIX: ""
12+
CCATCTL: "ccatctl"
13+
COMMIT_ID: "${{ github.sha }}"
1014

11-
release:
15+
jobs:
16+
build:
1217
runs-on: ubuntu-latest
18+
strategy:
19+
matrix:
20+
# build and publish in parallel: linux/386, linux/amd64, windows/386, windows/amd64, darwin/amd64, darwin/arm64
21+
goos: [linux, windows, darwin]
22+
goarch: ["386", amd64, arm, arm64]
23+
exclude:
24+
- goos: darwin
25+
goarch: arm
26+
- goos: darwin
27+
goarch: "386"
28+
fail-fast: true
1329
steps:
14-
- uses: actions/checkout@v2
15-
- uses: actions/cache@v2
16-
with:
17-
path: |
18-
~/.cache/go-build
19-
~/go/pkg/mod
20-
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
21-
restore-keys: |
22-
${{ runner.os }}-go-
23-
- name: Set up Go
24-
uses: actions/setup-go@v2
25-
with:
26-
go-version: 1.16
30+
- uses: actions/checkout@v3
31+
32+
- name: Set up Go
33+
uses: actions/setup-go@v4
34+
with:
35+
go-version: '1.21'
2736

28-
- name: Test
29-
run: go test -v ./...
37+
- name: Lint
38+
uses: golangci/golangci-lint-action@v3
39+
with:
40+
version: latest
3041

31-
- name: Build
32-
run: make target
42+
- name: Tests
43+
run: |
44+
go test $(go list ./...)
3345
34-
- name: Set up QEMU
35-
uses: docker/setup-qemu-action@v1
36-
- name: Set up Docker Buildx
37-
uses: docker/setup-buildx-action@v1
38-
- name: Login to DockerHub
39-
uses: docker/login-action@v1
40-
with:
41-
username: ${{ secrets.DOCKERHUB_USERNAME }}
42-
password: ${{ secrets.DOCKERHUB_TOKEN }}
46+
- name: Build binary file
47+
env:
48+
GOOS: ${{ matrix.goos }}
49+
GOARCH: ${{ matrix.goarch }}
50+
run: |
51+
if [ $GOOS = "windows" ]; then export BINARY_SUFFIX="$BINARY_SUFFIX.exe"; fi
52+
export CGO_ENABLED=0
53+
export LD_FLAGS="-w -s -X github.com/scriptscat/cloudcat/configs.Version=${COMMIT_ID::7}"
4354
44-
- name: Get version
45-
id: get_version
46-
run: echo ::set-output name=VERSION::${GITHUB_REF:10}
55+
go build -o "bin/${CLOUDCAT}${BINARY_SUFFIX}" -trimpath -ldflags "$LD_FLAGS" ./cmd/cloudcat
56+
go build -o "bin/${CCATCTL}${BINARY_SUFFIX}" -trimpath -ldflags "$LD_FLAGS" ./cmd/ccatctl
4757
48-
- name: Build and push
49-
id: docker_build
50-
uses: docker/build-push-action@v2
51-
with:
52-
push: true
53-
tags: codfrm/cloudcat:${{ steps.get_version.outputs.VERSION }}
58+
cd bin
59+
if [ "${{ matrix.goos }}" = "windows" ]; then
60+
zip -j "${CLOUDCAT}_${GOOS}_${GOARCH}.zip" "${CCATCTL}.exe" "${CLOUDCAT}.exe"
61+
else
62+
tar czvf "${CLOUDCAT}_${GOOS}_${GOARCH}.tar.gz" "${CCATCTL}" "${CLOUDCAT}"
63+
fi
5464
55-
- name: Create Release
56-
id: create_release
57-
uses: actions/create-release@latest
58-
env:
59-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
60-
with:
61-
tag_name: ${{ github.ref }}
62-
release_name: ${{ github.ref }}
63-
body: |
64-
'no description'
65-
draft: false
66-
prerelease: false
65+
- name: Upload artifact
66+
uses: actions/upload-artifact@v3
67+
if: ${{ matrix.goos != 'windows' }}
68+
with:
69+
name: ${{ matrix.goos }}_${{ matrix.goarch }}
70+
path: bin/*.tar.gz
6771

68-
- name: Build Target
69-
run: |
70-
VERSION=${{ steps.get_version.outputs.VERSION }} GOOS=linux GOARCH=amd64 make target
71-
tar -zcvf cloudcat-${{ steps.get_version.outputs.VERSION }}-linux-amd64.tar.gz cloudcat-${{ steps.get_version.outputs.VERSION }}-linux-amd64
72+
- name: Upload windows artifact
73+
uses: actions/upload-artifact@v3
74+
if: ${{ matrix.goos == 'windows' }}
75+
with:
76+
name: ${{ matrix.goos }}_${{ matrix.goarch }}
77+
path: bin/*.zip
78+
79+
release:
80+
needs: build
81+
runs-on: ubuntu-latest
82+
steps:
83+
- uses: actions/checkout@v3
84+
# 拿到build产物
85+
- uses: actions/download-artifact@v3
86+
with:
87+
path: bin/
7288

73-
- name: Upload Release Asset zip
74-
uses: actions/upload-release-asset@v1
75-
env:
76-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
77-
with:
78-
upload_url: ${{ steps.create_release.outputs.upload_url }}
79-
asset_path: cloudcat-${{ steps.get_version.outputs.VERSION }}-linux-amd64.tar.gz
80-
asset_name: cloudcat-${{ steps.get_version.outputs.VERSION }}-linux-amd64.tar.gz
81-
asset_content_type: application/tar+gz
89+
- uses: ncipollo/release-action@v1
90+
with:
91+
artifacts: "bin/*/*.tar.gz,bin/*/*.zip"
92+
body: "no describe"
93+
# 判断是否为预发布(包含alpha、beta等关键字)
94+
prerelease: ${{ contains(github.ref, 'alpha') || contains(github.ref, 'beta') }}

.github/workflows/test.yml

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# This workflow will build a golang project
2+
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go
3+
4+
name: Test
5+
6+
on:
7+
push:
8+
pull_request:
9+
10+
jobs:
11+
test:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v3
15+
16+
- name: Set up Go
17+
uses: actions/setup-go@v4
18+
with:
19+
go-version: '1.21'
20+
21+
- name: Lint
22+
uses: golangci/golangci-lint-action@v3
23+
with:
24+
version: latest
25+
26+
- name: Tests
27+
run: |
28+
go test $(go list ./...)

cmd/ccatctl/main.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"os"
77
"runtime"
88

9+
"github.com/scriptscat/cloudcat/configs"
10+
911
"github.com/scriptscat/cloudcat/cmd/ccatctl/command"
1012
"github.com/scriptscat/cloudcat/pkg/cloudcat_api"
1113
"github.com/scriptscat/cloudcat/pkg/utils"
@@ -25,8 +27,9 @@ func init() {
2527
func main() {
2628
config := ""
2729
rootCmd := &cobra.Command{
28-
Use: "ccatctl",
29-
Short: "ccatctl controls the cloudcat service.",
30+
Use: "ccatctl",
31+
Short: "ccatctl controls the cloudcat service.",
32+
Version: configs.Version,
3033
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
3134
config, err := utils.Abs(config)
3235
if err != nil {

cmd/cloudcat/main.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@ import (
55
"log"
66
"runtime"
77

8+
"github.com/scriptscat/cloudcat/configs"
9+
810
"github.com/scriptscat/cloudcat/cmd/cloudcat/server"
911
"github.com/scriptscat/cloudcat/pkg/utils"
1012
"github.com/spf13/cobra"
1113
)
1214

13-
var configFile = "~/.cloudcat/config.yaml"
15+
var (
16+
configFile = "~/.cloudcat/config.yaml"
17+
)
1418

1519
func init() {
1620
// 判断是否为windows
@@ -22,8 +26,9 @@ func init() {
2226
func main() {
2327
var config string
2428
rootCmd := &cobra.Command{
25-
Use: "cloudcat",
26-
Short: "cloudcat service.",
29+
Use: "cloudcat",
30+
Short: "cloudcat service.",
31+
Version: configs.Version,
2732
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
2833
// 转为绝对路径
2934
var err error

configs/config.go

+2
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
package configs
2+
3+
var Version = ""

0 commit comments

Comments
 (0)