Kanji
・クラウドエンジニア / フリーランス ・1993年生まれ ・愛媛県出身 / 東京都渋谷区在住 ・AWS歴5年 プロフィールの詳細
gh auth login
目次
GH CLI multi-account switch にある GitHub のエイリアス機能を利用して切り替える方法がシンプルだったため、少しカスタマイズして実装してみました。 参考にしたサイトではアクセスする GitHub アカウント毎に ~/.config/gh/hosts.yml.${プロファイル名} を ~/.config/gh/config.yml へ追記しなければなりませんでした。
~/.config/gh/hosts.yml.${プロファイル名}
~/.config/gh/config.yml
以下の方法では、プロファイルの作成と切り替えをエイリアス化することで、初期設定時以外は ~/.config/gh/config.yml ファイルを直接変更しなくても良いやり方で実装しています。 ただし、この方法ではあくまでも GitHub CLI の認証が変わるのみで、Git の認証はなぜか切り替わりません。 Git 側では以下のコマンドでクレデンシャルヘルパーを設定すれば、GitHub CLI でアカウントを切り替えなくても問題ございません。 (2023/02/21 追記) 以下の設定を入れても GitHub CLI の認証を変更しないとレポジトリへアクセスできませんでした。
git config --global 'credential.https://github.com.helper' '' git config --global --add 'credential.https://github.com.helper' '!gh auth git-credential'
初期設定として、 ~/.config/gh/config.yml ファイルを vi エディタなどで開き、 aliases キーに create-profile と switch-profile のエイリアスを追加しています。エイリアス名や処理は好みで書き換えても OK です。 - create-profile では、 gh auth status コマンドで現在の認証状態を確認し、プロファイルを作成するかどうかを対話形式で確認しています。処理としては、 ~/.config/gh/hosts.yml ファイルを ~/.config/gh/hosts.yml.${プロファイル名} へコピーしています。 - switch-profile では、 ~/.config/gh/hosts.yml.${プロファイル名} ファイルが存在するかどうかを確認し、存在する場合は ~/.config/gh/hosts.yml ファイルへコピーしています。 - delete-profile では、プロファイルを削除するかどうかを対話形式で確認し、削除する場合は ~/.config/gh/hosts.yml.${プロファイル名} ファイルを削除しています。 - list-profiles では、現在のプロファイル一覧を表示するようにしています。
aliases
create-profile
switch-profile
gh auth status
~/.config/gh/hosts.yml
delete-profile
list-profiles
git_protocol: https aliases: create-profile: '![ -z "$1" ] && echo "[ERROR] Please specify the profile name." && exit 1; gh auth status && read -p "Do you create this profile? (y/N): " yn; case "$yn" in [yY]*) cp ~/.config/gh/hosts.yml ~/.config/gh/hosts.yml.$1 && echo "New hosts file created: ~/.config/gh/hosts.yml.$1";; esac' switch-profile: '![ -z "$1" ] && echo "[ERROR] Please specify the profile name." && exit 1; if [ -e ~/.config/gh/hosts.yml.$1 ]; then cp ~/.config/gh/hosts.yml.$1 ~/.config/gh/hosts.yml && gh auth status && echo "[INFO] Hosts file switched"; else echo "[ERROR] Hosts file not found: ~/.config/gh/hosts.yml.$1"; fi' delete-profile: '![ -z "$1" ] && echo "[ERROR] Please specify the profile name." && exit 1; read -p "Do you delete this profile? (y/N): " yn; case "$yn" in [yY]*) rm -f ~/.config/gh/hosts.yml.$1 && echo "Hosts file deleted: ~/.config/gh/hosts.yml.$1";; esac' list-profiles: '!ls ~/.config/gh/hosts.yml.* | sed -e "s|^.*hosts.yml.||" | sed -e "s|^| - |"'
# プロファイル作成コマンド yq -o yaml -i '.aliases += { "create-profile": "''''![ -z \"$1\" ] && echo \"[ERROR] Please specify the profile name.\" && exit 1; gh auth status && read -p \"Do you create this profile? (y/N): \" yn; case \"$yn\" in [yY]*) cp ~/.config/gh/hosts.yml ~/.config/gh/hosts.yml.$1 && echo \"New hosts file created: ~/.config/gh/hosts.yml.$1\";; esac''''" } ' ~/.config/gh/config.yml # プロファイル切り替えコマンド yq -o yaml -i '.aliases += { "switch-profile": "''''![ -z \"$1\" ] && echo \"[ERROR] Please specify the profile name.\" && exit 1; if [ -e ~/.config/gh/hosts.yml.$1 ]; then cp ~/.config/gh/hosts.yml.$1 ~/.config/gh/hosts.yml && gh auth status && echo \"Hosts file switched\"; else echo \"[ERROR] Hosts file not found: ~/.config/gh/hosts.yml.$1\"; fi''''" }' ~/.config/gh/config.yml # プロファイル削除コマンド yq -o yaml -i '.aliases += { "delete-profile": "![ -z \"$1\" ] && echo \"[ERROR] Please specify the profile name.\" && exit 1; read -p \"Do you delete this profile? (y/N): \" yn; case \"$yn\" in [yY]*) rm -f ~/.config/gh/hosts.yml.$1 && echo \"Hosts file deleted: ~/.config/gh/hosts.yml.$1\";; esac" }' ~/.config/gh/config.yml # プロファイルリストコマンド yq -o yaml -i '.aliases += { "list-profiles": "!ls ~/.config/gh/hosts.yml.* | sed -e \"s|^.*hosts.yml.||\" | sed -e \"s|^| - |\"" }' ~/.config/gh/config.yml
gh auth login コマンドで GitHub アカウントへログインした後、プロファイルを作成したい場合は以下のコマンドを実行します。
gh create-profile ${プロファイル名}
以下は実行例です。誤って上書きしてプロファイルを作成しないよう、対話形式で作成有無を確認するようにしています。 y を入力して Enter を押下すればプロファイルとしてファイルが生成されます。
y
github.com ✓ Logged in to github.com as xxxxxxxxxx (oauth_token) ✓ Git operations for github.com configured to use https protocol. ✓ Token: ******************* Do you create this profile? (y/N): y New hosts file created: ~/.config/gh/hosts.yml.xxxx
GitHub アカウントの切り替えを行いたい場合には以下のコマンドを実行します。
gh switch-profile ${プロファイル名}
以下は実行例です。エイリアスのコマンドは単純な処理にしているため、プロファイルのファイルが存在するかしないかのみ判断して処理を行なってます。
# プロファイルが存在する場合 github.com ✓ Logged in to github.com as xxxxxxxxxx (oauth_token) ✓ Git operations for github.com configured to use https protocol. ✓ Token: *******************c [INFO] Hosts file switched # プロファイルが存在しない場合 [ERROR] Hosts file not found: ~/.config/gh/hosts.yml.test # プロファイルが存在するが、認証が失敗した場合 ※注意:再ログイン後にプロファイルを再作成する必要があります github.com X Failed to log in to github.com account ${Git Hub Account Name} $(/Users/user01/.config/gh/hosts.yml) - Active account: true - The token in /Users/user01/.config/gh/hosts.yml is invalid. - To re-authenticate, run: gh auth login -h github.com - To forget about this account, run: gh auth logout -h github.com -u ${Git Hub Account Name}
プロファイルを削除したい場合は以下のコマンドを実行します。
gh delete-profile ${プロファイル名}
以下は実行例です。プロファイルの削除を確認するため、対話形式で削除有無を確認するようにしています。 y を入力して Enter を押下すればプロファイルが削除されます。 これを実行しても現在のセッションがログアウトされる訳ではありません。
Do you delete this profile? (y/N): y Hosts file deleted: ~/.config/gh/hosts.yml.xxxx
プロファイルの一覧を表示したい場合は以下のコマンドを実行します。
gh list-profiles
以下は実行例です。プロファイル名のみを表示するようにしています。
- xxxx - yyyy - zzzz
僕は 1Password ではなく Bitwarden を利用しているため試していませんが、1Password でも GitHub アカウントの切り替えが可能なようです。