name: CI/CD Pipeline on: pull_request: branches: - beckn-onix-v1.0-develop # Trigger when a PR is raised for merging into `beck-onix-v1.0-develop` env: # AR_LOCATION: "${{ secrets.GCP_PROJECT_REGION }}-docker.pkg.dev/${{ secrets.GCP_PROJECT_ID }}/${{ secrets.GCP_AR_REPO }}" APP_DIRECTORY: "shared/plugin/implementations/signer" REF_BRANCH: "feature/*" 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 with: ref: ${{ env.REF_BRANCH }} # Make sure we're on the 'test' branch for checking out code fetch-depth: 1 # 2. Debugging: List files in the repository root after checkout - name: List files in the root directory run: | echo "Current working directory is: $(pwd)" echo "Listing files in the root directory:" ls -al # 3. Set up Go environment - name: Set up Go 1.24.0 uses: actions/setup-go@v4 with: go-version: '1.24.0' # 4. Navigate to the app directory - name: Navigate to app directory run: | echo "Changing directory to ${{ env.APP_DIRECTORY }}" cd ${{ env.APP_DIRECTORY }} echo "Listing files in ${{ env.APP_DIRECTORY }} directory:" ls -al # List files in the ${{ env.APP_DIRECTORY }} directory # 5. Install golangci-lint - name: Install golangci-lint run: | echo "Installing golangci-lint..." go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest echo "golangci-lint installed to: $(go env GOPATH)/bin/" ls -l $(go env GOPATH)/bin/ # Debugging: List the contents of the bin directory # 6. Verify golangci-lint binary location - name: Verify golangci-lint binary run: | echo "Checking if golangci-lint is installed..." ls $(go env GOPATH)/bin/ # Debugging: Check if golangci-lint is present if [ ! -f $(go env GOPATH)/bin/golangci-lint ]; then echo "golangci-lint was not found, exiting." exit 1 fi # 7. Run Linter - name: Run golangci-lint run: | echo "Running golangci-lint..." cd ${{ env.APP_DIRECTORY }} golangci-lint run # 8. Run unit tests with coverage - name: Run unit tests with coverage run: | cd ${{ env.APP_DIRECTORY }} echo "Running unit tests..." go test -v -coverprofile=coverage.out go tool cover -func=coverage.out | tee coverage.txt # 9. Check if coverage is >= 90% - name: Check coverage percentage run: | cd ${{ env.APP_DIRECTORY }} # 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 # 10. Build the Go code - name: Build Go code run: | echo "Building the Go application..." cd ${{ env.APP_DIRECTORY }} go build -o myapp # Verify if the build was successful if [ ! -f myapp ]; then echo "Build failed: myapp executable was not created." exit 1 else echo "Build succeeded: myapp executable created." fi