Search code examples
github-actionsgithub-cli

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


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!


Solution

  • 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 }}