練習 - 在 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_COMMENT/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 指令碼可協助您針對開啟的新問題,建立完整的回應。 此回應也是以存放庫中的範本為基礎,因此在未來也很輕易變更。 最後,您也包含可將問題新增至專案版面的觸發程式,以便輕鬆地將問題分級以供未來工作使用。