從 Travis 移轉


以下是 .travis.yml 的範例(取自 elm 學習路徑):

sudo: false
language: node_js
node_js:
  - lts/*
script:
  - bin/fetch-configlet
  - bin/configlet lint
  - bin/build.sh

想快速把它轉換成 GitHub Actions,請依照下列步驟:

決定範本變數

變數 值
<track> elm
<image-name> ubuntu-latest
<action to setup tooling> actions/setup-node@v1
<install dependencies> npm ci(在 build.sh 內執行)
<code-extensions> .elm

透過這個搜尋找到了 setup action。 查看 Travis 的預設發行版,找到了映像檔名稱。

決定步驟

  • bin/fetch-configlet:使用 configlet.yml 工作流後,就不再需要它了
  • bin/configlet lint:使用 configlet.yml 工作流後,就不再需要它了
  • bin/build.sh:一個負責所有事情的腳本

準備「scripts」

這條學習路徑使用 bin 資料夾,所以在 bin 資料夾內建立下列檔案:

# bin/pr
bin/build.sh
# bin/pr-check
echo "No checks yet"
# bin/ci
bin/build.sh
# bin/ci-check
echo "No checks yet"

把這些建立成_各自獨立_的執行檔,之後就能進行最佳化。現在還不需要把任何東西內嵌。

填入範本

以下是 workflows/ci.yml 的 diff。

# # .github/workflows/ci.yml

  # This workflow will do a clean install of node dependencies and run tests across different versions
  #
- # Replace <track> with the track name
- # Replace <image-name> with an image to run the jobs on
- # Replace <action to setup tooling> with a github action to setup tooling on the image
- # Replace <install dependencies> with a cli command to install the dependencies
- # Replace <code-extensions> with file extensions that should trigger the workflow
- #
- # Find Github Actions to setup tooling here:
- # - https://github.com/actions/?q=setup&type=&language=
- # - https://github.com/actions/starter-workflows/tree/main/ci
- # - https://github.com/marketplace?type=actions&query=setup
- #
  # Requires scripts:
- # - scripts/ci-check
- # - scripts/ci
+ # - bin/ci-check
+ # - bin/ci

- name: <track> / main
+ name: elm / main

  on:
    push:
      branches: [main]
    workflow_dispatch:

  jobs:
    precheck:
-     runs-on: <image-name>
+     runs-on: ubuntu-latest

      steps:
        - uses: actions/checkout@v2
-       - name: Use <setup tooling>
-         uses: <action to setup tooling>
+       - name: Use Node LTS
+         uses: actions/setup-node@v1
          with:
-           # here, use the LTS/stable version of the track's tooling
-           # node-version: 12.x
+           node-version: 12.x

        - name: Install project dependencies
-         run: <install dependencies>
+         run: npm ci

-       - name: Run exercism/<track> ci pre-check (checks config, lint code) for all exercises
-         run: scripts/ci-check
+       - name: Run exercism/elm ci pre-check (checks config, lint code) for all exercises
+         run: bin/ci-check

    ci:
-     runs-on: <image-name>
+     runs-on: ubuntu-latest

      strategy:
        matrix:
-         # here, add all SUPPORTED versions only
-         # version: [10.x, 12.x, 14.x]
+         version: [10.x, 12.x, 14.x]

      steps:
        - uses: actions/checkout@v2
-       - name: Use <setup tooling> ${{ matrix.version }}
-         uses: <action to setup tooling>
+       - name: Use Node ${{ matrix.version }}
+         uses: actions/setup-node@v1
          with:
-           # below: see how to inject the version
-           # node-version: ${{ matrix.version }}
+           node-version: ${{ matrix.version }}

        - name: Install project dependencies
-         run: <install dependencies>
+         run: npm ci

-       - name: Run exercism/<track> ci (runs tests) for all exercises
-         run: scripts/ci
+       - name: Run exercism/elm ci (runs tests) for all exercises
+         run: bin/ci

workflows/pr.yml 也有相同的變更,但特別的是那段呼叫 pr-check 和 pr 腳本、並把每個變更的檔案當作引數傳入的 bash 技巧:

# # .github/workflows/pr.yml

# # ...

-       - name: Run exercism/<track> ci pre-check (stub files, config integrity) for changed exercises
+       - name: Run exercism/elm ci pre-check (stub files, config integrity) for changed exercises
          run: |
            PULL_REQUEST_URL=$(jq -r ".pull_request.url" "$GITHUB_EVENT_PATH")
            curl --url $"${PULL_REQUEST_URL}/files" --header 'authorization: Bearer ${{ secrets.GITHUB_TOKEN }}' | \
-             jq -c '.[] | select(.status == "added" or .status == "modified") | select(.filename | match("\\.(<code-extensions>|md|json)$")) | .filename' | \
+             jq -c '.[] | select(.status == "added" or .status == "modified") | select(.filename | match("\\.(elm|md|json)$")) | .filename' | \
-             xargs -r scripts/pr-check
+             xargs -r bin/pr-check

  # ...

現在應該可以運作了

這樣就足以轉換到 GitHub Actions 了,之後還能再最佳化你的腳本。

  1. 從 build.sh 移除只該執行一次的步驟,並把它們抽到 ci-check.sh 和 pre-check.sh 檔案中(提示:你可以建立 lint.sh,然後從兩個「scripts」呼叫它)
  2. 對 pr.sh 和 pr-check.sh 加入最佳化,利用傳入的引數判斷要檢查哪些檔案或練習。
  3. 加入額外的檢查
  4. 加入說明文件,解釋如何在本機執行檢查,以及每個檢查想達成什麼。