あるGithubレポジトリのGithub Actionsから、別のGithubレポジトリに変更をコミットする方法について。
ワークフローの流れとしては、以下になる:
- まずコミットをPushするレポジトリをCloneする
- 何らかの変更を加える
- 変更をコミットしてPushする
サンプルのワークフロー定義は以下。
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権限を付与する(画像内の上の赤枠):
自動で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