GitHub Actions Workflowの実行ログをGitHub CLIで確認する

2022.10.12

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

こんにちは、CX事業本部 IoT事業部の若槻です。

最近知ったのですが、GitHub ActionsのWorkflow実行のログをGitHub CLIで取得できるようです。

Job中にコンソール出力される情報を確認したい場合などに、GitHubのコンソールへのアクセスが不要なのはとても便利でしたので、方法を書き残しておきます。

やってみた

まず、必要に応じてですが、gh workflow runコマンドを実行して、Workflowを実行します。

# Workflow一覧の確認
$ gh workflow list
.github/workflows/cron.yml                 active  2949574
.github/workflows/onpush.yml               active  3363674
.github/workflows/manual.yml               active  11296175
.github/workflows/parse_config_json_2.yml  active  26613735
.github/workflows/parse_config_json_1.yml  active  26614175
.github/workflows/matrix.yml               active  27032648
.github/workflows/add-comment.yml          active  27209272
.github/workflows/assign.yml               active  27783371
.github/workflows/getAccesstoken.yml       active  28508818

# (必要な場合)Workflowの実行
$ WORKFLOW_NAME=onpush.yml
$ gh workflow run ${WORKFLOW_NAME}

gh run listコマンドを実行して、直近のWorkflow実行のID(runId)を取得します。

# 直近のWorkflow実行のURLを取得
$ runUrl=$(gh run list --workflow=${WORKFLOW_NAME} --limit 1 --json url | jq -r '.[0].url')
$ echo ${runUrl}
https://github.com/cm-rwakatsuki/devio/actions/runs/3227968964

# URLから直近のWorkflow実行のIDを取得
$ runId=$(basename ${runUrl})
$ echo ${runId}
3227968964

gh run viewコマンドを実行して、ログを確認したいジョブのID(jobId)を取得します。(残念ながらjobIdは前述のURL取得のように--jsonパラメータで取得ができなかったため、画面出力中のJOBSから手動でコピーします。)

$ gh run view ${runId}

✓ 1720-20221011 .github/workflows/onpush.yml #1721 · 3227968964
Triggered via push about 23 hours ago

JOBS
✓ update_issue_and_pull_name_title in 9s (ID 8826074797)

For more information about the job, try: gh run view --job=8826074797
View this run on GitHub: https://github.com/cm-rwakatsuki/devio/actions/runs/3227968964

gh run view --log --job=${JOB_ID}を実行すれば、対象のジョブの実行ログを出力することができます。

$ JOB_ID=8826074797
$ gh run view --log --job=${JOB_ID} > ${JOB_ID}_log.txt

