diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..596833d --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,117 @@ +name: CI + +on: + push: + pull_request: + +jobs: + build: + name: Build + runs-on: ubuntu-latest + strategy: + matrix: + goos: [linux, windows, darwin] + goarch: [amd64, arm64, arm] + exclude: + - goos: darwin + goarch: arm + - goos: windows + goarch: arm + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version: '1.24' + + - name: Install Protoc + uses: arduino/setup-protoc@v3 + + - name: Install Protoc Gen Go + run: | + go install google.golang.org/protobuf/cmd/protoc-gen-go@latest + go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest + + - name: Generate Protos + run: make proto + + - name: Build + env: + GOOS: ${{ matrix.goos }} + GOARCH: ${{ matrix.goarch }} + CGO_ENABLED: 0 + run: | + VERSION=${{ github.sha }} + COMMIT=${{ github.sha }} + BUILD_TIME=$(date -u +"%Y-%m-%dT%H:%M:%SZ") + LDFLAGS="-X 'github.com/MINIOpenSource/CIMS-backend/internal/pkg/version.Version=$VERSION' -X 'github.com/MINIOpenSource/CIMS-backend/internal/pkg/version.CommitHash=$COMMIT' -X 'github.com/MINIOpenSource/CIMS-backend/internal/pkg/version.BuildTime=$BUILD_TIME' -s -w" + + OUTPUT_NAME=cims-backend-${{ matrix.goos }}-${{ matrix.goarch }} + if [ "${{ matrix.goos }}" = "windows" ]; then + OUTPUT_NAME+='.exe' + fi + go build -ldflags "$LDFLAGS" -o $OUTPUT_NAME ./cmd/cims/main.go + + - name: Upload Artifact + uses: actions/upload-artifact@v4 + with: + name: cims-backend-${{ matrix.goos }}-${{ matrix.goarch }} + path: cims-backend-* + retention-days: 1 + + lint: + name: Lint + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version: '1.24' + + - name: Install Protoc + uses: arduino/setup-protoc@v3 + + - name: Install Protoc Gen Go + run: | + go install google.golang.org/protobuf/cmd/protoc-gen-go@latest + go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest + + - name: Generate Protos + run: make proto + + - name: Run golangci-lint + uses: golangci/golangci-lint-action@v6 + with: + version: latest + args: --timeout=5m + + test: + name: Test + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version: '1.24' + + - name: Install Protoc + uses: arduino/setup-protoc@v3 + + - name: Install Protoc Gen Go + run: | + go install google.golang.org/protobuf/cmd/protoc-gen-go@latest + go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest + + - name: Generate Protos + run: make proto + + - name: Run tests + run: go test -v ./... diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 9736a8f..c2c47b2 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -12,11 +12,11 @@ jobs: strategy: matrix: goos: [linux, windows, darwin] - goarch: [amd64, arm64, 386, arm] + goarch: [amd64, arm64, arm] exclude: - goos: darwin - goarch: 386 - - goos: darwin + goarch: arm + - goos: windows goarch: arm steps: - name: Checkout code @@ -25,7 +25,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v5 with: - go-version: '1.22' + go-version: '1.24' - name: Install Protoc uses: arduino/setup-protoc@v3 @@ -44,11 +44,16 @@ jobs: GOARCH: ${{ matrix.goarch }} CGO_ENABLED: 0 run: | + VERSION=${{ github.ref_name }} + COMMIT=${{ github.sha }} + BUILD_TIME=$(date -u +"%Y-%m-%dT%H:%M:%SZ") + LDFLAGS="-X 'github.com/MINIOpenSource/CIMS-backend/internal/pkg/version.Version=$VERSION' -X 'github.com/MINIOpenSource/CIMS-backend/internal/pkg/version.CommitHash=$COMMIT' -X 'github.com/MINIOpenSource/CIMS-backend/internal/pkg/version.BuildTime=$BUILD_TIME' -s -w" + OUTPUT_NAME=cims-backend-${{ matrix.goos }}-${{ matrix.goarch }} if [ "${{ matrix.goos }}" = "windows" ]; then OUTPUT_NAME+='.exe' fi - go build -o $OUTPUT_NAME ./cmd/cims/main.go + go build -ldflags "$LDFLAGS" -o $OUTPUT_NAME ./cmd/cims/main.go - name: Upload Artifact uses: actions/upload-artifact@v4 diff --git a/Makefile b/Makefile index 9d97895..ec54675 100644 --- a/Makefile +++ b/Makefile @@ -76,5 +76,13 @@ proto: clean: rm -rf internal/proto +VERSION := $(shell git describe --tags --always --dirty || echo "dev") +COMMIT := $(shell git rev-parse --short HEAD || echo "none") +BUILD_TIME := $(shell date -u +"%Y-%m-%dT%H:%M:%SZ") + +LDFLAGS := -X 'github.com/MINIOpenSource/CIMS-backend/internal/pkg/version.Version=$(VERSION)' \ + -X 'github.com/MINIOpenSource/CIMS-backend/internal/pkg/version.CommitHash=$(COMMIT)' \ + -X 'github.com/MINIOpenSource/CIMS-backend/internal/pkg/version.BuildTime=$(BUILD_TIME)' + build: - go build -o cims_server ./cmd/cims/main.go + go build -ldflags "$(LDFLAGS)" -o cims_server ./cmd/cims/main.go diff --git a/cmd/cims/main.go b/cmd/cims/main.go index 6bd0a73..33f05d4 100644 --- a/cmd/cims/main.go +++ b/cmd/cims/main.go @@ -10,6 +10,7 @@ import ( "github.com/MINIOpenSource/CIMS-backend/internal/api/grpc" v1 "github.com/MINIOpenSource/CIMS-backend/internal/api/http/v1" "github.com/MINIOpenSource/CIMS-backend/internal/data" + "github.com/MINIOpenSource/CIMS-backend/internal/pkg/version" "github.com/MINIOpenSource/CIMS-backend/internal/proto/Protobuf/Service" "github.com/gin-gonic/gin" "github.com/kardianos/service" @@ -18,7 +19,6 @@ import ( ) var ( - cfgFile string dbPath string port int grpcPort int @@ -168,9 +168,20 @@ func main() { }, } + var versionCmd = &cobra.Command{ + Use: "version", + Short: "Print the version number of CIMS", + Run: func(cmd *cobra.Command, args []string) { + fmt.Printf("CIMS-backend Version: %s\n", version.Version) + fmt.Printf("Git Commit: %s\n", version.CommitHash) + fmt.Printf("Build Time: %s\n", version.BuildTime) + }, + } + rootCmd.AddCommand(startCmd) rootCmd.AddCommand(serviceCmd) rootCmd.AddCommand(initCmd) + rootCmd.AddCommand(versionCmd) if err := rootCmd.Execute(); err != nil { fmt.Println(err) diff --git a/internal/api/grpc/command.go b/internal/api/grpc/command.go index 2145cf6..2323052 100644 --- a/internal/api/grpc/command.go +++ b/internal/api/grpc/command.go @@ -70,7 +70,7 @@ func (s *ClientCommandDeliverServer) ListenCommand(stream Service.ClientCommandD } if req.Type == Enum.CommandTypes_Ping { - s.clientService.UpdateHeartbeat(clientID, clientIP) + _ = s.clientService.UpdateHeartbeat(clientID, clientIP) pong := &Server.ClientCommandDeliverScRsp{ RetCode: Enum.Retcode_Success, Type: Enum.CommandTypes_Pong, diff --git a/internal/pkg/version/version.go b/internal/pkg/version/version.go new file mode 100644 index 0000000..7c249b8 --- /dev/null +++ b/internal/pkg/version/version.go @@ -0,0 +1,10 @@ +package version + +var ( + // Version is the current version of the application. + Version = "dev" + // CommitHash is the git commit hash of the build. + CommitHash = "none" + // BuildTime is the time when the application was built. + BuildTime = "unknown" +) diff --git a/internal/service/command.go b/internal/service/command.go index 069ed09..7b69ac9 100644 --- a/internal/service/command.go +++ b/internal/service/command.go @@ -35,9 +35,7 @@ func (s *CommandService) RegisterClientChannel(clientID string, ch chan *Server. func (s *CommandService) UnregisterClientChannel(clientID string) { s.mu.Lock() defer s.mu.Unlock() - if _, ok := s.clientChans[clientID]; ok { - delete(s.clientChans, clientID) - } + delete(s.clientChans, clientID) } // SendCommand sends a command to a specific client