Outsider's Dev Story

Stay Hungry. Stay Foolish. Don't Be Satisfied.
RetroTech 팟캐스트 44BITS 팟캐스트

GitHub CLI gh의 새 기능 살펴보기

작년 9월에 GitHub에서 공식으로 만든 CLI인 gh 1.0이 나와서 소개했는데 1년 사이에 버전이 많이 올라가서 현재 최신 버전은 1.13.1이고 새로운 기능도 많이 추가되었다.

그동안 gh를 쓰면서도 쓰던 기능만 쓰곤 해서 새로운 기능도 파악할 겸 정리를 해보았다. 기본 기능은 이전 글에서 확인할 수 있고 전체 명령어와 사용법은 공식 문서에서 볼 수 있다.

gh issue, gh pr

gh issue viewgh pr view를 사용하면 터미널에서 이슈나 풀리퀘스트에 내용을 볼 수 있는데 최신 댓글도 보여주도록 개선되었다.

$ gh issue view 5
set up server #5
Open • outsideris opened about 5 minutes ago • 2 comments
Assignees: outsideris


  test issure for server


———————— Not showing 1 comment ————————


outsideris (Owner) • 4m • Newest comment

  +1


Use --comments to view the full conversation
View this issue on GitHub: https://github.com/outsideris/gh-example/issues/5

전체 댓글을 보고 싶으면 --comments 옵션을 사용하면 된다.

$ gh issue view 5 --comments
set up server #5
Open • outsideris opened about 5 minutes ago • 2 comments
Assignees: outsideris


  test issure for server


outsideris (Owner) • 5m

  LGTM

outsideris (Owner) • 4m • Newest comment

  +1


View this issue on GitHub: https://github.com/outsideris/gh-example/issues/5

gh issue comment, gh pr comment

gh issue commentgh pr comment를 사용해서 댓글도 남길 수 있다.

$ gh issue comment 5 --body "comment from cli"
https://github.com/outsideris/gh-example/issues/5#issuecomment-890336068

gh pr merge

gh pr merge로 Pull Reqeust를 머지할 수도 있는데 여기에 --delete-branch 플래그(-d)를 사용하면 머지한 뒤에 자동으로 해당 Pull Request에 사용된 로컬 브랜치를 삭제해 준다.

$ gh pr merge 7 -d
? What merge method would you like to use? Create a merge commit
? What's next? Submit
✓ Merged pull request #7 (teest)
✓ Deleted branch pr-branch and switched to branch master


gh secret

gh secret 명령어로 저장소의 Secret을 관리할 수 있게 되었다.

AWS_REGION이라는 시크릿을 설정하고 싶다면 gh secret set AWS_REGION을 실행하면 시크릿값을 입력하고 바로 저장할 수 있다.

$ gh secret set AWS_REGION
? Paste your secret **************

✓ Set secret AWS_REGION for outsideris/gh-example

프롬프트를 사용하지 않고 한 번에 저장하고 싶다면 gh secret set AWS_REGION -b ap-northeast-2처럼 사용할 수 있다.

저장된 시크릿은 gh secret list로 확인할 수 있다.

$ gh secret list
AWS_REGION  Updated 2021-07-31


gh gist

gh gist를 이용하면 GitHub Gist도 관리할 수 있다. gh gist list로 자신의 gist도 볼 수 있는데 사실 gist도 Git 저장소이므로 클론도 할 수 있다.

$ gh gist clone d3c77b36cbe607d3098231a1ba7b5189
'd3c77b36cbe607d3098231a1ba7b5189'에 복제합니다...
The authenticity of host 'gist.github.com (52.78.231.108)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'gist.github.com' (RSA) to the list of known hosts.
remote: Enumerating objects: 9, done.
remote: Total 9 (delta 0), reused 0 (delta 0), pack-reused 9
오브젝트를 받는 중: 100% (9/9), 완료.
델타를 알아내는 중: 100% (2/2), 완료.

gh gist clone은 해시로 된 ID나 gist URL을 지정해서 가져올 수 있는데 해당 ID로 된 폴더가 생기고(git clone이랑 같다고 생각하면 된다) 로컬에 gist의 git 저장소가 클론된다.

$ tree d3c77b36cbe607d3098231a1ba7b5189
d3c77b36cbe607d3098231a1ba7b5189
└── mocha_contributhon_2020.md

0 directories, 1 file


gh ssh-key

gh ssh-key를 사용하면 GitHub의 SSH 키를 CLI에서 보고 추가할 수 있다.

$ gh ssh-key list
your-email@example.com  ssh-rsa AAAAB3N222zaC...SkcfuL879Dc7tHQ==  Mar  4, 2014

gh ssh-key add로 로컬의 SSH 키를 GitHub에 등록할 수도 있다.

gh에서 사용하는 키에 SSH 키에 대한 권한이 없는 경우 다음과 같이 오류가 나오므로 gh auth refresh -s read:public_key 처럼 권한을 갱신해주면 된다. 이른 다른 기능을 사용할 때도(org의 권한 등) 추가 기능이 필요할 때도 gh auth refresh 명령어를 사용해서 업데이트해주면 된다.

$ gh ssh-key list
Error: insufficient OAuth scopes to list SSH keys
Run the following to grant scopes: gh auth refresh -s read:public_key


gh api

gh api를 사용하면 GitHub REST API를 쉽게 사용할 수 있다.

$ gh api users/outsideris/repos
[
  {
    "id": 1068311,
    "node_id": "MDEwOlJlcG9zaXRvcnkxMDY4MzEx",
    "name": ".vim",
    "full_name": "outsideris/.vim",
    "private": false,
    "owner": {
      "login": "outsideris",
    }
  }
  ... 생략
]

