diff --git a/go-ci.yml b/go-ci.yml new file mode 100644 index 0000000..5bf0eed --- /dev/null +++ b/go-ci.yml @@ -0,0 +1,82 @@ +name: Go Project CI Workflow + +# 定义触发条件 +on: + push: + branches: + - main + pull_request: + branches: + - main + schedule: + - cron: '0 2 * * *' # 每天凌晨 2 点定时触发 + +jobs: + # 定义第一个Job,编译和测试 + build-test: + runs-on: ubuntu-latest + strategy: + matrix: + go-version: ['1.19', '1.20'] # 测试不同的Go版本兼容性 + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Set up Go + uses: actions/setup-go@v2 + with: + go-version: ${{ matrix.go-version }} + + - name: Install dependencies + run: go mod download + + - name: Run unit tests + run: go test ./... -v + + - name: Lint code + run: | + go vet ./... # 静态代码分析 + golangci-lint run # 使用第三方 lint 工具检查 + + - name: Check formatting + run: go fmt ./... + + # 定义第二个 Job,测试代码覆盖率 + test-coverage: + runs-on: ubuntu-latest + needs: build-test + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Set up Go + uses: actions/setup-go@v2 + with: + go-version: '1.20' + + - name: Install dependencies + run: go mod download + + - name: Run coverage tests + run: | + go test ./... -coverprofile=coverage.out + go tool cover -func=coverage.out + + - name: Upload coverage report + uses: actions/upload-artifact@v2 + with: + name: coverage-report + path: coverage.out + + # 定义第三个 Job,进行安全扫描 + security-scan: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Install dependencies + run: go mod download + + - name: Security check + run: gosec ./... # 使用 gosec 工具进行安全扫描