はじめに
GitHub Actionsを触りたくてActionを作ってみました。Node.js(というかTypeScript)で書いているのですが、テストカバレッジをREADMEにバッジ表示したくて調べました。
前提
公式テンプレート1を利用してリポジトリを作成します。
https://github.com/actions/typescript-action
Coveralls側でAdd repoしてください。TOKENは使わないので放っておいてよいです。
サンプル
https://github.com/oke-py/npm-audit-action
https://coveralls.io/github/oke-py/npm-audit-action
設定ファイル修正
package.json
テストにはJestを使用しています。npm test
の引数に-- --coverage
を追加するだけでカバレッジを取得できます。
diff --git a/package.json b/package.json
index ec291cb..2e0a658 100644
--- a/package.json
+++ b/package.json
@@ -11,7 +11,7 @@
"lint": "eslint src/**/*.ts",
"pack": "ncc build",
"test": "jest",
- "all": "npm run build && npm run format && npm run lint && npm run pack && npm test"
+ "all": "npm run build && npm run format && npm run lint && npm run pack && npm test -- --coverage"
},
"repository": {
"type": "git",
.github/workflows/test.yml
GitHub Actionsで実行されるテストの設定を変更します。Coveralls公式のAction1が提供されているので利用します。
diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml
index d62684f..fade74a 100644
--- a/.github/workflows/test.yml
+++ b/.github/workflows/test.yml
@@ -14,10 +14,13 @@ jobs:
- run: |
npm install
npm run all
+ - uses: coverallsapp/github-action@master
+ with:
+ github-token: ${{ secrets.GITHUB_TOKEN }}
test: # make sure the action works on a clean machine without building
runs-on: ubuntu-latest
steps:
以上です。あとはREADMEにバッジを表示するだけです(割愛)。
おわりに
Coveralls連携は簡単でした。カバレッジ上げよう(本記事が公開される頃にはマシになっているだろうか・・・)。
GitHub Actionsよいですね。楽しいです。