特定のリポジトリでREADMEを更新した場合のみ、github actionでTable of contentsを更新する。
Table of contentsについて
Table of contents
とはいわゆる目次のことであり、Github actionsでは以下のtoc-generator
がマーケットで提供されている。
https://github.com/marketplace/actions/toc-generator
以下のように使用する。
name: Update Markown TOC
on:
push:
paths:
- 'README.md'
jobs:
UpdateTOC:
name: UpdateTOC
runs-on: ubuntu-20.04
steps:
- name: TOC Generator
uses: technote-space/toc-generator@v3
with:
# 目次を更新するファイル
TARGET_PATHS: README.md
# 目次を作成する最大階層レベル
MAX_HEADER_LEVEL: 4
GITHUB_TOKEN: ${{ secrets.SECRETS_UPDATE_TOC_ACCESS_TOKEN }}
# 目次の見出し
TOC_TITLE: '**Table of Contents**'
これを.github/workflow/xxx.yaml
のような形で置いておくと、README.md更新時に目次が自動的に作られる。
(${{ secrets.SECRETS_UPDATE_TOC_ACCESS_TOKEN }}
にはSECRETSを格納しておく)
特定のリポジトリでTOCを更新したい場合
github actionsから以下の変数を使う
${GITHUB_REPOSITORY}
#owner/repo
${{ github.repository }}
#owner/repo
${GITHUB_REPOSITORY_OWNER}
#owner
${{ github.repository_owner }}
#owner
例えば以下のようにすると、リポジトリのオーナー名を取得できる(例: repository_owner:seigot)
steps:
- name: test step
run:
echo "repository_owner:${GITHUB_REPOSITORY_OWNER}"
echo "repository_owner:${{ github.repository_owner }}"
if: github.repository_owner == 'seigot'
のようにすると、特定のリポジトリ/オーナーでREADMEを更新した場合のみ、github actionでTable of contentsを更新することができる。
name: Update Markown TOC
on:
push:
paths:
- 'README.md'
jobs:
UpdateTOC:
name: UpdateTOC
runs-on: ubuntu-20.04
steps:
- name: TOC Generator
uses: technote-space/toc-generator@v3
if: github.repository_owner == 'seigot'
with:
# 目次を更新するファイル
TARGET_PATHS: README.md
# 目次を作成する最大階層レベル
MAX_HEADER_LEVEL: 4
GITHUB_TOKEN: ${{ secrets.SECRETS_UPDATE_TOC_ACCESS_TOKEN }}
# 目次の見出し
TOC_TITLE: '**Table of Contents**'
参考