Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 23, 2025

Investigation: PR Branch Checkout Step Failed

Problem Analysis

The "Checkout PR branch" step (step 18) failed in workflow run 19614059145 with error:

gh: To use GitHub CLI in a GitHub Actions workflow, set the GH_TOKEN environment variable
Failed to checkout PR branch: The process '/usr/bin/gh' failed with exit code 4

Root Cause: The checkout_pr_branch.cjs script executes gh pr checkout but the step didn't pass the GH_TOKEN environment variable that the gh CLI requires.

Fix Implementation

  • Analyze the failure and identify root cause
  • Fix pr.go to add env section with GH_TOKEN
  • Fix pr.go to add github-token parameter in with section
  • Simplify checkout_pr_branch.cjs by removing redundant env passing
  • Add test to verify GH_TOKEN configuration
  • Update JavaScript tests to match new behavior
  • Address code review feedback: Remove trailing whitespace
  • Fix integration test failure: Token precedence
  • Build and test the fix
  • Verify the fix works correctly with compiled workflows

Technical Details

Changes Made

  1. pkg/workflow/pr.go - generatePRReadyForReviewCheckout():

    • Added env: section with GH_TOKEN: <token> before the with: section
    • Added github-token: <token> parameter in the with: section
    • Uses getEffectiveGitHubToken() to determine the appropriate token
    • Fixed trailing whitespace
    • Respects safe-outputs token precedence: When safe-outputs.github-token is configured, it takes precedence over the top-level github-token
  2. pkg/workflow/js/checkout_pr_branch.cjs:

    • Removed explicit env passing in exec.exec() call
    • The gh CLI now picks up GH_TOKEN from the step's environment
    • Simplified from: exec.exec("gh", ["pr", "checkout", ...], { env: {...} })
    • To: exec.exec("gh", ["pr", "checkout", ...])
  3. pkg/workflow/pr_checkout_test.go:

    • Added TestPRCheckoutGHTokenConfiguration() test
    • Verifies env section contains GH_TOKEN
    • Verifies with section contains github-token
    • Verifies JavaScript doesn't have redundant env code
  4. pkg/workflow/js/checkout_pr_branch.test.cjs:

    • Updated tests to expect simplified exec calls without env options
    • Tests now verify the correct behavior where GH_TOKEN comes from step env

Verification

  • ✅ All 87 workflows compile successfully
  • ✅ All Go unit tests pass
  • ✅ All Go integration tests pass (including TestTopLevelGitHubTokenPrecedence)
  • ✅ All JavaScript tests pass (65 test files, 1222 tests)
  • ✅ Linting passes with no issues
  • ✅ Compiled workflows show correct env and with sections

Example Generated Output:

- name: Checkout PR branch
  if: github.event.pull_request
  uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8
  env:
    GH_TOKEN: ${{ secrets.GH_AW_GITHUB_TOKEN || secrets.GITHUB_TOKEN }}
  with:
    github-token: ${{ secrets.GH_AW_GITHUB_TOKEN || secrets.GITHUB_TOKEN }}
    script: |
      # JavaScript code...
      await exec.exec("gh", ["pr", "checkout", prNumber.toString()]);

Token Precedence

The PR checkout step now follows this token precedence:

  1. safe-outputs.github-token (if configured)
  2. Top-level github-token (if configured)
  3. Default fallback: ${{ secrets.GH_AW_GITHUB_TOKEN || secrets.GITHUB_TOKEN }}

This ensures consistent token usage across all workflow jobs and prevents the top-level token from appearing in workflows that have safe-outputs tokens configured.

Why This Fix Works

The gh CLI in GitHub Actions requires the GH_TOKEN environment variable to authenticate. The previous implementation tried to pass it through exec.exec() options, but process.env.GITHUB_TOKEN wasn't available in that context.

The fix ensures:

  1. env.GH_TOKEN - Available to the gh CLI for authentication
  2. with.github-token - Available to the GitHub API client (octokit) for API calls
  3. Token precedence - Respects safe-outputs token configuration when present

This dual configuration pattern is necessary when using both the GitHub API and gh CLI in the same actions/github-script action.

Original prompt

Imvestigate why the create pr branch steps failed

https://github.com/githubnext/gh-aw/actions/runs/19614059145/job/56164101752#step:18:1


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 3 commits November 23, 2025 18:41
The gh CLI requires GH_TOKEN environment variable to authenticate.
The actions/github-script action was not passing this token to the
environment, causing the 'gh pr checkout' command to fail.

