Run relevant tests #10
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Affected Go Tests | |
on: | |
pull_request: | |
types: [opened, reopened, synchronize, ready_for_review] | |
permissions: | |
contents: read | |
jobs: | |
test-affected: | |
if: github.event.pull_request.draft == false | |
runs-on: ubuntu-latest | |
timeout-minutes: 30 | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
with: | |
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 | |
- name: Compute base ref | |
id: pr-info | |
run: | | |
echo "BASE_REF=origin/${{ github.base_ref }}" >> "$GITHUB_OUTPUT" | |
echo "Base ref: origin/${{ github.base_ref }}" | |
# 2) Detect relevant unit packages and e2e tests | |
- name: Compute affected packages | |
id: affected | |
run: | | |
set -euo pipefail | |
echo "Base: ${{ steps.pr-info.outputs.BASE_REF }}" | |
# Generate affected package list to a file for reuse in subsequent steps | |
go run ./scripts/affected-packages.go -base "${{ steps.pr-info.outputs.BASE_REF }}" > /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 | |
run: | | |
set -euo pipefail | |
go run ./scripts/affected-packages.go -mode=suites -base "${{ steps.pr-info.outputs.BASE_REF }}" > /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 | |
- name: Publish affected summary | |
if: always() | |
run: | | |
{ | |
echo "### Affected unit packages"; | |
if [ -s /tmp/affected.txt ]; then | |
sed 's/^/- /' /tmp/affected.txt; | |
else | |
echo "- (none)"; | |
fi; | |
echo; | |
echo "### Affected e2e tests"; | |
if [ -s /tmp/affected-e2e.txt ]; then | |
sed 's/^/- /' /tmp/affected-e2e.txt; | |
else | |
echo "- (none)"; | |
fi; | |
} | tee -a "$GITHUB_STEP_SUMMARY" | |
# Provision a Kubernetes cluster for e2e using Replicated compatibility actions | |
- name: Create k3s cluster | |
id: create-cluster | |
if: steps.affected_e2e.outputs.has_changes == 'true' | |
uses: replicatedhq/compatibility-actions/create-cluster@v1 | |
with: | |
api-token: ${{ secrets.REPLICATED_API_TOKEN }} | |
kubernetes-distribution: k3s | |
cluster-name: pr-${{ github.run_id }}-${{ github.run_attempt }} | |
ttl: 25m | |
timeout-minutes: 5 | |
- name: Configure kubeconfig | |
if: steps.affected_e2e.outputs.has_changes == 'true' | |
run: | | |
echo "${{ steps.create-cluster.outputs.cluster-kubeconfig }}" > $GITHUB_WORKSPACE/kubeconfig.yaml | |
echo "KUBECONFIG=$GITHUB_WORKSPACE/kubeconfig.yaml" >> $GITHUB_ENV | |
- name: Build binaries for script-based e2e | |
if: steps.affected_e2e.outputs.has_changes == 'true' | |
run: | | |
make bin/preflight bin/support-bundle | |
# 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" | |
make test | |
else | |
echo "Running tests for affected packages" | |
pkgs=$(tr '\n' ' ' < /tmp/affected.txt) | |
PACKAGES="$pkgs" make test-packages | |
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="$(grep -v '^$' /tmp/preflight-tests.txt | tr '\n' '|' | sed 's/|$//')" | |
if [ -n "$regex" ]; then | |
RUN="^(${regex})$" make preflight-e2e-test | |
else | |
echo "No valid preflight tests matched after filtering" | |
fi | |
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="$(grep -v '^$' /tmp/support-tests.txt | tr '\n' '|' | sed 's/|$//')" | |
if [ -n "$regex" ]; then | |
# Ensure no stale kind cluster from previous steps | |
docker rm -f kind-cluster-control-plane 2>/dev/null || true | |
# Scope to support-bundle suite only to avoid preflight kind interactions | |
E2EPATHS=./test/e2e/support-bundle RUN="^(${regex})$" make support-bundle-e2e-go-test | |
else | |
echo "No valid support-bundle tests matched after filtering" | |
fi | |
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." | |
# Cleanup cluster | |
- name: Remove cluster | |
if: always() && steps.affected_e2e.outputs.has_changes == 'true' | |
uses: replicatedhq/compatibility-actions/remove-cluster@v1 | |
continue-on-error: true | |
with: | |
api-token: ${{ secrets.REPLICATED_API_TOKEN }} | |
cluster-id: ${{ steps.create-cluster.outputs.cluster-id }} | |