REST API의 경로만 지정하면 JSON 응답이 오기 때문에 GitHub API를 테스트할 때 유용하게 사용할 수 있다.

$ gh api users/outsideris/repos --jq '.[].name'
.vim
2captcha-example
AngularJS-Atom
...

jq를 지원하기 때문에 --jq 옵션을 사용하면 쉽게 응답을 조작해서 원하는 결과를 볼 수 있다. 이는 API를 이용해서 자동화 작업을 할 때도 유용할 것 같다.

GitHub Actions

GitHub Actions는 gh workflowgh run을 사용할 수 있다.

$ gh workflow list
Continuous integration  active  959991
Deployment              active  11759455

gh workflow view 명령어로 워크플로우의 상태를 확인할 수 있다. 여기서 파라미터는 워크플로우의 이름이나 ID를 지정할 수 있고 YAML 파일명도 사용할 수 있다.

$ gh workflow view 'Continuous integration'
Continuous integration - ci.yml
ID: 959991

Total runs 11
Recent runs
X  Update README.md                      Continuous integration  outsideris-patch-1                    pull_request  1084935566
X  Bump hosted-git-info from 2.8.8 t...  Continuous integration  dependabot/npm_and_yarn/hosted-gi...  pull_request  832247428
X  Bump lodash from 4.17.15 to 4.17.21   Continuous integration  dependabot/npm_and_yarn/lodash-4....  pull_request  831166611

To see more runs for this workflow, try: gh run list --workflow ci.yml
To see the YAML for this workflow, try: gh workflow view ci.yml --yaml

gh workflow run으로 워크플로우를 실행할 수도 있는데 이때는 workflow_dispatch 이벤트를 사용하기 때문에 워크플로우가 workflow_dispatch를 이용해서 수동 트리거가 설정된 경우만 사용할 수 있다.

GitHub Actions의 실행과 관련해서는 gh run 명령어를 사용하면 더 자세히 볼 수 있다. 아직 많이 안 써봐서 그런지 굳이 명령어가 workflow와 나누어져 있는 이유는 잘 모르겠다. gh run list를 실행하면 액션의 실행 내역을 확인할 수 있다.

$ gh run list
STATUS  NAME                    WORKFLOW                BRANCH                  EVENT         ID          ELAPSED  AGE
✓       Update README.md        Deployment              outsideris-patch-1      push          1085081798  1m23s    1m
-       Merge pull request ...  Deployment              master                  push          1085026470  22s      34m
X       teest                   Continuous integration  pr-branch               pull_request  1085026002  21s      34m
X       Update README.md        Continuous integration  outsideris-patch-1      pull_request  1084935566  1m19s    1h
-       Update README.md        Deployment              outsideris-patch-1      push          1084935325  28s      1h
X       add README              Deployment              master                  push          1081942466  2m1s     1d
X       update deploy workflow  Deployment              master                  push          1081747847  28s      1d
X       Bump hosted-git-inf...  Continuous integration  dependabot/npm_and_...  pull_request  832247428   33s      May 11, 2021
X       Bump lodash from 4....  Continuous integration  dependabot/npm_and_...  pull_request  175216196   24s      Jul 20, 2020
X       fix yaml                .github/workflows/c...  master                  push          71880559    0s       Apr  6, 2020

For details on a run, try: gh run view <run-id>

실패한 워크플로우는 gh run rerun 명령어로 다시 실행할 수 있다.

$ gh run rerun 1085026002
✓ Requested rerun of run 1085026002

gh run watch 명령어를 실행하면 다음과 같이 실행 중인 워크플로우 중에 선택한 뒤

gh run watch 실행 뒤 동작 중인 워크플로우 선택

실행 중인 잡의 상태를 CLI에서 실시간으로 확인할 수 있다.

gh run watch로 실행 중인 워크플로우의 상태 실시간 확인

워크플로우에서 아티팩트를 저장하는 경우 gh run download를 사용하면 아티팩트를 다운로드받을 수 있다. 아티팩트를 이용해서 디버깅하거나 결과를 확인할 때 터미널에서 바로 사용할 수 있는 건 유용할 것 같다.

gh alias

gh alias로 별칭을 만들어서 자주 쓰는 명령어를 더 적은 명령어로 사용할 수 있다.

앞에서 사용한 gh run watchrw로 축약해서 쓰고 싶다면gh alias set rw 'run watch'`처럼 하면 된다.

$ gh alias set rw 'run watch'
- Adding alias for rw: run watch
✓ Added alias.

등록된 별칭은 gh alias list로 확인할 수 있고 이제 gh wr를 실행하면 gh run watch가 실행된다.

$ gh alias list
prco:  pr checkout
rw:    run watch


gh browse

gh browse는 이슈나 파일, 메뉴 등을 브라우저에서 바로 열 수 있는 기능이다. 브라우저와 터미널을 오가면서 작업할 때 편리한 기능이다.

$ gh browse 5
now opening https://github.com/outsideris/gh-example/issues/5 in browser

번호를 입력하면 이슈나 풀리퀘스트를 바로 기본 웹브라우저로 열어준다.

$ gh browse --settings
now opening https://github.com/outsideris/gh-example/settings in browser

--settings 플래그를 사용하면 해당 저장소의 세팅 메뉴로 바로 이동할 수 있다. --wiki, --projects를 이용해서 저장소의 위키나 프로젝트로 이동할 수도 있고 --branch로 특정 브랜치를 선택할 수도 있다.

$ gh browse src/sum.js:3
now opening https://github.com/outsideris/gh-example/tree/master/src/sum.js#L3 in browser

src/sum.js:3처럼 저장소의 파일과 라인 번호를 입력해도 해당 파일을 브라우저에서 열어준다.

2021/07/31 22:11 2021/07/31 22:11