練習 - 在 GitHub Actions 中使用 GitHub 指令碼

已完成

在本單元中,您將深入瞭解如何使用 GitHub 腳本來改善工作流程。

將問題新增至專案版面

除了使用 octokit/rest.js 建立註解並開啟提取要求外,您也可以使用 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"
            });

每當建立新問題時,此工作流程的第一個區段就會建立批注,而上一個單元會涵蓋此問題。 下一節會根據此問題建立卡片,並將卡片新增至專案版面。

將工作流程分成步驟

使用動作的其中一個優點是,您可以將作業分成較小的工作單位,稱為 步驟。 在上述的範例工作流程中,您可以將工作流程分成兩個步驟。

將目前的工作流程分成多個步驟的其中一個優點是,它可讓您使用表達式來套用邏輯。 使用步驟可讓您建立允許執行步驟時的規則,並有助於最佳化您的工作流程執行。

若要將範例工作流程分成步驟:

  • 為每個步驟命名,以便您可以從 [ 動作] 索引標籤追蹤它。
  • 使用表達式來判斷步驟是否應該執行 (選擇性)。

在此範例中,原始工作流程檔案中的兩個工作會分成個別步驟:

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的樣板化響應檔案。
  • 新增 JavaScript 以使用 Node.js 檔案系統模組,將範本回應的內容放置為問題註解的正文
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 腳本可協助您建立對新問題的完整回應。 此回應也以存放庫中的範本為基礎,因此很容易變更。 最後,您也包含可將問題新增至專案版面的觸發程式,以便輕鬆地將問題分級以供未來工作使用。