GitHub Actions で外部の組織のプライベートリポジトリにPRを出す
はじめに
organization Aのプライベートリポジトリのコードに変更があった時、organization BのプライベートリポジトリにGitHub ActionsでPRを出したい、というケースがありました。
同じ組織の別リポジトリへPRを出す方法は、調べるといくつか記事が出てきますが、別の組織の場合は設定を変える必要があります。
少しハマったので、記録として残しておきます。
やりたいこと
org-a
の repo-a
から org-b
の repo-b
にPRを出す。
方法
以下の記事を参考にしました。
この記事では同じ組織の別リポジトリへのPRの場合なので、別の組織の場合に変更する部分を説明します。
GitHub Apps
GitHub Apps は org-b
の組織に作成、インストールします。 Repository access
を Only select repositories
にする場合、 repo-b
を指定します。
GitHub Actions
上の記事と同様に tibdex/github-app-token
を使う場合、パラメータとして installation_retrieval_mode: "organization"
と installation_retrieval_payload: "org-b"
を追加します。
全体としては以下のようになります。
name: Test
on:
workflow_dispatch:
jobs:
test:
name: test
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v3
- name: Generate Token
id: generate_token
uses: tibdex/github-app-token@3beb63f4bd073e61482598c45c71c1019b59b73a
with:
app_id: ${{ secrets.CLIENT_APP_ID }}
private_key: ${{ secrets.CLIENT_PRIVATE_KEY }}
installation_retrieval_mode: "organization"
installation_retrieval_payload: "org-b"
- name: Code
run: |
git config --global user.email github-actions[bot]@users.noreply.github.com
git config --global user.name github-actions[bot]
CLONE_DIR=$(mktemp -d)
git clone --depth=1 https://x-access-token:${{ steps.generate_token.outputs.token }}@github.com/repo-b.git ${CLONE_DIR}
cd ${CLONE_DIR}
git checkout -b feature/update
## コードの変更
git add .
if git status | grep -q "Changes to be committed"
then
git commit --message "update"
git push -u origin feature/update
gh pr create \
--title "Update API Client for ${{ needs.setup.outputs.version }}" \
--body "update" \
--repo repo-b \
--base main \
--head feature/update
else
echo "No changes detected"
fi
env:
GH_TOKEN: ${{ steps.generate_token.outputs.token }}
このActionsを org-a
の repo-a
で実行します。
おわりに
別の組織のプライベートリポジトリにGitHub ActionsでPRを出す方法でした。あまりやる機会はないと思いますが、参考にしていただければと思います。