檢查工作流程檔案
GitHub Actions 使用 YAML 語法來定義工作流程。 每個工作流程都會儲存為程式代碼存放庫中的個別 YAML 檔案,並儲存在名為 .github/workflows的目錄中。 每當程式代碼推送至存放庫時,就會觸發下列工作流程範例。 工作流程會執行下列步驟:
- 簽出推送的程式碼。
- 安裝 Node.js。
- 安裝Bash自動化測試系統 (Bats) 測試架構。
- 執行命令以輸出 Bats 版本:
bats -v。
name: learn-github-actions
run-name: ${{ github.actor }} is learning GitHub Actions
on: [push]
jobs:
check-bats-version:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '20'
- run: npm install -g bats
- run: bats -v
瞭解工作流程檔案
為了協助您瞭解 YAML 語法如何用來建立工作流程檔案,本節說明上一個範例的每一行:
# Optional - The name of the workflow as it will appear in the "Actions" tab of the GitHub repository. If this field is omitted, the name of the workflow file will be used instead.
name: learn-github-actions
# Optional - The name for workflow runs generated that appear in the list of workflow runs on your repository's "Actions" tab. This example uses an expression with the 'github' context to display the username of the actor that triggered the workflow run.
run-name: ${{ github.actor }} is learning GitHub Actions
# Specifies the trigger for this workflow. This example uses the 'push' event, so a workflow run is triggered every time someone pushes a change to the repository or merges a pull request.
on: [push]
# Groups together all the jobs that run in the 'learn-github-actions' workflow.
jobs:
# Defines a job named 'check-bats-version'. The child keys will define properties of the job.
check-bats-version:
# Configures the job to run on the latest version of an Ubuntu Linux runner. This means that the job will execute on a fresh virtual machine hosted by GitHub.
runs-on: ubuntu-latest
# Groups together all the steps that run in the 'check-bats-version' job. Each item nested under this section is a separate action or shell script.
steps:
# The 'uses' keyword specifies that this step will run 'v4' of the 'actions/checkout' action. This is an action that checks out your repository onto the runner, allowing you to run scripts or other actions against your code (such as build and test tools). You should use the checkout action any time your workflow will use the repository's code.
- uses: actions/checkout@v4
# This step uses the 'actions/setup-node@v4' action to install the specified version of the Node.js. (This example uses version 20.) This puts both the 'node' and 'npm' commands in your PATH.
- uses: actions/setup-node@v4
with:
node-version: '20'
# The 'run' keyword tells the job to execute a command on the runner. In this case, you are using 'npm' to install the 'bats' software testing package.
- run: npm install -g bats
# The 'bats' command with a parameter that outputs the software version.
- run: bats -v
檢視工作流程執行的活動
當工作流程被觸發時,會建立一個工作流程運行來執行該工作流程。 工作流程執行開始之後,您可以看到執行進度的可視化圖表,並在 GitHub 上檢視每個步驟的活動。 請遵循下列步驟來檢視活動:
流覽至存放庫的主頁面。
選取存放庫名稱底下的 [動作]。
在左側提要欄位中,選取您想要查看的工作流程。
從工作流程執行清單中,選取執行的名稱以查看工作流程執行摘要。
在左側提要欄位或視覺效果圖形中,選取您想要查看的工作。
選取步驟以檢視詳細結果。
現在您已瞭解工作流程檔案元件,您可以了解開發人員如何針對各種使用案例自定義檔案。