5
2

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 ラベルをファイルで管理する

5
Last updated at Posted at 2024-04-03

ScreenShot 2024-03-08 19.14.05.png

概要

  • GitHub Actionsを使ってちょっとした業務改善できないか
  • デフォルトのラベルが業務で使うとちょっと扱いづらい
  • 他のリポジトリからラベルの名前と色をパクろう...!
  • GitHubのラベルを手で作っていくのがめんどい...😭
  • 3個目を作った辺りから飽きてくる😮‍💨

そうだ、ファイルで管理しよう😎

Gitシリーズ記事まとめ

利用ツールなど

こちらのアクション、ライブラリを使用します。

実装

下記、2つのファイルを用意します。

  • .github/labels.json
  • .github/workflows/sync-labels.yaml

.github/labels.json

.github/labels.json
[
  {
    "name": "bug",
    "color": "FC2C2B",
    "description": "種別: 不具合の修正"
  },
  {
    "name": "feature",
    "color": "375E97",
    "description": "種別: 新機能"
  },
  {
    "name": "document",
    "color": "FFFFFF",
    "description": "種別: ドキュメントの変更"
  },
  {
    "name": "chore",
    "color": "E2601E",
    "description": "種別: ツールやライブラリの変更"
  },
  {
    "name": "refactor",
    "color": "AFD38D",
    "description": "種別: リファクタリング"
  },
  {
    "name": "duplicate",
    "color": "CCCCCC",
    "description": "種別: 重複"
  },
  {
    "name": "wontfix",
    "color": "CCCCCC",
    "description": "種別: 対応しない"
  },
  {
    "name": "icebox",
    "color": "BDDEEC",
    "description": "種別: 優先度低"
  },
  {
    "name": "release",
    "color": "003B46",
    "description": "種別: リリース"
  },
  {
    "name": "major",
    "color": "07575B",
    "description": "バージョニング: メジャーアップデート"
  },
  {
    "name": "minor",
    "color": "C4DFE6",
    "description": "バージョニング: マイナーアップデート"
  },
  {
    "name": "dependencies",
    "color": "5319E7",
    "description": "種別: セキュリティアップデート"
  },
  {
    "name": "docker",
    "color": "E6D72A",
    "description": "依存関係: docker compose build が必要な変更"
  },
  {
    "name": "npm",
    "color": "E6D72A",
    "description": "依存関係: npm install が必要な変更"
  },
  {
    "name": "composer",
    "color": "E6D72A",
    "description": "依存関係: composer install が必要な変更"
  },
  {
    "name": "migrate",
    "color": "E6D72A",
    "description": "依存関係: php artisan migrate が必要な変更"
  },
  {
    "name": "not-reviewed",
    "color": "F9C1CF",
    "description": "未レビュー"
  }
]

必要に応じて、ラベルの定義を追加・削除してください。

.github/workflows/sync-labels.yaml

.github/workflows/sync-labels.yaml
name: Sync Labels
on:
  workflow_dispatch:
    inputs:
      dry-run:
        description: 'テスト実行モード: 実際の変更は行わず、実行内容のみ確認'
        required: false
        type: boolean
        default: false
      allow-added-labels:
        description: 'リポジトリ独自ラベルを保持: labels.jsonで未定義のラベルを削除しない'
        required: false
        type: boolean
        default: false
jobs:
  sync-labels:
    permissions:
      contents: read
      issues: write
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
      - name: Install dependencies
        run: yarn add -D @azu/github-label-setup
      - name: Run github-label-setup
        run: |
          COMMAND="yarn github-label-setup --token ${{ secrets.GITHUB_TOKEN }} --labels .github/labels.json"
          if [[ "${{ inputs.dry-run }}" == "true" ]]; then
            COMMAND="$COMMAND --dry-run"
          fi
          if [[ "${{ inputs.allow-added-labels }}" == "true" ]]; then
            COMMAND="$COMMAND --allow-added-labels"
          fi
          eval "$COMMAND"

@azu/github-label-setup はnpmパッケージなので、Node.jsをセットアップしています。

.github/labels.json を参照したいので、actions/checkoutも利用しています。

${{ secrets.GITHUB_TOKEN }} は自動トークン認証で認証が必要なGitHub APIを叩く時に渡します。

結果

ScreenShot 2024-03-08 19.22.23.png

workflow_dispatch で任意のブランチ、タイミングで実行します。
--dry-run, --allow-added-labels も引数で選択できるようにしています。

個人的にとても嬉しかったです!

業務改善度: ★☆☆☆☆

星1つです。
新しくリポジトリを作る時だけまとめてラベルを作れるので、5分〜10分くらい時間が短縮できるでしょうか...

ラベルをファイルで管理できるようになったので、次回は応用編です。
続編にご期待ください。

参考

5
2
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
5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?