test-workflow-act.sh raw
1 #!/usr/bin/env bash
2 # Run GitHub Actions workflow locally using act
3 # Usage: ./scripts/test-workflow-local.sh [job-name]
4 # job-name: optional, defaults to 'build'. Can be 'build' or 'release'
5
6 set -e
7
8 SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
9 WORKFLOW_FILE="${SCRIPT_DIR}/../.github/workflows/go.yml"
10 JOB_NAME="${1:-build}"
11
12 # Check if act is installed
13 if ! command -v act >/dev/null 2>&1; then
14 echo "Error: 'act' is not installed"
15 echo "Install it with:"
16 echo " curl https://raw.githubusercontent.com/nektos/act/master/install.sh | sudo bash"
17 echo " # or on macOS: brew install act"
18 exit 1
19 fi
20
21 echo "=== Running GitHub Actions workflow locally ==="
22 echo "Workflow: .github/workflows/go.yml"
23 echo "Job: $JOB_NAME"
24 echo ""
25
26 case "$JOB_NAME" in
27 build)
28 echo "Running build job..."
29 act push --workflows "$WORKFLOW_FILE" --job build
30 ;;
31 release)
32 echo "Running release job (simulating tag push)..."
33 # Simulate a tag push event with a valid tag format
34 # The workflow requires build to run first and succeed
35 echo "Step 1: Running build job (required dependency)..."
36 if ! act push --workflows "$WORKFLOW_FILE" --job build; then
37 echo "Error: Build job failed. Release job cannot proceed."
38 exit 1
39 fi
40
41 echo ""
42 echo "Step 2: Running release job..."
43 echo "Note: GitHub release creation may fail locally (no valid token), but binary building will be tested"
44 # Use a tag that matches the workflow pattern: v[0-9]+.[0-9]+.[0-9]+
45 # Provide a dummy GITHUB_TOKEN to prevent immediate failure
46 # The release won't actually be created, but the workflow will test binary building
47 # Temporarily disable exit on error to allow release step to fail gracefully
48 set +e
49 GITHUB_REF=refs/tags/v1.0.0 \
50 GITHUB_TOKEN=dummy_token_for_local_testing \
51 act push \
52 --workflows "$WORKFLOW_FILE" \
53 --job release \
54 --secret GITHUB_TOKEN=dummy_token_for_local_testing \
55 --eventpath /dev/stdin <<EOF
56 {
57 "ref": "refs/tags/v1.0.0",
58 "pusher": {"name": "test"},
59 "repository": {
60 "name": "next.orly.dev",
61 "full_name": "test/next.orly.dev"
62 },
63 "head_commit": {
64 "id": "test123"
65 }
66 }
67 EOF
68 RELEASE_EXIT_CODE=$?
69 set -e
70
71 # Check if binary building succeeded (exit code 0) or if only release creation failed
72 if [ $RELEASE_EXIT_CODE -eq 0 ]; then
73 echo "✓ Release job completed successfully (including binary building)"
74 else
75 echo "⚠ Release job completed with errors (likely GitHub release creation failed)"
76 echo " This is expected in local testing. Binary building should have succeeded."
77 echo " Check the output above to verify 'Build Release Binaries' step succeeded."
78 fi
79 ;;
80 *)
81 echo "Error: Unknown job '$JOB_NAME'"
82 echo "Valid jobs: build, release"
83 exit 1
84 ;;
85 esac
86
87 echo ""
88 echo "=== Workflow completed ==="
89
90