Github認証まとめ


Githubなどで認証情報エラーにつまづく場合

Terminal
remote: Invalid username or password.
fatal: Authentication failed for 'https://github.com/webmaster-patche/test_cicd_pilot.git/'
  • この様なエラーが出ると萎えますよね
  • この手のエラーは、そもそもユーザーのsshのキー登録を済ませていない場合や、PCユーザーとgitのpush権限のあるユーザーが異なっていたりするケースで発生します。

Secretを登録しておくパターン

  • sshキー登録を済ませていない場合の手法で、PCユーザーとgitのpush権限のあるユーザーが同じ場合にこのパターンが有効です。(異なる場合、このパターンでは認証エラーが起きます)
Terminal
webmaster-patche-no-MacBook-Pro:~ webmaster-patche$ ssh-keygen -t rsa -b 4096 -C "%mail_address%"
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/webmaster-patche/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /Users/webmaster-patche/.ssh/id_rsa.
Your public key has been saved in /Users/webmaster-patche/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:ku2lqNzzS5+******************************** 
%mail_address%
The key's randomart image is:
+---[RSA 4096]----+
|  ..oo==+=o.o. .o|
|   + .==+ ..  o.*|
|    o oo .    E=+|
|     o o       ..|
|      = S .      |
|     o + o       |
|      o + .      |
|   . o.o o +     |
|    o .=*o*o.    |
+----[SHA256]-----+
webmaster-patche-no-MacBook-Pro:~ webmaster-patche$ pbcopy < ~/.ssh/id_rsa.pub


Settings > Secretsを開きます


New secretボタンを押下し、pbcopyしたキー情報を登録します


Nameには分かりやすい規則を、Valueにはpbcopyキー情報を入力します


これで設定完了です

認証情報をgit/configに登録しておくパターン

Terminal
webmaster-patche-no-MacBook-Pro:capistrano_sample_app_v1 webmaster-patche$ cat .git/config 
[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
    ignorecase = true
    precomposeunicode = true
[remote "origin"]
    url = https://github.com/webmaster-patche/capistrano_sample_app_v1.git
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
    remote = origin
    merge = refs/heads/master

webmaster-patche-no-MacBook-Pro:smocca_v3_ webmaster-patche$ vi .git/config 
[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
    ignorecase = true
    precomposeunicode = true
[remote "origin"]
    url = https://%user_name%:%password%@github.com/webmaster-patche/capistrano_sample_app_v1.git
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
    remote = origin
    merge = refs/heads/master

なお、[user]情報が無く、追加したい場合は以下のコマンドを実行します

Terminal
webmaster-patche-no-MacBook-Pro:capistrano_sample_app_v1 webmaster-patche$ git config --local user.name webmaster-patche
webmaster-patche-no-MacBook-Pro:capistrano_sample_app_v1 webmaster-patche$ git config --local user.email [email protected]

webmaster-patche-no-MacBook-Pro:capistrano_sample_app_v1 webmaster-patche$ cat .git/config 
[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
    ignorecase = true
    precomposeunicode = true
[remote "origin"]
    url = https://%user_name%:%password%@github.com/webmaster-patche/capistrano_sample_app_v1.git
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
    remote = origin
    merge = refs/heads/master
[user]
    name = webmaster-patche
    email = [email protected]

番外編:authorを変える

  • 認証情報をgit/configに登録しておくパターンの場合、userとauthorが一緒になるとは限りません。
  • この場合、authorを指定することで解消できます
Terminal
webmaster-patche-no-MacBook-Pro:hoge_repository webmaster-patche$ git add .
webmaster-patche-no-MacBook-Pro:hoge_repository webmaster-patche$ git commit --amend --author="webmaster-patche <[email protected]>"
[feature/development_yyyyMMdd 594e75b20] 修正
 Date: Tue Jul 15 13:24:20 2020 +0900
 2 files changed, 2 insertions(+), 2 deletions(-)
webmaster-patche-no-MacBook-Pro:hoge_repository webmaster-patche$ git push -f origin feature/development_yyyyMMdd
Enumerating objects: 15, done.
Counting objects: 100% (15/15), done.
Delta compression using up to 8 threads
Compressing objects: 100% (8/8), done.
Writing objects: 100% (8/8), 634 bytes | 634.00 KiB/s, done.
Total 8 (delta 7), reused 0 (delta 0)
remote: Resolving deltas: 100% (7/7), completed with 7 local objects.
To https://github.com/webmaster-patche/hoge_repository.git
 + 1332gl234...521e75b10 feature/development_yyyyMMdd -> feature/development_yyyyMMdd (forced update)

これでlogを確認してみてください。