Skip to content

Commit

Permalink
ci: github action
Browse files Browse the repository at this point in the history
  • Loading branch information
CodFrm committed Oct 4, 2023
1 parent 0634a5d commit ea20d92
Show file tree
Hide file tree
Showing 6 changed files with 179 additions and 87 deletions.
66 changes: 47 additions & 19 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -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/
152 changes: 89 additions & 63 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -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
28 changes: 28 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -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 ./...)
7 changes: 5 additions & 2 deletions cmd/ccatctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand Down
11 changes: 8 additions & 3 deletions cmd/cloudcat/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions configs/config.go
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
package configs

var Version = ""

0 comments on commit ea20d92

Please sign in to comment.