# 複合アクションを作成する

このチュートリアルでは、複合アクションを構築する方法について説明します。

## はじめに

このガイドでは、パッケージ化された複合アクションを作成して使用するために必要な基本コンポーネントについて説明します。 アクションのパッケージ化に必要なコンポーネントのガイドに焦点を当てるため、アクションのコードの機能は最小限に留めます。 アクションによって "Hello World" と "Goodbye" が出力されます。または、カスタム名を指定すると、"Hello \[who-to-greet]" と "Goodbye" が出力されます。 このアクションでは、乱数も `random-number` 出力変数にマップされて、`goodbye.sh` という名前のスクリプトが実行されます。

このプロジェクトを完了すれば、独自の複合アクションを作成し、それをワークフローでテストする方法を理解できます。

> \[!WARNING]
> ワークフローとアクションを作成するときは、攻撃者によってコードが信頼されていない入力を実行する可能性があるかどうかを常に考慮する必要があります。 攻撃者が悪意あるコンテンツを挿入してくるかもしれないので、特定のコンテキストは信頼できない入力として扱うべきです。 詳しくは、「[セキュリティで保護された使用に関するリファレンス](/ja/actions/security-guides/security-hardening-for-github-actions#understanding-the-risk-of-script-injections)」をご覧ください。

### 複合アクションと再利用可能なワークフロー

複合アクションを使用すると、一連のワークフロー ジョブ ステップを 1 つのアクションに収集し、複数のワークフローで 1 つのジョブ ステップとして実行できます。 再利用可能なワークフローは、他のワークフロー内から完全なワークフローを実行できるようにすることで、重複を回避する別の方法を提供します。 詳しくは、「[ワークフロー構成の再利用](/ja/actions/using-workflows/avoiding-duplication)」をご覧ください。

## 前提条件

> \[!NOTE]
> この例では、別のリポジトリ内に複合アクションを作成する方法について説明します。 ただし、同じリポジトリ内に複合アクションを作成することができます。 詳しくは、「[複合アクションを作成する](/ja/actions/creating-actions/creating-a-composite-action#creating-a-composite-action-within-the-same-repository)」をご覧ください。

開始する前に、 GitHubにリポジトリを作成します。

1. ```
          GitHubに新しいパブリック リポジトリを作成します。 任意のリポジトリ名を選択することも、次の `hello-world-composite-action` の例を使用することもできます。 これらのファイルは、プロジェクトが GitHubにプッシュされた後に追加できます。 詳しくは、「[AUTOTITLE](/repositories/creating-and-managing-repositories/creating-a-new-repository)」をご覧ください。
   ```

2. リポジトリをお手元のコンピューターにクローンします。 詳しくは、「[リポジトリをクローンする](/ja/repositories/creating-and-managing-repositories/cloning-a-repository)」をご覧ください。

3. ターミナルから、ディレクトリを新しいリポジトリに変更します。

   ```shell copy
   cd hello-world-composite-action
   ```

4. ```
          `hello-world-composite-action` リポジトリで、`goodbye.sh` という名前の新しいファイルを次のコード例で作成します。
   ```

   ```shell copy
   echo "echo Goodbye" > goodbye.sh
   ```

5. ターミナルから、`goodbye.sh` 実行可能ファイルを作成します。

   <div class="ghd-tool linux">

   ```shell copy
   chmod +x goodbye.sh
   ```

   </div>

   <div class="ghd-tool mac">

   ```shell copy
   chmod +x goodbye.sh
   ```

   </div>

   <div class="ghd-tool windows">

   ```shell copy
   git add --chmod=+x -- goodbye.sh
   ```

   </div>

6. ターミナルから、`goodbye.sh` ファイルをチェックインします。

   <div class="ghd-tool linux">

   ```shell copy
   git add goodbye.sh
   git commit -m "Add goodbye script"
   git push
   ```

   </div>

   <div class="ghd-tool mac">

   ```shell copy
   git add goodbye.sh
   git commit -m "Add goodbye script"
   git push
   ```

   </div>

   <div class="ghd-tool windows">

   ```shell copy
   git commit -m "Add goodbye script"
   git push
   ```

   </div>

## アクションのメタデータファイルの作成

1. ```
          `hello-world-composite-action` リポジトリで、`action.yml` という名前の新しいファイルを作成し、次のコード例を追加します。 この構文の詳細については、「[AUTOTITLE](/actions/creating-actions/metadata-syntax-for-github-actions#runs-for-composite-actions)」を参照してください。
   ```

   ```yaml copy
   name: 'Hello World'
   description: 'Greet someone'
   inputs:
     who-to-greet:  # id of input
       description: 'Who to greet'
       required: true
       default: 'World'
   outputs:
     random-number:
       description: "Random number"
       value: ${{ steps.random-number-generator.outputs.random-number }}
   runs:
     using: "composite"
     steps:
       - name: Set Greeting
         run: echo "Hello $INPUT_WHO_TO_GREET."
         shell: bash
         env:
           INPUT_WHO_TO_GREET: ${{ inputs.who-to-greet }}

       - name: Random Number Generator
         id: random-number-generator
         run: echo "random-number=$(echo $RANDOM)" >> $GITHUB_OUTPUT
         shell: bash

       - name: Set GitHub Path
         run: echo "$GITHUB_ACTION_PATH" >> $GITHUB_PATH
         shell: bash
         env:
           GITHUB_ACTION_PATH: ${{ github.action_path }}

       - name: Run goodbye.sh
         run: goodbye.sh
         shell: bash

   ```

   このファイルでは、`who-to-greet` 入力を定義し、ランダムに生成された数値を `random-number` 出力変数にマップし、アクションのパスをランナーのシステム パスに追加し (実行中に `goodbye.sh` スクリプトを見つけるため)、`goodbye.sh` スクリプトを実行します。

   出力の管理の詳細については、「[メタデータ構文リファレンス](/ja/actions/creating-actions/metadata-syntax-for-github-actions#outputs-for-composite-actions)」を参照してください。

   ```
          `github.action_path` の使い方の詳細については、「[AUTOTITLE](/actions/learn-github-actions/contexts#github-context)」を参照してください。
   ```

2. ターミナルから、`action.yml` ファイルをチェックインします。

   ```shell copy
   git add action.yml
   git commit -m "Add action"
   git push
   ```

3. ターミナルから、タグを追加します。 この例では、`v1` という名前のタグを使用します。 詳しくは、「[カスタム アクションについて](/ja/actions/creating-actions/about-custom-actions#using-release-management-for-actions)」をご覧ください。

   ```shell copy
   git tag -a -m "Description of this release" v1
   git push --follow-tags
   ```

## ワークフローでアクションをテストする

次のワークフロー コードでは、「[複合アクションを作成する](/ja/actions/creating-actions/creating-a-composite-action#creating-an-action-metadata-file)」で完成した hello world アクションを使います。

ワークフロー コードを別のリポジトリの `.github/workflows/main.yml` ファイルにコピーしますが、`OWNER` と`SHA` をリポジトリ オーナーとコミットに使用したい SHA にそれぞれ置き換えます。
`who-to-greet` 入力を自分の名前に置き換えることもできます。

```yaml copy
on: [push]

jobs:
  hello_world_job:
    runs-on: ubuntu-latest
    name: A job to say hello
    steps:
      - uses: actions/checkout@v6
      - id: foo
        uses: OWNER/hello-world-composite-action@SHA
        with:
          who-to-greet: 'Mona the Octocat'
      - run: echo random-number "$RANDOM_NUMBER"
        shell: bash
        env:
          RANDOM_NUMBER: ${{ steps.foo.outputs.random-number }}
```

リポジトリから **\[アクション]** タブをクリックして、最新のワークフロー実行を選択します。 出力には、「Hello Mona the Octocat」、"Goodbye"スクリプトの結果、および乱数が含まれているはずです。

## 同じリポジトリ内に複合アクションを作成する

1. ```
          `hello-world-composite-action` という新しいサブフォルダーを作成します。これはリポジトリ内の任意のサブフォルダーに配置できます。 ただし、整理しやすいように、`.github/actions` サブフォルダーに配置することをお勧めします。
   ```

2. ```
          `hello-world-composite-action` フォルダーで、同じ手順を実行して `goodbye.sh` スクリプトを作成します
   ```

   ```shell copy
   echo "echo Goodbye" > goodbye.sh
   ```

   <div class="ghd-tool linux">

   ```shell copy
   chmod +x goodbye.sh
   ```

   </div>

   <div class="ghd-tool mac">

   ```shell copy
   chmod +x goodbye.sh
   ```

   </div>

   <div class="ghd-tool windows">

   ```shell copy
   git add --chmod=+x -- goodbye.sh
   ```

   </div>

   <div class="ghd-tool linux">

   ```shell copy
   git add goodbye.sh
   git commit -m "Add goodbye script"
   git push
   ```

   </div>

   <div class="ghd-tool mac">

   ```shell copy
   git add goodbye.sh
   git commit -m "Add goodbye script"
   git push
   ```

   </div>

   <div class="ghd-tool windows">

   ```shell copy
   git commit -m "Add goodbye script"
   git push
   ```

   </div>

3. ```
          `hello-world-composite-action` フォルダーに、`action.yml` ファイルを[AUTOTITLE](/actions/creating-actions/creating-a-composite-action#creating-an-action-metadata-file)の手順に基づいて作成します。
   ```

4. アクションを使うときは、複合アクションの `action.yml` ファイルが配置されているフォルダーへの相対パスを `uses` キーに使います。 以下の例では、`.github/actions/hello-world-composite-action` フォルダー内にあることを前提としています。

```yaml copy
on: [push]

jobs:
  hello_world_job:
    runs-on: ubuntu-latest
    name: A job to say hello
    steps:
      - uses: actions/checkout@v6
      - id: foo
        uses: ./.github/actions/hello-world-composite-action
        with:
          who-to-greet: 'Mona the Octocat'
      - run: echo random-number "$RANDOM_NUMBER"
        shell: bash
        env:
          RANDOM_NUMBER: ${{ steps.foo.outputs.random-number }}
```