Skip to content
Go back

Github Actionsから別のリポジトリにコミットする

Published:  at  12:00 AM

あるGithubレポジトリのGithub Actionsから、別のGithubレポジトリに変更をコミットする方法について。

ワークフローの流れとしては、以下になる:

サンプルのワークフロー定義は以下。 Push先のレポジトリ名はyyyとする:

name: Update antoher repo

on:
  workflow_dispatch:
  schedule:
    - cron: "49 0 */2 * *" # once two days for example

jobs:
  update-another-repo:
    runs-on: ubuntu-latest
    steps:
      # may be some scripts here
      - name: checkout yyy repo
        uses: actions/checkout@v4
        with:
          repository: "xxx/yyy"
          ref: main
          path: "yyy"
          token: "${{ secrets.PAT_WRITE_YYY }}"
          persist-credentials: true

      - name: make change to yyy repo's file
        run: |
          make some-change # just example, apply change to files in yyy repo

      - name: commit to yyy repo
        run: |
          git config --local user.email "[email protected]"
          git config --local user.name "GitHub Action"
          cd yyy
          git add . -A
          (git commit -m "Update from github actions" && git push) || echo "No changes to commit"

ここで、${{ secrets.PAT_WRITE_YYY }}は、yyyレポジトリにPushするためのPAT。

PATを発行するには、Githubアカウントにログインし、Settingsから、Developer settings > Personal access tokens(Fine-grained)に行き、yyyレポジトリにContents: Write権限を付与する(画像内の上の赤枠):

PATのScopeを設定する

自動でMetadataのRead権限も付与されるが、必須で付随するものなのでこのままでOK(画像内の下の赤枠)

PATを発行したら、yyyレポジトリのSettings > Secrets and Variables > Actionsを開き、secretsにPATを登録する。

ちなみにPATがないと、Pushする権限がないため、以下エラーになる。

remote: Permission to xxx.git denied to github-actions[bot].
fatal: unable to access 'https://github.com/xxx/': The requested URL returned error: 403


Previous Post
GitHub ActionsからCloudflare pagesにDirect Uploadする
Next Post
AWSアカウント間でのS3 to S3ファイルコピー