LoginSignup
9
9

GitHub Actionsで組織内でのみ使えるカスタムアクションを作る方法

Last updated at Posted at 2024-02-16

GitHub Actionsで組織内でのみ使えるカスタムアクションを作る方法

はじめに

リポジトリを作る

カスタムアクションの名前を決めて、その名前でリポジトリをつくる
今回つくるアクションは、プルリクエストの差分を取得し、それを openAiなどの生成AIに送信します。その差分を要約し、その要約をGithub上のプルリクエストのコメントとして投稿します。を作ります。
image.png

権限を変更&確認する

https://github.com/リポジトリ名/settings
Change repository visibilityをinternalにしないと他リポジトリから参照できない
image.png

https://github.com/リポジトリ名/settings/actions
のGeneral
Actions permissions
image.png

Access
image.png

を要チェックして要件に応じて許可する
許可されていないと他リポジトリから参照できない

rootにaction.ymlを作る

イメージ的にはこんな感じ

name: Pull Request Summarizer
inputs:
  USERNAME:
    description: "The username"
    required: true
  PR_NUMBER:
    description: "The number of the pull request to summarize"
    required: true
  PASS:
    description: "Password for openAi"
    required: true
  GITHUB_TOKEN:
    description: "GitHub Token for authentication"
    required: true
runs:
  using: composite
  steps:
    - name: Set up Python
      uses: actions/setup-python@v5
      with:
        python-version: "3"

    - name: Install dependencies
      shell: bash
      run: |
        pip install requests
    - name: Run WeiseHub script
      env:
        USERNAME: ${{ inputs.USERNAME }}
        PR_NUMBER: ${{ inputs.PR_NUMBER }}
        PASSWORD: ${{ inputs.PASS }}
        GITHUB_TOKEN: ${{ inputs.GITHUB_TOKEN }}
        REPO: ${{ github.repository }}
        WEISEHUB_URL: "http://xx"
      shell: bash
      run: |
        python  "${GITHUB_ACTION_PATH}/scripts/xx.py"

実際に他リポジトリから呼び出してみる

name: PR Summarizer
on:
  pull_request:
    types: [opened, reopened]
jobs:
  pr-summarizer-job:
    runs-on: [self-hosted, linux, x64]
    steps:
      - name: pr summarize
        uses: whi-product/カスタムアクション名@v1
        with:
          PR_NUMBER: ${{ github.event.pull_request.number }}
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          USERNAME: ${{ vars.USERNAME }}
          PASS: ${{ secrets.PASS }}

@v1.0.0
のようにタグを指定してもいいし
@main
のようにブランチしてもできる

ハマったところ&学び

ここにチェックを入れてなくて許可されていないかった。

これが抜けていて無いって言われてた😿

Error: Unable to resolve actions. Cannot access repositories 'リポジトリ名'. Enable access using Settings in the Action repository. See https://docs.github.com/enterprise-cloud@latest/repositories/managing-your-repositorys-settings-and-features/enabling-features-for-your-repository/managing-github-actions-settings-for-a-repository#allowing-access-to-components-in-an-internal-repository for more information.

304658471-1db32a8c-754d-4852-8de8-201a1fc5bbeb.png

なぜか引数inputsとかが取れないというエラー

ちょっとしたyamlの書き方ミス

runs:
  using: composite

Required You must set this value to 'composite'.
とのことです。

改めてチェックアウトしなくて良い

呼び出したカスタムアクション中で、チェックアウトしなくてもリポジトリを読み込んでくれているので
scripts/xx.py
みたいなのをaction.ymlで実行するときはそのままできる

GITHUB_ACTION_PATH

以下みたいな形でそのまま実行できる。

runs:
  using: "composite"
  steps:
    - run: $GITHUB_ACTION_PATH/script.sh
      shell: bash

${{ github.action_path }}でも代用可能

作ったカスタムアクション用のリポジトリの環境変数は読み込むことができない

今のところ環境変数は引数で受け取るしかなさそう

以上、GitHub Actionsで組織内でのみ使えるカスタムアクションを作る方法を解説しました。実際のコードの詳細やディレクトリ構成などは、具体的なプロジェクトに合わせて調整してください。

参考文献

9
9
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
9
9