# Docker コンテナーのアクションを作成する

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

## はじめに

このガイドでは、パッケージ化されたDockerコンテナのアクションを作成して使うために必要な、基本的コンポーネントについて学びます。 アクションのパッケージ化に必要なコンポーネントのガイドに焦点を当てるため、アクションのコードの機能は最小限に留めます。 アクションでは、ログに "Hello World" が出力されます。カスタム名を指定した場合は "Hello \[who-to-greet]" が出力されます。

このプロジェクトを完了すると、あなたの Docker コンテナのアクションをビルドして、ワークフローでテストする方法が理解できます。

セルフホストランナーでDockerコンテナアクションを実行するためには、Linuxオペレーティングシステムを使い、Dockerがインストールされていなければなりません。 自己ホストランナーの要件の詳細については、「[セルフホステッド ランナー リファレンス](/ja/actions/reference/runners/self-hosted-runners#requirements-for-self-hosted-runner-machines)」を参照してください。

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

## 前提条件

* GitHubにリポジトリを作成し、ワークステーションに複製する必要があります。 詳細については、「[新しいリポジトリの作成](/ja/repositories/creating-and-managing-repositories/creating-a-new-repository)」および「[リポジトリをクローンする](/ja/repositories/creating-and-managing-repositories/cloning-a-repository)」を参照してください。
* リポジトリで Git LFSを使用する場合は、リポジトリのアーカイブにオブジェクトを含める必要があります。 詳しくは、「[リポジトリのアーカイブで Git LFS オブジェクトを管理する](/ja/enterprise-cloud@latest/repositories/managing-your-repositorys-settings-and-features/managing-repository-settings/managing-git-lfs-objects-in-archives-of-your-repository)」をご覧ください。
* GitHub Actions、環境変数、Docker コンテナー ファイル システムの基本的な理解が役立つ場合があります。 詳細については、「[変数に情報を格納する](/ja/actions/how-tos/write-workflows/choose-what-workflows-do/use-variables)」および「[GitHub ホステッド ランナー リファレンス](/ja/actions/reference/runners/github-hosted-runners#docker-container-filesystem)」を参照してください。

## Dockerfileの作成

新しい `hello-world-docker-action` ディレクトリに、新しい `Dockerfile` ファイルを作成します。 問題が発生する場合は、ファイル名で大文字が正しく使用されていることを確認します (`D` は大文字にしますが、`f` は大文字にしません)。 詳しくは、「[GitHub ActionsのためのDockerfileサポート](/ja/actions/reference/workflows-and-actions/dockerfile-support)」をご覧ください。

**Dockerfile**

```dockerfile copy
# Container image that runs your code
FROM alpine:3.10

# Copies your code file from your action repository to the filesystem path `/` of the container
COPY entrypoint.sh /entrypoint.sh

# Code file to execute when the docker container starts up (`entrypoint.sh`)
ENTRYPOINT ["/entrypoint.sh"]
```

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

上で作成した `action.yml` ディレクトリに新しい `hello-world-docker-action` ファイルを作成します。 詳しくは、「[メタデータ構文リファレンス](/ja/actions/reference/workflows-and-actions/metadata-syntax)」をご覧ください。

**action.yml**

```yaml copy
# action.yml
name: 'Hello World'
description: 'Greet someone and record the time'
inputs:
  who-to-greet:  # id of input
    description: 'Who to greet'
    required: true
    default: 'World'
outputs:
  time: # id of output
    description: 'The time we greeted you'
runs:
  using: 'docker'
  image: 'Dockerfile'
  args:
    - ${{ inputs.who-to-greet }}
```

このメタデータで、1 つの `who-to-greet` 入力と 1 つの `time` 出力パラメーターを定義します。 入力を Docker コンテナーに渡すには、`inputs` を使用して入力を宣言し、`args` キーワードで入力を渡す必要があります。
`args` に含めたすべてのものがコンテナーに渡されますが、アクションのユーザーにわかりやすいよう、inputs を使用することをお勧めします。

GitHub は、 `Dockerfile`からイメージをビルドし、このイメージを使用して新しいコンテナーでコマンドを実行します。

## アクションのコードの記述

任意のベース Docker イメージを選択できるので、アクションに任意の言語を選択できます。 次のシェル スクリプトの例では、`who-to-greet` 入力変数を使って、ログ ファイルに "Hello \[who-to-greet]" と出力されます。

次に、スクリプトは現在の時刻を取得し、それをジョブ内で後に実行するアクションが利用できる出力変数に設定します。
GitHubが出力変数を認識するには、`$GITHUB_OUTPUT`環境ファイル (`echo "<output name>=<value>" >> $GITHUB_OUTPUT`) に書き込む必要があります。 詳しくは、「[GitHub Actions のワークフロー コマンド](/ja/actions/reference/workflows-and-actions/workflow-commands#setting-an-output-parameter)」をご覧ください。

1. `entrypoint.sh` ディレクトリに新しい `hello-world-docker-action` ファイルを作成します。

2. 次のコードを `entrypoint.sh` ファイルに追加します。

**entrypoint.sh**

```shell copy
#!/bin/sh -l

echo "Hello $1"
time=$(date)
echo "time=$time" >> $GITHUB_OUTPUT

```

`entrypoint.sh` がエラーなしで実行された場合、アクションの状態は `success` に設定されます。 アクションのコード中で明示的に終了コードを設定して、アクションのステータスを提供することもできます。 詳しくは、「[アクションの終了コードの設定](/ja/actions/how-tos/create-and-publish-actions/set-exit-codes)」をご覧ください。

1. `entrypoint.sh` ファイルを実行可能にします。 Git では、クローンやフォークがあるたびにリセットされないように、ファイルのアクセス許可モードを明示的に変更する方法が用意されています。

   ```shell copy
   git add entrypoint.sh
   git update-index --chmod=+x entrypoint.sh
   ```

2. 必要に応じて、Git インデックス内のファイルのアクセス許可モードを確認するには、次のコマンドを実行します。

   ```shell copy
   git ls-files --stage entrypoint.sh
   ```

`100755 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0       entrypoint.sh` のような出力は、ファイルに実行可能なアクセス許可があることを意味します。 この例では、`755` が実行可能なアクセス許可を示しています。

## READMEの作成

アクションの使用方法を説明するために、README ファイルを作成できます。 README はアクションの公開を計画している時に非常に役立ちます。また、アクションの使い方をあなたやチームが覚えておく方法としても優れています。

`hello-world-docker-action` ディレクトリに、次の情報を指定する `README.md` ファイルを作成します。

* アクションの動作に関する詳細な説明。
* 必須の入力および出力の引数。
* 省略可能な入力および出力の引数。
* アクションで使用されるシークレット。
* アクションで使用される環境変数。
* ワークフローでのアクションの使用方法の例。

**README.md**

```markdown copy
# Hello world docker action

This action prints "Hello World" or "Hello" + the name of a person to greet to the log.

## Inputs

## `who-to-greet`

**Required** The name of the person to greet. Default `"World"`.

## Outputs

## `time`

The time we greeted you.

## Example usage

uses: actions/hello-world-docker-action@v2
with:
  who-to-greet: 'Mona the Octocat'
```

## アクションをコミットし、タグを付けて、プッシュする

お使いのターミナルから、`action.yml`、`entrypoint.sh`、`Dockerfile`、`README.md` の各ファイルをコミットします。

アクションのリリースにはバージョンタグを加えることもベストプラクティスです。 アクションのバージョン管理の詳細については、「[カスタム アクションの管理](/ja/actions/how-tos/create-and-publish-actions/manage-custom-actions#using-release-management-for-actions)」を参照してください。

```shell copy
git add action.yml entrypoint.sh Dockerfile README.md
git commit -m "My first action is ready"
git tag -a -m "My first action release" v1
git push --follow-tags
```

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

これで、ワークフローでアクションをテストできるようになりました。

* アクションがプライベート リポジトリにある場合は、アクセスできるユーザーを制御できます。 詳しくは、「[リポジトリのGitHub Actions設定の管理](/ja/repositories/managing-your-repositorys-settings-and-features/enabling-features-for-your-repository/managing-github-actions-settings-for-a-repository#allowing-access-to-components-in-a-private-repository)」をご覧ください。
* 」を参照してください。アクションが内部リポジトリにある場合、アクションは同じリポジトリ内のワークフローでのみ使用できます。
* パブリック アクションは、任意のリポジトリ内のワークフローで使用できます。

### パブリックアクションを使用する例

次のワークフロー コードでは、パブリックの \_\_ リポジトリにある完全な `actions/hello-world-docker-action` アクションを使用します。 次のワークフローの例のコードを `.github/workflows/main.yml` ファイルにコピーしますが、`actions/hello-world-docker-action` を実際のリポジトリとアクション名に置き換えてください。
`who-to-greet` 入力を自分の名前に置き換えることもできます。
パブリック アクションは、 GitHub Marketplaceに公開されていない場合でも使用できます。 詳しくは、「[GitHub Marketplaceでのアクションの公開](/ja/actions/how-tos/create-and-publish-actions/publish-in-github-marketplace#publishing-an-action)」をご覧ください。

**.github/workflows/main.yml**

```yaml copy
on: [push]

jobs:
  hello_world_job:
    runs-on: ubuntu-latest
    name: A job to say hello
    steps:
      - name: Hello world action step
        id: hello
        uses: actions/hello-world-docker-action@v2
        with:
          who-to-greet: 'Mona the Octocat'
      # Use the output from the `hello` step
      - name: Get the output time
        run: echo "The time was ${{ steps.hello.outputs.time }}"
```

### プライベートアクションを使用する例

次の例のワークフロー コードを、アクションのリポジトリ内の `.github/workflows/main.yml` ファイルにコピーします。
`who-to-greet` 入力を自分の名前に置き換えることもできます。
このプライベート アクションを GitHub Marketplaceに発行することはできません。また、このリポジトリでのみ使用できます。

**.github/workflows/main.yml**

```yaml copy
on: [push]

jobs:
  hello_world_job:
    runs-on: ubuntu-latest
    name: A job to say hello
    steps:
      # To use this repository's private action,
      # you must check out the repository
      - name: Checkout
        uses: actions/checkout@v6
      - name: Hello world action step
        uses: ./ # Uses an action in the root directory
        id: hello
        with:
          who-to-greet: 'Mona the Octocat'
      # Use the output from the `hello` step
      - name: Get the output time
        run: echo "The time was ${{ steps.hello.outputs.time }}"
```

リポジトリから **\[アクション]** タブをクリックして、最新のワークフロー実行を選択します。 **\[ジョブ]** または視覚化グラフで、"**A job to say hello**" をクリックします。

**Hello world action step** をクリックすると、"Hello Mona the Octocat" またはログに出力されている `who-to-greet` に入力した名前が表示されます。 タイムスタンプを表示するには、 **\[出力時刻の取得]** をクリックします。

## コンテナー アクションで作成したファイルへのアクセス

コンテナー アクションを実行する、ランナーの既定の作業ディレクトリ (`GITHUB_WORKSPACE`) がコンテナー上の `/github/workspace` ディレクトリに自動的にマップされます。 コンテナーのこのディレクトリに追加されたファイルは、同じジョブ内の後続のステップで使用できます。 たとえば、プロジェクトをビルドするコンテナー アクションがあり、ビルド出力を成果物としてアップロードする場合は、次の手順を使用できます。

**workflow\.yml**

```yaml copy
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v6

      # Output build artifacts to /github/workspace on the container.
      - name: Containerized Build
        uses: ./.github/actions/my-container-action

      - name: Upload Build Artifacts
        uses: actions/upload-artifact@v4
        with:
          name: workspace_artifacts
          path: ${{ github.workspace }}
```

ビルド出力を成果物としてアップロードする方法の詳細については、「[ワークフロー成果物を使ったデータの格納と共有](/ja/actions/tutorials/store-and-share-data)」を参照してください。

## の Docker コンテナー アクションの例 GitHub.com

GitHub.comでは、Docker コンテナー アクションの多くの例を見つけることができます。

* [github/issue-metrics](https://github.com/github/issue-metrics)
* [microsoft/infersharpaction](https://github.com/microsoft/infersharpaction)
* [microsoft/ps-docs](https://github.com/microsoft/ps-docs)