Github Action - Can't use gh-cli in self-runner in Enterprise

Here are the tips to run a self-runner with gh-cli

  1. Install gh
  2. Use PAT with permission below to operate your org/repo
    1. repo
    2. workflow
    3. admin:org
  3. Need to run gh auth login
  4. Now you can start to use gh-cli in your self-runner

Make sure you:

  • Add secrets.MY_PAT in org/repo settings
  • --hostname git.mycompany.com is your domain
name: Weekly Report

on:
workflow_dispatch:

schedule:
- cron: '0 4 * * 4' # 12:00 UTC+8, Friday

jobs:
report:
runs-on: [self-hosted]
steps:
- uses: devops/gh_actions_checkout@v3
## TODO: remove this after runner image installed it.
- name: Install gh-cli
run: |
curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg \
&& sudo chmod go+r /usr/share/keyrings/githubcli-archive-keyring.gpg \
&& echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null \
&& sudo apt update \
&& sudo apt install gh -y

- name: gh auth
run: |
echo ${{ secrets.MY_PAT }} | gh auth login --with-token --hostname git.mycompany.com --git-protocol https

- name: Search - Logging
run: |
gh issue list

Here are every type of error if you set it wrong or miss some steps

HTTP 401: This endpoint requires you to be authenticated. (https://git.mycompany.com/api/graphql)
Try authenticating with: gh auth login
Error: Process completed with exit code 1.
could not determine base repo: fatal: not a git repository (or any parent up to mount point /)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).
/usr/bin/git: exit status 128
Error: Process completed with exit code 1.
/runner/_work/_temp/4e6ad363-9482-4dc1-8edd-a3ff80577dae.sh: line 1: gh: command not found
Error: Process completed with exit code 127.
HTTP 401: This endpoint requires you to be authenticated. (https://git.mycompany.com/api/graphql)
Try authenticating with: gh auth login
Error: Process completed with exit code 1.
The value of the GH_TOKEN environment variable is being used for authentication.
error fetching organization projects: Message: Resource not accessible by integration, Locations: [{Line:1 Column:92}]
Error: Process completed with exit code 1.