How do you authenticate with Github and create PRs using GitHub cli (gh) in a GitHub Action?
Asked Answered
P

1

10

In a GitHub action, I create a file with a script. Then I can use git create a branch, add the file, commit the file and push the branch to the repo. All using git.

I then want to create a PR from within my action so I'm using GH CLI. In my steps this is the PR relate part:

echo "GH CLI Auth"
gh auth login --with-token < ${{ secrets.GITHUB_TOKEN }}
echo "Create PR"
gh pr create --reviewer myhandle --title "Show Notes ${{ env.NEWFILE_PATH }}" --body "Show Notes ${{ env.NEWFILE_PATH }}"

But I'm getting an error.

GH CLI Auth
/home/runner/work/_temp/<GUID_CENSORED>.sh: line 13: ***: No such file or directory
Error: Process completed with exit code 1.

I am giving the GitHub Token write access to the code and pull-requests.

permissions:
  contents: write
  pull-requests: write

I'm stumped. Thanks for the help!

Phyliciaphylis answered 16/12, 2021 at 5:32 Comment(0)
F
10

You can use environment variable for that. Either GH_TOKEN or GITHUB_TOKEN, check docs for details.

Here is an example:

name: Create PR workflow example
on: push
jobs:
  create_pr:
    runs-on: ubuntu-latest
    steps:
      - run: gh pr create --title "The fix" --body "This fixes the issue"
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Fixity answered 16/12, 2021 at 9:49 Comment(1)
I see now, I was doing that on the step before the one I was actually using gh cli in. Once I moved that, as you pointed out it worked. Thanks!Phyliciaphylis

© 2022 - 2024 — McMap. All rights reserved.