name: CI/CD Test 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: 10 # Increased timeout due to additional steps 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 repo, starting from the root directory - name: Run golangci-lint run: | golangci-lint run ./... # This will lint all Go files in the repo and subdirectories # 5. Run unit tests with coverage in the entire repository - name: Run unit tests with coverage run: | # Create a directory to store coverage files mkdir -p $GITHUB_WORKSPACE/coverage_files # Find all *_test.go files test_files=$(find ./ -type f -name '*_test.go') # Check if there are any test files if [ -z "$test_files" ]; then echo "No test cases found in the repository. Skipping test execution." exit 0 fi # Run tests and store coverage for each test directory for test_file in $test_files; do test_dir=$(dirname "$test_file") go_file="${test_file/_test.go/.go}" echo "Running tests in $test_dir for $go_file" go test -v -coverprofile=$GITHUB_WORKSPACE/coverage_files/coverage_$(basename "$go_file" .go).out $test_dir || echo "Tests failed, but continuing." done # 6. Check coverage for each generated coverage file - name: Check coverage for each test file run: | coverage_files=$(find $GITHUB_WORKSPACE/coverage_files -name "coverage_*.out") # If no coverage files were generated, exit without failure if [ -z "$coverage_files" ]; then echo "No coverage files found. Skipping coverage check." exit 0 fi for coverage_file in $coverage_files; do echo "Checking coverage for $coverage_file" coverage=$(go tool cover -func=$coverage_file | grep total | awk '{print $3}' | sed 's/%//') echo "Coverage for $coverage_file: $coverage%" if (( $(echo "$coverage < 90" | bc -l) )); then echo "Coverage for $coverage_file is below 90%. Failing the job." exit 1 fi done