Files
troubleshoot/.github/workflows/affected-tests.yml
2025-10-13 09:52:47 -05:00

144 lines
4.8 KiB
YAML

name: Affected Go Tests
on:
workflow_run:
workflows: ["build"]
types: [completed]
branches: [main]
permissions:
contents: read
jobs:
test-affected:
# Only run if the build workflow succeeded and it's not a draft PR
if: |
github.event.workflow_run.conclusion == 'success' &&
github.event.workflow_run.event == 'pull_request' &&
github.event.workflow_run.pull_requests[0].draft == false
runs-on: ubuntu-latest
timeout-minutes: 30
steps:
- name: Checkout
uses: actions/checkout@v4
with:
# Check out the PR merge commit
ref: ${{ github.event.workflow_run.head_sha }}
fetch-depth: 0
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version-file: go.mod
cache: true
- name: Go Mod Download
run: go mod download
# 1) Build and compile tests only (no execution)
- name: Build (compile-only)
run: go build ./...
- name: Compile tests (no execution)
shell: bash
run: |
set -euo pipefail
while read -r pkg; do
go test -c -o /dev/null "$pkg" || exit 1
done < <(go list ./...)
- name: Get PR Base SHA
id: pr-info
env:
GH_TOKEN: ${{ github.token }}
run: |
# Get the PR number from the workflow run
PR_NUMBER="${{ github.event.workflow_run.pull_requests[0].number }}"
echo "PR Number: ${PR_NUMBER}"
# Get the base SHA for this PR
BASE_SHA=$(gh pr view ${PR_NUMBER} --json baseRefOid -q .baseRefOid)
echo "BASE_SHA=${BASE_SHA}" >> "$GITHUB_OUTPUT"
echo "Base SHA: ${BASE_SHA}"
# 2) Detect relevant unit packages and e2e tests
- name: Compute affected packages
id: affected
env:
BASE_SHA: ${{ steps.pr-info.outputs.BASE_SHA }}
run: |
set -euo pipefail
echo "Base SHA: ${BASE_SHA}"
# Generate affected package list to a file for reuse in subsequent steps
go run ./scripts/affected-packages.go -base "${BASE_SHA}" > /tmp/affected.txt
echo "Affected packages:" || true
if [ -s /tmp/affected.txt ]; then
cat /tmp/affected.txt
else
echo "(none)"
fi
# Expose whether we have any packages to test
if [ -s /tmp/affected.txt ]; then
echo "has_changes=true" >> "$GITHUB_OUTPUT"
else
echo "has_changes=false" >> "$GITHUB_OUTPUT"
fi
- name: Compute affected e2e tests
id: affected_e2e
env:
BASE_SHA: ${{ steps.pr-info.outputs.BASE_SHA }}
run: |
set -euo pipefail
go run ./scripts/affected-packages.go -mode=suites -base "${BASE_SHA}" > /tmp/affected-e2e.txt
awk -F: '$1=="preflight"{print $2}' /tmp/affected-e2e.txt > /tmp/preflight-tests.txt
awk -F: '$1=="support-bundle"{print $2}' /tmp/affected-e2e.txt > /tmp/support-tests.txt
if [ -s /tmp/preflight-tests.txt ] || [ -s /tmp/support-tests.txt ]; then
echo "has_changes=true" >> "$GITHUB_OUTPUT"
else
echo "has_changes=false" >> "$GITHUB_OUTPUT"
fi
# 3) Run filtered tests only
- name: Run unit tests for affected packages
if: steps.affected.outputs.has_changes == 'true'
run: |
set -euo pipefail
# If the script output contains './...' then run all tests
if grep -qx "./..." /tmp/affected.txt; then
echo "Module files changed; running all tests"
go test -race -count=1 ./...
else
echo "Running tests for affected packages"
# xargs will pass the package list as arguments to go test
xargs -a /tmp/affected.txt go test -race -count=1 -v
fi
- name: Run preflight e2e (filtered)
if: steps.affected_e2e.outputs.has_changes == 'true'
run: |
set -euo pipefail
if [ -s /tmp/preflight-tests.txt ]; then
regex="$(tr '\n' '|' < /tmp/preflight-tests.txt | sed 's/|$//')"
go test -v -count=1 ./test/e2e/preflight -run "^((${regex}))$"
else
echo "No preflight e2e changes"
fi
- name: Run support-bundle e2e (filtered)
if: steps.affected_e2e.outputs.has_changes == 'true'
run: |
set -euo pipefail
if [ -s /tmp/support-tests.txt ]; then
regex="$(tr '\n' '|' < /tmp/support-tests.txt | sed 's/|$//')"
go test -v -count=1 ./test/e2e/support-bundle -run "^((${regex}))$"
else
echo "No support-bundle e2e changes"
fi
- name: No affected packages — skip tests
if: steps.affected.outputs.has_changes != 'true'
run: echo "No Go packages affected by this PR; skipping tests."