演習 - GitHub Actions で GitHub スクリプトを使用する
このユニットでは、GitHub スクリプトを使用してワークフローを改善する方法について詳しく説明します。
プロジェクト ボードに issue を追加する
octokit/rest.js を使用してコメントを作成し、pull request を開くことだけでなく、octokit/rest.js を使用して GitHub プロジェクトを管理することもできます。 次のコード サンプルでは、誰かがリポジトリに新しい問題を追加するたびに実行されるワークフローを作成します。 これにより、プロジェクト ボードに問題が追加され、作業のトリアージが容易になります。
name: Learning GitHub Script
on:
issues:
types: [opened]
jobs:
comment:
runs-on: ubuntu-latest
steps:
- uses: actions/github-script@0.8.0
with:
github-token: {% raw %}${{secrets.GITHUB_TOKEN}}{% endraw %}
script: |
github.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: "🎉 You've created this issue comment using GitHub Script!!!"
})
github.projects.createCard({
column_id: {{columnID}},
content_id: context.payload.issue.id,
content_type: "Issue"
});
このワークフローの最初のセクションでは、前のユニットで説明した新しい問題が作成されるたびにコメントを作成します。 次のセクションでは、この issue に基づいてカードを作成し、プロジェクト ボードに追加します。
ワークフローをステップに分ける
アクションを使用する利点の 1 つは、ジョブを ステップと呼ばれるより小さな作業単位に分割できることです。 前述のワークフロー例では、ワークフローを 2 つのステップに分けることができます。
現在のワークフローを複数の手順に分割する利点の 1 つは、式を使用してロジックを適用できる点です。 ステップを使用すると、ステップの実行を許可するタイミングについてルールを作成することができ、ワークフロー実行の最適化に役立てることができます。
ワークフローの例をステップに分ける方法は次のとおりです。
- 各ステップに名前を付け、[ アクション] タブから追跡できるようにします。
- 式を使用して、ステップを実行する必要があるかどうかを判断します (省略可能)。
この例では、元のワークフロー ファイルの 2 つのタスクが個別の手順に分かれています。
name: Learning GitHub Script
on:
issues:
types: [opened]
jobs:
comment:
runs-on: ubuntu-latest
steps:
- name: Comment on new issue
uses: actions/github-script@0.8.0
with:
github-token: {% raw %}${{secrets.GITHUB_TOKEN}}{% endraw %}
script: |
github.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: "🎉 You've created this issue comment using GitHub Script!!!"
})
- name: Add issue to project board
if: contains(github.event.issue.labels.*.name, 'bug')
uses: actions/github-script@0.8.0
with:
github-token: {% raw %}${{secrets.GITHUB_TOKEN}}{% endraw %}
script: |
github.projects.createCard({
column_id: {{columnID}},
content_id: context.payload.issue.id,
content_type: "Issue"
});
各ステップには、説明的な name 要素が含まれており、[ アクション] タブから追跡するのにも役立ちます。
Add issue to project boardステップには、プロジェクト ボードに問題がifラベル付けされている場合にのみ追加する必要があることを指定するbug ステートメントも含まれています。
Node.js 環境を使用する
GitHub スクリプトでは、完全な Node.js 環境へのアクセスも許可されます。 GitHub スクリプトを使用して複雑なアクションのロジックを記述することはお勧めしませんが、それを使用して octo/rest.js API にさらに機能を追加できます。
たとえば、Node.js を使用して、問題が開かれるたびに投稿ガイドを表示できます。 Node.js ファイル システムを使用して、ファイルを読み取り、問題のコメントの本文として使用できます。 リポジトリ内のファイルにアクセスするには、ワークフロー内の最初のステップとして actions/checkout アクションを含めます。
次の例では、ワークフロー ファイルに次の変更を加えます。
-
action/checkoutアクションを追加して、次の場所にあるテンプレート化された応答ファイルを読み取る.github/ISSUE_RESPONSES/comment.md - Node.js ファイル システム モジュールを使用して、テンプレート化された応答の内容を issue コメントの本文として配置するように JavaScript を追加します
name: Learning GitHub Script
on:
issues:
types: [opened]
jobs:
comment:
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@v2
- name: Comment on new issue
uses: actions/github-script@0.8.0
with:
github-token: {% raw %}${{secrets.GITHUB_TOKEN}}{% endraw %}
script: |
const fs = require('fs')
const issueBody = fs.readFileSync(".github/ISSUE_RESPONSES/comment.md", "utf8")
github.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: issueBody
})
- name: Add issue to project board
if: contains(github.event.issue.labels.*.name, 'bug')
uses: actions/github-script@0.8.0
with:
github-token: {% raw %}${{secrets.GITHUB_TOKEN}}{% endraw %}
script: |
github.projects.createCard({
column_id: {{columnID}},
content_id: context.payload.issue.id,
content_type: "Issue"
});
GitHub スクリプトは、新しい問題に対する包括的な対応を作成するのに役立ちました。 この応答はリポジトリ内のテンプレートにも基づいているため、簡単に変更できます。 最後に、issue をプロジェクト ボードに追加するためのトリガーも含めることで、将来の作業で簡単にトリアージできるようになりました。