nazo6 knowledge

Github Actionsでリポジトリのコミット日時が最近の時のみ処理を実行する

作成:2023/7/1 13:20:11

更新:2023/7/1 13:21:17

マイナーな内容すぎる
具体的にはこのブログを定期的にアップデートするのに使いたい

ワークフローファイル

actions/github-scriptがめっちゃ便利だった。他でも使っていきたい。
name: Update

on: 
  workflow_dispatch:
  schedule:
    - cron: '0 */12 * * *'

jobs:
  build:
    runs-on: ubuntu-latest
    timeout-minutes: 60

    steps:
    - uses: actions/github-script@v6
      with:
        result-encoding: string
        retries: 3
        script: |
          const repo = (await github.rest.repos.get({
            owner: context.repo.owner,
            repo: context.repo.repo,
          }));
          if(Date.now() - Date.parse(repo.data.pushed_at) < 12 * 60 * 60 * 1000) {
            console.log("Recent commit found, triggering deploy")
            const res = await fetch(process.env.DEPLOY_HOOK_URL, {method:"post"});
            const json = await res.json();
            if (res.status !== 200) {
              throw new Error(json.errors);
            }
          } else {
            console.log("No recent commit found, skipping deploy")
          }
      env:
        DEPLOY_HOOK_URL: ${{ secrets.DEPLOY_HOOK_URL }}