name: CI/CD Pipeline on: pull_request: branches: - beckn-onix-v1.0-develop env: APP_DIRECTORY: "shared/plugin" # Root directory to start searching from jobs: lint_and_test: runs-on: ubuntu-latest if: github.event_name == 'pull_request' timeout-minutes: 5 steps: # 1. Checkout the code from the test branch (triggered by PR) - name: Checkout code uses: actions/checkout@v4 # 2. Set up Go environment - name: Set up Go 1.24.0 uses: actions/setup-go@v4 with: go-version: '1.24.0' # 3. Install golangci-lint - name: Install golangci-lint run: | go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest # 4. Run golangci-lint on the entire APP_DIRECTORY (including subdirectories) ${{ env.APP_DIRECTORY }} - name: Run golangci-lint run: | golangci-lint run ./... # 5. Run unit tests with coverage in the entire repository - name: Run unit tests with coverage run: | # Find all directories with Go test files and run `go test` on them find ./ -type f -name '*_test.go' -exec dirname {} \; | sort -u | while read dir; do echo "Running tests in $dir" go test -v -coverprofile=coverage.out $dir go tool cover -func=coverage.out | tee coverage.txt done # 6. Check if coverage is >= 90%, but only check coverage if tests exist - name: Check coverage percentage run: | # Check if coverage file exists if [ -f coverage.out ]; then # Extract total coverage percentage from the output coverage=$(go tool cover -func=coverage.out | grep total | awk '{print $3}' | sed 's/%//') echo "Total coverage: $coverage%" # Check if coverage is greater than or equal to 90% if (( $(echo "$coverage < 90" | bc -l) )); then echo "Coverage is below 90%. Failing the job." exit 1 else echo "Coverage is 90% or above. Continuing the job." fi else echo "No coverage file found. Skipping coverage check." fi # 7. Build the Go code #- name: Build Go code # run: | # go build -o myapp ${{ env.APP_DIRECTORY }}/... # if [ ! -f myapp ]; then # echo "Build failed: myapp executable was not created." # exit 1 # else # echo "Build succeeded: myapp executable created." # fi