0
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 Actions 導入

Posted at

はじめに

この記事では、Github Actions ワークフローをプライベートリポジトリ上で実行させる上でポイントだった点を紹介します。

ワークフローの全体像

以下は、今回のワークフローで使用するコードです。このワークフローは、PRが作成または更新された際に、PRに will-be-deployed というラベルが付けられている場合のみ、ビルドとデプロイを実行します。
このコードは、vite のスターターコードを使ってテストコードを追加したのみのプロジェクトを動作確認のために使用しました。

name: Deploy website
on:
  pull_request:
    types: [opened, synchronize, labeled]
env:
  # Use github.base_ref for PR events to get the target branch name.
  BRANCH_NAME: ${{ github.base_ref }}
permissions:
  pull-requests: write
  contents: read
  packages: write
jobs:
  build-and-deploy:
    # Run the job only if the PR contains the label "will-be-deployed"
    if: contains(github.event.pull_request.labels.*.name, 'will-be-deployed')
    runs-on: ubuntu-latest
    steps:
      - name: Repository Checkout
        uses: actions/checkout@v4

      - name: Check The Target Branch
        run: echo "$BRANCH_NAME"

      - name: Setup NodeJS
        uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: "yarn"

      - name: Check Yarn Global Bin & PATH
        run: |
          echo "Yarn global bin is: $(yarn global bin)"
          echo "Current PATH: $PATH"

      - name: Install Dependencies
        run: yarn install --frozen-lockfile

      - name: Run test
        run: yarn run test

      - name: Build project
        run: yarn run build

      - name: Post comment on PR
        uses: peter-evans/create-or-update-comment@v2
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          issue-number: ${{ github.event.pull_request.number }}
          body: |
            **Workflow completed successfully!**

詰まったポイント

権限の設定

ワークフロー実行を試している時に「Repository Checkout」セクションのところで、なぜか「remote: Repository not found.」というエラーが出ていていました。原因と回避方法を調べていたら、github actions checkout の公式リポジトリの Issue #254 で下記のコメントを見つけました。

what you need to do is simply add the Permission for you Github action Job to read from your private repo.

つまり、
プライベートリポジトリ向けのワークフロー実行には、権限設定が必要。

コメントの内容を参考に下記の通りに設定したところ、エラーが出ずにワークフローを実行できるようになりました。

permissions:
  pull-requests: write
  contents: read
  packages: write

公式リンクは下記。
https://docs.github.com/ja/actions/writing-workflows/choosing-what-your-workflow-does/controlling-permissions-for-github_token

補足:上記コメントに加え、さらに最低限「contents: read」があれば、今回のケースの中での該当のエラーを回避できることをテストしました。

おまけ

各セクションの解説

1. トリガー設定

on:
pull_request:
types: [opened, synchronize, labeled]

このワークフローは、PRが新規作成されたり、更新(synchronize)されたり、ラベルが変更されたときに実行されます。
ラベルの変更イベントも対象にしているため、PRに will-be-deployed というラベルが付与された時にのみジョブが実行されます。

2. 環境変数の設定

BRANCH_NAME: ${{ github.base_ref }}

PRイベントの場合、ターゲットブランチ名を取得するために github.base_ref を利用しています。

これにより、デプロイ先ブランチを動的に決定でき、より柔軟な運用が可能です。

3. ジョブ「build-and-deploy」

jobs:
  build-and-deploy:
    if: contains(github.event.pull_request.labels.\*.name, 'will-be-deployed')

ジョブは、指定された条件(PRにwill-be-deployedラベルが含まれる)を満たす場合のみ実行されます。

ポイント:
条件付き実行により、不要なビルドやデプロイメントを避け、CIリソースを効率的に利用できます。

4. ステップごとの詳細 (各ステップの名前ごとに項目化しています)

Repository Checkout

actions/checkout を利用してリポジトリのコードを取得します。

Check The Target Branch

環境変数に設定したターゲットブランチ名を表示。

run: echo "$BRANCH_NAME"

Setup NodeJS

Node.js(バージョン20)とYarnのキャッシュ機能をセットアップします。

uses: actions/setup-node@v4
with:
node-version: 20
cache: "yarn"
Check Yarn Global Bin & PATH:

YarnのグローバルバイナリのパスとシステムPATHを確認し、環境の状態を検証します。

Install Dependencies:

yarn install --frozen-lockfile により、依存パッケージを正確なバージョンでインストールします。

--frozen-lockfile オプションを使用することで、lockfileに記載されているバージョンから変更が加えられないように保証します。

Run test

テストを実行し、コードの品質をチェックします。

Build project

プロジェクトのビルドを行います。

ビルドが成功することで、デプロイメント可能な状態に変換されます。

Post comment on PR

最後に、peter-evans/create-or-update-comment アクションを使用して、PRに成功のメッセージをコメントします。

コメントにより、開発者はワークフローの結果を簡単に確認できます。とても便利な機能だと思います。

まとめ

この記事では、GitHub Actionsを利用して、プライベートリポジトリ上で実行されるワークフローの設定方法を中心に解説しました。
他にもすぐに有用になりそうな点や、特にyarnを使っているプロジェクトで応用可能な点も、記載しましたので参考になれば幸いです。

0
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
0
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?