diff --git a/.github/actions/setup-go/action.yml b/.github/actions/setup-go/action.yml new file mode 100644 index 00000000..ff1d4f34 --- /dev/null +++ b/.github/actions/setup-go/action.yml @@ -0,0 +1,39 @@ +name: 'Setup Go Environment' +description: 'Setup Go with caching and common environment variables' +inputs: + go-version-file: + description: 'Path to go.mod file' + required: false + default: 'go.mod' +outputs: + go-version: + description: 'The Go version that was installed' + value: ${{ steps.setup-go.outputs.go-version }} + cache-hit: + description: 'Whether the Go cache was hit' + value: ${{ steps.setup-go.outputs.cache-hit }} +runs: + using: 'composite' + steps: + - name: Setup Go + id: setup-go + uses: actions/setup-go@v5 + with: + go-version-file: ${{ inputs.go-version-file }} + cache: true + + - name: Set Go environment variables + shell: bash + run: | + echo "GOMAXPROCS=2" >> $GITHUB_ENV + echo "GOCACHE=$(go env GOCACHE)" >> $GITHUB_ENV + echo "GOMODCACHE=$(go env GOMODCACHE)" >> $GITHUB_ENV + + - name: Print Go environment + shell: bash + run: | + echo "Go version: $(go version)" + echo "GOOS: $(go env GOOS)" + echo "GOARCH: $(go env GOARCH)" + echo "Cache directory: $(go env GOCACHE)" + echo "Module cache: $(go env GOMODCACHE)" diff --git a/.github/workflows/build-test.yaml b/.github/workflows/build-test.yaml index 9c661c02..f448a052 100644 --- a/.github/workflows/build-test.yaml +++ b/.github/workflows/build-test.yaml @@ -2,145 +2,161 @@ name: build-test on: pull_request: - types: - - opened - - reopened - - synchronize - - ready_for_review - branches: - - v1beta3 + types: [opened, reopened, synchronize, ready_for_review] + branches: [v1beta3] push: - branches: - - "v1beta3" + branches: [v1beta3] concurrency: group: ${{ github.workflow }}-${{ github.ref }} cancel-in-progress: true jobs: - fail_if_pull_request_is_draft: - if: github.event.pull_request.draft == true - runs-on: ubuntu-latest - steps: - - name: Fails in order to indicate that pull request needs to be marked as ready to review and unit tests workflow needs to pass. - run: exit 1 - - test-integration: + # Detect changes to optimize test execution + changes: runs-on: ubuntu-latest + outputs: + go-files: ${{ steps.filter.outputs.go-files }} + preflight: ${{ steps.filter.outputs.preflight }} + support-bundle: ${{ steps.filter.outputs.support-bundle }} + examples: ${{ steps.filter.outputs.examples }} steps: - uses: actions/checkout@v5 - - uses: actions/setup-go@v6 + - uses: dorny/paths-filter@v3 + id: filter with: - go-version-file: 'go.mod' - - uses: replicatedhq/action-k3s@main - id: k3s + filters: | + go-files: + - '**/*.go' + - 'go.{mod,sum}' + - 'Makefile' + preflight: + - 'cmd/preflight/**' + - 'pkg/preflight/**' + support-bundle: + - 'cmd/troubleshoot/**' + - 'pkg/supportbundle/**' + examples: + - 'examples/**' + - 'test/run-examples.sh' + + # Validation jobs + validate: + if: needs.changes.outputs.go-files == 'true' + needs: changes + runs-on: ubuntu-latest + timeout-minutes: 10 + steps: + - uses: actions/checkout@v5 + - uses: ./.github/actions/setup-go + + - name: Check go mod tidy + run: | + go mod tidy + git diff --exit-code go.mod go.sum || { + echo "::error::Please run 'go mod tidy' and commit changes" + exit 1 + } + + - name: Format and vet + run: | + make fmt + git diff --exit-code || { + echo "::error::Please run 'make fmt' and commit changes" + exit 1 + } + make vet + + # Unit and integration tests + test: + if: needs.changes.outputs.go-files == 'true' + needs: [changes, validate] + runs-on: ubuntu-latest + timeout-minutes: 20 + steps: + - uses: actions/checkout@v5 + - uses: ./.github/actions/setup-go + + - name: Setup K3s + uses: replicatedhq/action-k3s@main with: version: v1.31.2-k3s1 - # test-integration includes unit tests - - run: make test-integration - compile-preflight: + - name: Run tests + run: make test-integration + + # Build binaries + build: + if: needs.changes.outputs.go-files == 'true' + needs: [changes, validate] runs-on: ubuntu-latest + timeout-minutes: 10 steps: - uses: actions/checkout@v5 - - uses: actions/setup-go@v6 - with: - go-version-file: 'go.mod' - - run: make preflight + - uses: ./.github/actions/setup-go + - run: make build - uses: actions/upload-artifact@v4 with: - name: preflight - path: bin/preflight - - validate-preflight-e2e: - runs-on: ubuntu-latest - needs: compile-preflight - steps: - - uses: actions/checkout@v5 - - uses: replicatedhq/action-k3s@main - id: k3s - with: - version: v1.31.2-k3s1 - - name: Download preflight binary - uses: actions/download-artifact@v5 - with: - name: preflight + name: binaries path: bin/ - - run: chmod +x bin/preflight - - run: make preflight-e2e-test + retention-days: 1 - run-examples: + # Examples validation + examples: + if: needs.changes.outputs.examples == 'true' || needs.changes.outputs.go-files == 'true' + needs: changes runs-on: ubuntu-latest + timeout-minutes: 10 steps: - uses: actions/checkout@v5 - run: make run-examples - compile-supportbundle: + # E2E tests + e2e: + if: needs.changes.outputs.go-files == 'true' || github.event_name == 'push' + needs: [changes, build] runs-on: ubuntu-latest + timeout-minutes: 15 + strategy: + fail-fast: false + matrix: + include: + - name: preflight + target: preflight-e2e-test + needs-k3s: true + - name: support-bundle-shell + target: support-bundle-e2e-test + needs-k3s: true + - name: support-bundle-go + target: support-bundle-e2e-go-test + needs-k3s: false steps: - uses: actions/checkout@v5 - - uses: actions/setup-go@v6 - with: - go-version-file: 'go.mod' - - run: make support-bundle - - uses: actions/upload-artifact@v4 - with: - name: support-bundle - path: bin/support-bundle - validate-supportbundle-e2e: - runs-on: ubuntu-latest - needs: compile-supportbundle - steps: - - uses: actions/checkout@v5 - - uses: replicatedhq/action-k3s@main - id: k3s + - name: Setup K3s + if: matrix.needs-k3s + uses: replicatedhq/action-k3s@main with: version: v1.31.2-k3s1 - - name: Download support bundle binary - uses: actions/download-artifact@v5 - with: - name: support-bundle - path: bin/ - - run: chmod +x bin/support-bundle - - run: make support-bundle-e2e-test - # Additional e2e tests for support bundle that run in Go, these create a Kind cluster - validate-supportbundle-e2e-go: - runs-on: ubuntu-latest - needs: compile-supportbundle - steps: - - uses: actions/checkout@v5 - - name: Download support bundle binary - uses: actions/download-artifact@v5 + - uses: actions/download-artifact@v4 with: - name: support-bundle + name: binaries path: bin/ - - run: chmod +x bin/support-bundle - - name: Download preflight binary - uses: actions/download-artifact@v5 - with: - name: preflight - path: bin/ - - run: chmod +x bin/preflight - - run: make support-bundle-e2e-go-test - # Summary job - all tests must pass for this to succeed - validate-success: - runs-on: ubuntu-latest - needs: - - tidy-check - - test-integration - - run-examples - - validate-preflight-e2e - - validate-supportbundle-e2e - - validate-supportbundle-e2e-go + - run: chmod +x bin/* + - run: make ${{ matrix.target }} + + # Success summary + success: if: always() + needs: [validate, test, build, examples, e2e] + runs-on: ubuntu-latest steps: - # Check if all required jobs succeeded - - name: fail if any required job failed - if: contains(needs.*.result, 'failure') || contains(needs.*.result, 'cancelled') - run: exit 1 - # Success message - - name: All tests passed - run: echo "All build and test jobs completed successfully" + - name: Check results + run: | + if [[ "${{ contains(needs.*.result, 'failure') || contains(needs.*.result, 'cancelled') }}" == "true" ]]; then + echo "::error::Some jobs failed or were cancelled" + exit 1 + fi + echo "✅ All tests passed!"