12
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でワークフローを作成していると、同じような処理を複数のステップで繰り返し書くことがよくあります。この記事では、複合アクション(Composite Action)を使って処理を共通化する方法を紹介します。

複合アクション(Composite Action)とは

複合アクションは、複数のステップを1つのアクションとしてまとめて再利用できる機能です。ローカルリポジトリ内に配置することで、ワークフローから何度でも呼び出せます。

実装例

今回は、シンプルな挨拶を出力する共通処理を作成してみます。

1. 共通処理の作成

.github/actions/hello/action.yml として複合アクションを作成します。

name: 'Hello Action'
description: '共通の挨拶処理'

inputs:
  name:
    description: '名前'
    required: true

runs:
  using: 'composite'
  steps:
    - name: Say hello
      shell: bash
      run: echo "Hello, ${{ inputs.name }}!"

ポイント:

  • runs.using: 'composite' で複合アクションであることを宣言
  • inputs でパラメータを定義
  • shell の指定が必須

2. ワークフローから呼び出す

.github/workflows/test.yml で共通処理を使用します。

name: Test

on:
  push:
  workflow_dispatch:

jobs:
  greet:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Greet Alice
        uses: ./.github/actions/hello
        with:
          name: Alice

      - name: Greet Bob
        uses: ./.github/actions/hello
        with:
          name: Bob

ポイント:

  • ローカルパス ./.github/actions/hello で参照
  • with でパラメータを渡す
  • 同じアクションを異なるパラメータで何度でも呼び出せる

ディレクトリ構成

.github/
├── actions/
│   └── hello/
│       └── action.yml      # 共通処理
└── workflows/
    └── test.yml            # 共通処理を使うワークフロー

メリット

  1. DRY原則: 同じコードを繰り返し書く必要がない
  2. 保守性: 共通処理を1箇所で管理できる
  3. 可読性: ワークフローがシンプルになる

まとめ

複合アクションを使うことで、GitHub Actionsの処理を効率的に共通化できます。環境セットアップや通知処理など、繰り返し使う処理があればぜひ活用してみてください。

参考リンク

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