1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

GitHub Appを作成

Last updated at Posted at 2024-10-30

権限

By default, only organization owners can manage the settings of GitHub Apps in an organization. To allow additional users to change the developer settings of GitHub Apps owned by the organization, an owner can grant them GitHub App manager permissions.

ref

  • Org ownersが必要
  • Orgで管理しているGitHub Appの権限だけを付与することも可能

GitHub App作成例

GitHub Actionsで、変更が必要であればCommitをPushしてPRを作成するApp

やる理由は、普通のGitHub Actions Tokenで commitするとGitHub Actionsがトリガーされないので単純に GITHUB_TOKENでGitHub Actionsを実行するのではなくGitHub Appを使う。

テストAppを作成

Screenshot 2024-10-27 at 21.11.53.png

Webhookは不要

Screenshot 2024-10-27 at 21.12.59.png

権限

Pushするので、ContentsのRead and Write権限を付与

Screenshot 2024-10-27 at 21.13.38.png

Pull Requestの作成用も付与

Screenshot 2024-10-28 at 10.42.32.png

とりあえずAccount以下のみ

Screenshot 2024-10-27 at 21.14.20.png

作成完了すると以下がゲットできる

  • App ID
  • Client ID

インストールする

Screenshot 2024-10-28 at 10.12.08.png

今回はまず github-actions-practice だけにインストールする。

Screenshot 2024-10-28 at 10.13.02.png

インストール完了

Screenshot 2024-10-28 at 10.14.31.png

Private keyが必要なので生成する。生成すると <app名>.<date>.private-key.pemというファイルがダウンロードされる。

cat nakamasato-test-app.2024-10-28.private-key.pem

これで秘密鍵を取得できる。次のステップでGitHub Repository secretsに登録する。

Screenshot 2024-10-28 at 10.11.12.png

GitHub AppをGitHub Actionsで使う

上記で作成したprivate keyをrepository secretにいれる

今回はGH_TEST_APP_IDGH_TEST_PRIVATE_KEYにいれた。

Screenshot 2024-10-28 at 10.34.24.png

Tokenを得るためには以下のStepで可能

      - name: create-github-app-token
        if: steps.date_check.outputs.updated == 'true'
        id: app-token
        uses: actions/create-github-app-token@v1
        with:
          app-id: ${{ secrets.GH_TEST_APP_ID }}
          private-key: ${{ secrets.GH_TEST_PRIVATE_KEY }}
          owner: ${{ github.repository_owner }}

以下のGitHub Actionsで、GitHub ActionsのTokenをそのまま使った場合との比較を見ることができる。

name: create-pull-request

on:
  push:
    branches:
      - main

jobs:
  create-pr-with-github-token:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Check and Update Date
        id: date_check
        run: |
          TODAY=$(date +"%Y-%m-%d")
          FILE_DATE=$(cat updated_on.txt || echo "")
          if [ "$FILE_DATE" != "$TODAY" ]; then
            echo "$TODAY" > updated_on.txt
            echo "updated=true" >> "$GITHUB_OUTPUT"
          else
            echo "updated=false" >> "$GITHUB_OUTPUT"
          fi
      # GitHub Actions are NOT triggered in this PR
      - name: create-pull-request
        if: steps.date_check.outputs.updated == 'true'
        uses: peter-evans/create-pull-request@v5
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          commit-message: 'chore: update updated_on (github token)'
          branch: update-updated-on-github-token
          title: 'chore: update updated_on (github token)'
          body: 'Update updated_on automatically (github token)'
          base: main
          delete-branch: true

  create-pr-with-github-app:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Check and Update Date
        id: date_check
        run: |
          TODAY=$(date +"%Y-%m-%d")
          FILE_DATE=$(cat updated_on.txt || echo "")
          if [ "$FILE_DATE" != "$TODAY" ]; then
            echo "$TODAY" > updated_on.txt
            echo "updated=true" >> "$GITHUB_OUTPUT"
          else
            echo "updated=false" >> "$GITHUB_OUTPUT"
          fi
      - name: create-github-app-token
        if: steps.date_check.outputs.updated == 'true'
        id: app-token
        uses: actions/create-github-app-token@v1
        with:
          app-id: ${{ secrets.GH_TEST_APP_ID }}
          private-key: ${{ secrets.GH_TEST_PRIVATE_KEY }}
          owner: ${{ github.repository_owner }}

      # GitHub Actions are triggered in this PR
      - name: create-pull-request
        if: steps.date_check.outputs.updated == 'true'
        uses: peter-evans/create-pull-request@v5
        with:
          token: ${{ steps.app-token.outputs.token }}
          commit-message: 'chore: update updated_on (github app)'
          branch: update-updated-on-github-app
          title: 'chore: update updated_on (github app)'
          body: 'Update updated_on automatically (github app)'
          base: main
          delete-branch: true

結果:

Screenshot 2024-10-28 at 11.17.13.png

想定通り、GitHub Appで作成したPRはGitHub Actionsが普通のPRと同様に実行されている事がわかります。

GitHub PRをエンドレスに作るGitHub Actionsにならないように注意が必要です。

GitHub Appを使って GitHub APIを叩く (個人のPATではなく)

GitHub AppをGitHub Actionsで使う

Tokenを得るためには以下のStepで可能

create-github-app-tokenを使ってTokenを取得する。あらかじめAPP_IDPRIVATE_KEYをRepository Secretsなどに格納しておく必要がある

      - name: create-github-app-token
        if: steps.date_check.outputs.updated == 'true'
        id: app-token
        uses: actions/create-github-app-token@v1
        with:
          app-id: ${{ secrets.GH_TEST_APP_ID }}
          private-key: ${{ secrets.GH_TEST_PRIVATE_KEY }}
          owner: ${{ github.repository_owner }}

Ref

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?