# 実行ログ
$ cat ${JOB_ID}_log.txt
update_issue_and_pull_name_title        Set up job      2022-10-11T15:03:20.2242428Z Current runner version: '2.298.2'
update_issue_and_pull_name_title        Set up job      2022-10-11T15:03:20.2269753Z ##[group]Operating System
update_issue_and_pull_name_title        Set up job      2022-10-11T15:03:20.2270353Z Ubuntu
update_issue_and_pull_name_title        Set up job      2022-10-11T15:03:20.2270730Z 20.04.5
update_issue_and_pull_name_title        Set up job      2022-10-11T15:03:20.2271022Z LTS
update_issue_and_pull_name_title        Set up job      2022-10-11T15:03:20.2271324Z ##[endgroup]
update_issue_and_pull_name_title        Set up job      2022-10-11T15:03:20.2271602Z ##[group]Runner Image
update_issue_and_pull_name_title        Set up job      2022-10-11T15:03:20.2271972Z Image: ubuntu-20.04
update_issue_and_pull_name_title        Set up job      2022-10-11T15:03:20.2272330Z Version: 20221002.2
update_issue_and_pull_name_title        Set up job      2022-10-11T15:03:20.2272818Z Included Software: https://github.com/actions/runner-images/blob/ubuntu20/20221002.2/images/linux/Ubuntu2004-Readme.md
update_issue_and_pull_name_title        Set up job      2022-10-11T15:03:20.2273478Z Image Release: https://github.com/actions/runner-images/releases/tag/ubuntu20%2F20221002.2
update_issue_and_pull_name_title        Set up job      2022-10-11T15:03:20.2273933Z ##[endgroup]
update_issue_and_pull_name_title        Set up job      2022-10-11T15:03:20.2274236Z ##[group]Runner Image Provisioner
update_issue_and_pull_name_title        Set up job      2022-10-11T15:03:20.2274623Z 1.0.0.0-main-20220916-1
update_issue_and_pull_name_title        Set up job      2022-10-11T15:03:20.2274942Z ##[endgroup]
update_issue_and_pull_name_title        Set up job      2022-10-11T15:03:20.2275880Z ##[group]GITHUB_TOKEN Permissions
update_issue_and_pull_name_title        Set up job      2022-10-11T15:03:20.2276577Z Actions: write
update_issue_and_pull_name_title        Set up job      2022-10-11T15:03:20.2276853Z Checks: write
update_issue_and_pull_name_title        Set up job      2022-10-11T15:03:20.2277372Z Contents: write
update_issue_and_pull_name_title        Set up job      2022-10-11T15:03:20.2277711Z Deployments: write
update_issue_and_pull_name_title        Set up job      2022-10-11T15:03:20.2278059Z Discussions: write
update_issue_and_pull_name_title        Set up job      2022-10-11T15:03:20.2278388Z Issues: write
update_issue_and_pull_name_title        Set up job      2022-10-11T15:03:20.2278656Z Metadata: read
update_issue_and_pull_name_title        Set up job      2022-10-11T15:03:20.2278987Z Packages: write
update_issue_and_pull_name_title        Set up job      2022-10-11T15:03:20.2279303Z Pages: write
update_issue_and_pull_name_title        Set up job      2022-10-11T15:03:20.2279608Z PullRequests: write
update_issue_and_pull_name_title        Set up job      2022-10-11T15:03:20.2279982Z RepositoryProjects: write
update_issue_and_pull_name_title        Set up job      2022-10-11T15:03:20.2280357Z SecurityEvents: write
update_issue_and_pull_name_title        Set up job      2022-10-11T15:03:20.2280644Z Statuses: write
update_issue_and_pull_name_title        Set up job      2022-10-11T15:03:20.2280958Z ##[endgroup]
update_issue_and_pull_name_title        Set up job      2022-10-11T15:03:20.2284678Z Secret source: Actions
update_issue_and_pull_name_title        Set up job      2022-10-11T15:03:20.2285155Z Prepare workflow directory
update_issue_and_pull_name_title        Set up job      2022-10-11T15:03:20.3462730Z Prepare all required actions
update_issue_and_pull_name_title        Set up job      2022-10-11T15:03:20.3646822Z Getting action download info
update_issue_and_pull_name_title        Set up job      2022-10-11T15:03:20.9103897Z Download action repository 'actions/checkout@v3' (SHA:93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8)
update_issue_and_pull_name_title        Checkout        2022-10-11T15:03:21.7518245Z ##[group]Run actions/checkout@v3
update_issue_and_pull_name_title        Checkout        2022-10-11T15:03:21.7518589Z with:
update_issue_and_pull_name_title        Checkout        2022-10-11T15:03:21.7518817Z   repository: cm-rwakatsuki/devio
update_issue_and_pull_name_title        Checkout        2022-10-11T15:03:21.7519325Z   token: ***
update_issue_and_pull_name_title        Checkout        2022-10-11T15:03:21.7519537Z   ssh-strict: true
update_issue_and_pull_name_title        Checkout        2022-10-11T15:03:21.7519760Z   persist-credentials: true
update_issue_and_pull_name_title        Checkout        2022-10-11T15:03:21.7519987Z   clean: true
update_issue_and_pull_name_title        Checkout        2022-10-11T15:03:21.7520190Z   fetch-depth: 1
update_issue_and_pull_name_title        Checkout        2022-10-11T15:03:21.7520380Z   lfs: false
update_issue_and_pull_name_title        Checkout        2022-10-11T15:03:21.7520580Z   submodules: false
update_issue_and_pull_name_title        Checkout        2022-10-11T15:03:21.7520808Z   set-safe-directory: true
update_issue_and_pull_name_title        Checkout        2022-10-11T15:03:21.7521014Z env:
update_issue_and_pull_name_title        Checkout        2022-10-11T15:03:21.7521316Z   GITHUB_TOKEN: ***
update_issue_and_pull_name_title        Checkout        2022-10-11T15:03:21.7521561Z ##[endgroup]
update_issue_and_pull_name_title        Checkout        2022-10-11T15:03:22.1215633Z Syncing repository: cm-rwakatsuki/devio
...

おわりに

GitHub Actions Workflowの実行ログをGitHub CLIで確認する方法でした。

私の場合は主に下記で実装したWorkflow実行からアクセストークンを簡単に取得するのに役に立ちました。

以上