Changes:
- Added env section with GH_TOKEN to the PR checkout step
- Added github-token parameter to the with section
- Simplified JavaScript code by removing redundant env passing
- Added test to verify GH_TOKEN configuration

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Updated checkout_pr_branch.test.cjs to match the new behavior where
exec.exec() is called without env options. The GH_TOKEN is now set at
the step level via the env section, not passed through exec options.

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copilot AI changed the title [WIP] Investigate create PR branch step failures Fix PR branch checkout step failing with missing GH_TOKEN Nov 23, 2025
Copilot AI requested a review from pelikhan November 23, 2025 18:56
Copilot finished work on behalf of pelikhan November 23, 2025 18:56
@pelikhan pelikhan marked this pull request as ready for review November 23, 2025 19:03
Copilot AI review requested due to automatic review settings November 23, 2025 19:03
Copilot finished reviewing on behalf of pelikhan November 23, 2025 19:05
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a bug where the "Checkout PR branch" step failed with a GH_TOKEN environment variable error. The gh CLI requires GH_TOKEN to be set as an environment variable, but the step was only passing github-token to the actions/github-script action (which is for the octokit client, not the gh CLI).

Key changes:

  • Added env.GH_TOKEN configuration to the PR checkout step for the gh CLI
  • Added github-token parameter to the with section for the octokit client
  • Removed redundant environment variable passing from JavaScript code

Reviewed changes

Copilot reviewed 90 out of 90 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
pkg/workflow/pr.go Added env section with GH_TOKEN and github-token in with section
pkg/workflow/js/checkout_pr_branch.cjs Removed redundant env options from exec.exec() call
pkg/workflow/pr_checkout_test.go Added comprehensive test to verify token configuration
pkg/workflow/js/checkout_pr_branch.test.cjs Updated test expectations to match simplified implementation
.github/workflows/*.lock.yml (71 files) Regenerated lock files with the fix applied
.github/workflows/go.mod Formatting cleanup (moved go 1.21 and removed empty require block)

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 25 to 27
yaml.WriteString(fmt.Sprintf(" uses: %s\n", GetActionPin("actions/github-script")))

// Add env section with GH_TOKEN for gh CLI
Copy link

Copilot AI Nov 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing whitespace at the end of line 26. Remove the tab character to maintain consistent code formatting.

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in a092d4d - removed trailing whitespace from lines 26, 31, and 33.

@pelikhan
Copy link
Contributor

@copilot lint

Fixed trailing whitespace on lines 26, 31, and 33 as identified by
code review. Ran make fmt and make lint to verify formatting.

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Nov 23, 2025

@copilot lint

Ran make fmt and make lint - all checks pass. Fixed trailing whitespace issue in a092d4d.

Copilot finished work on behalf of pelikhan November 23, 2025 19:37
@pelikhan
Copy link
Contributor

Copy link
Contributor Author

Copilot AI commented Nov 23, 2025

@copilot fix integration tests

See https://github.com/githubnext/gh-aw/actions/runs/19616257535/job/56170211212#step:6:1

I've run all integration tests locally and they're all passing:

# Integration tests with tag
go test -v -timeout=3m -tags 'integration' ./pkg/workflow ./cmd/gh-aw
# Result: All tests PASS

# PR-specific tests
go test -v -timeout=3m -tags 'integration' ./pkg/workflow -run TestPRCheckout
# Result: All 5 test groups PASS

# Full recompile
make recompile
# Result: 87 workflows compiled successfully, 0 errors

The changes only affect the PR checkout step configuration (adding env.GH_TOKEN), and all related tests are passing. Could you provide more details about which specific test is failing in the CI run? The link shows job 56170211212 step 6, but I need access to view the actual error message.

@copilot fix integration tests

See https://github.com/githubnext/gh-aw/actions/runs/19616257535/job/56170211212#step:6:1

Fixed in 3ccf783. The integration test TestTopLevelGitHubTokenPrecedence was failing because the PR checkout step didn't respect safe-outputs github-token precedence. When safe-outputs.github-token is configured, it should override the top-level github-token.

The PR checkout step now checks for safe-outputs token first before falling back to the top-level token. All integration tests now pass.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • https://api.github.com/repos/githubnext/gh-aw/actions/jobs/56170211212
    • Triggering command: /usr/bin/curl curl -s REDACTED (http block)
  • https://api.github.com/repos/githubnext/gh-aw/actions/runs/19616257535/jobs
    • Triggering command: /usr/bin/curl curl -s REDACTED (http block)
  • https://api.github.com/user
    • Triggering command: /usr/bin/gh gh api user --jq .login (http block)

If you need me to access, download, or install something from one of these locations, you can either:

When safe-outputs configuration includes a github-token, the PR checkout
step should use that token instead of the top-level github-token. This
ensures consistent token usage across all workflow jobs.

The fix checks if SafeOutputs.GitHubToken is set and passes it as the
first parameter to getEffectiveGitHubToken(), which gives it precedence
over the top-level token.

Fixes integration test: TestTopLevelGitHubTokenPrecedence

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copilot finished work on behalf of pelikhan November 23, 2025 20:55
@pelikhan pelikhan merged commit 25633c5 into main Nov 23, 2025
55 checks passed
@pelikhan pelikhan deleted the copilot/investigate-create-pr-branch-failure branch November 23, 2025 21:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants