octokit.js を使って action をキャンセルする
octokit.js の octokit.rest.actions.cancelWorkflowRun で action 自体をキャンセルできる。
キャンセルには run_id が必要なので、github.run_id を渡す。実行に permissions.actions が必要だった。
permissions:
  actions: write
steps:
  # ... 中略 ...
  - name: cancel workflow
    uses: actions/github-script@v7
    with:
      script: |
        await github.rest.actions.cancelWorkflowRun({
          owner: context.repo.owner,
          repo: context.repo.repo,
          run_id: ${{ github.run_id }}
        });        
github-script では、octokit client が github に設定されているので、github.rest... として API を呼び出せる。github 等の arguments の説明は以下のリンクを参照。
https://github.com/actions/github-script?tab=readme-ov-file#actionsgithub-script
octokit.rest.actions.forceCancelWorkflowRun の API ドキュメント https://octokit.github.io/rest.js/v21/#actions-cancel-workflow-run
action 自体をキャンセルする弊害
action 自体をキャンセルしてしまうと、「キャンセルした」という通知が GitHub から飛んでしまう。 そのため、日常的に使うにはちょっとしんどい。
結果的に今回はキャンセルするのは諦めて、条件で step を実行しないようにする判断をした。