ノート: GitHubホストランナーは、現在GitHub Enterprise Serverでサポートされていません。 GitHubパブリックロードマップで、計画されている将来のサポートに関する詳しい情� �を見ることができます。
はじめに
このガイドでは、Docker Hubのredisイメージを使ってサービスコンテナを設定するワークフローの例を紹介します。 このワークフローは、Redisのクライアントを作成してクライアントにデータを展開するスクリプトを実行します。 Redisクライアントを作成して展開するワークフローをテストするために、このスクリプトはクライアントのデータをコンソールに出力します。
Note: If your workflows use Docker container actions, job containers, or service containers, then you must use a Linux runner:
- GitHubホストランナーを使うなら、Ubuntuランナーを使わなければなりません。
- セルフホストランナーを使っているなら、ランナーとしてLinuxマシンを使い、Dockerをインストールしておかなければなりません。
必要な環境
GitHub Actionsとのサービスコンテナの動作と、ジョブを直接ランナー上で動作させる� �合とコンテナ内で動作させる� �合のネットワーキングの差異について、親しんでおいてく� さい。 詳しい情� �については「サービスコンテナについて」を参照してく� さい。
YAML、GitHub Actionsの構文、Redisの基本な理解があれば役立つかも知れません。 詳しい情� �については、以下を参照してく� さい。
- 「GitHub Actions を学ぶ」
- Redisのドキュメンテーション中のGetting Started with Redis
コンテナ内でのジョブの実行
ジョブをコンテナ内で実行するように設定すれば、ジョブとサービスコンテナ間のネットワーク設定が単純になります。 同じユーザ定義ブリッジネットワーク上にあるDockerコンテナは、すべてのポートを互いに公開するので、サービスコンテナのポートをDockerホストにマップする必要がありません。 ワークフロー中で設定したラベルを使って、ジョブコンテナからサービスコンテナにアクセスできます。
このワークフローファイルはリポジトリの.github/workflowsディレクトリにコピーして、必要に応じて修正できます。
name: Redis container example on: push jobs: # Label of the container job container-job: # Containers must run in Linux based operating systems runs-on: ubuntu-latest # Docker Hub image that `container-job` executes in container: node:10.18-jessie # Service containers to run with `container-job` services: # Label used to access the service container redis: # Docker Hub image image: redis # Set health checks to wait until redis has started options: >- --health-cmd "redis-cli ping" --health-interval 10s --health-timeout 5s --health-retries 5 steps: # Downloads a copy of the code in your repository before running CI tests - name: Check out repository code uses: actions/checkout@v2 # Performs a clean installation of all dependencies in the `package.json` file # For more information, see https://docs.npmjs.com/cli/ci.html - name: Install dependencies run: npm ci - name: Connect to Redis # Runs a script that creates a Redis client, populates # the client with data, and retrieves data run: node client.js # Environment variable used by the `client.js` script to create a new Redis client. env: # Redisサービスコンテナとの通信に使われるホスト名 REDIS_HOST: redis # デフォルトのRedisポート REDIS_PORT: 6379コンテナジョブの設定
このワークフローはnode:10.18-jessieコンテナ内で実行されるジョブを設定し、ubuntu-latest GitHubホストランナーをコンテナ用のDockerホストとして使用します。 node:10.18-jessieコンテナに関する詳しい情� �についてはDocker Hubのnodeイメージを参照してく� さい。
ワークフローはredisというラベルでサービスコンテナを設定します。 すべてのサービスはコンテナ内で実行しなければならないので、各サービスについてコンテナのイメージを指定しなければなりません。 この例はredisコンテナイメージを使っており、サービスが動作していることを確認するためのヘルスチェックオプションが含まれます。 詳しい情� �については、Docker Hubのredis imageを参照してく� さい。
jobs: # コンテナジョブのラベル container-job: # コンテナはLinuxベースのオペレーティングシステ� 内で実行しなければならない runs-on: ubuntu-latest # `container-job`が実行されるDocker Hubのイメージ container: node:10.18-jessie # `container-job`と実行されるサービスコンテナ services: # サービスコンテナへのアクセスに使われるラベル redis: # Docker Hubのイメージ image: redis # redisが起動するまで待つヘルスチェックの設定 options: >- --health-cmd "redis-cli ping" --health-interval 10s --health-timeout 5s --health-retries 5ステップの設定
ワークフローは以下のステップを実行します。
- ランナー上にリポジトリをチェックアウト
- 依存関係のインストール
- クライアントを作成するスクリプトの実行
steps: # Downloads a copy of the code in your repository before running CI tests - name: Check out repository code uses: actions/checkout@v2 # Performs a clean installation of all dependencies in the `package.json` file # For more information, see https://docs.npmjs.com/cli/ci.html - name: Install dependencies run: npm ci - name: Connect to Redis # Runs a script that creates a Redis client, populates # the client with data, and retrieves data run: node client.js # Environment variable used by the `client.js` script to create a new Redis client. env: # Redisサービスコンテナとの通信に使われるホスト名 REDIS_HOST: redis # デフォルトのRedisポート REDIS_PORT: 6379client.jsスクリプトは、クライアントを作成するために環境変数のREDIS_HOSTとREDIS_PORTを探します。 ワークフローは、これら2つの環境変数を"Connect to Redis"ステップの一部として設定し、client.jsスクリプトから利用できるようにします。 このスクリプトに関する詳しい情� �については「Redisサービスコンテナのテスト」を参照してく� さい。
Redisサービスのホスト名は、ワークフロー中で設定されたラベルで、ここではredisです。 同じユーザー定義ブリッジネットワーク上のDockerコンテナは、デフォルトですべてのポートをオープンするので、サービスコンテナにはデフォルトのRedisのポートである6379でアクセスできます。
ランナーマシン上で直接のジョブの実行
ランナーマシン上で直接ジョブを実行する� �合、サービスコンテナ上のポートをDockerホスト上のポートにマップしなければなりません。 Dockerホストからサービスコンテナへは、localhostとDockerホストのポート番号を使ってアクセスできます。
このワークフローファイルはリポジトリの.github/workflowsディレクトリにコピーして、必要に応じて修正できます。
name: Redis runner example on: push jobs: # Label of the runner job runner-job: # You must use a Linux environment when using service containers or container jobs runs-on: ubuntu-latest # Service containers to run with `runner-job` services: # Label used to access the service container redis: # Docker Hub image image: redis # Set health checks to wait until redis has started options: >- --health-cmd "redis-cli ping" --health-interval 10s --health-timeout 5s --health-retries 5 ports: # Maps port 6379 on service container to the host - 6379:6379 steps: # Downloads a copy of the code in your repository before running CI tests - name: Check out repository code uses: actions/checkout@v2 # Performs a clean installation of all dependencies in the `package.json` file # For more information, see https://docs.npmjs.com/cli/ci.html - name: Install dependencies run: npm ci - name: Connect to Redis # Runs a script that creates a Redis client, populates # the client with data, and retrieves data run: node client.js # Environment variable used by the `client.js` script to create # a new Redis client. env: # Redisサービスコンテナとの通信に使われるホスト名 REDIS_HOST: localhost # デフォルトのRedisポート REDIS_PORT: 6379ランナージョブの設定
この例では、 ubuntu-latest GitHubホストランナーをDockerホストとして使います。
ワークフローはredisというラベルでサービスコンテナを設定します。 すべてのサービスはコンテナ内で実行しなければならないので、各サービスについてコンテナのイメージを指定しなければなりません。 この例はredisコンテナイメージを使っており、サービスが動作していることを確認するためのヘルスチェックオプションが含まれます。 詳しい情� �については、Docker Hubのredis imageを参照してく� さい。
このワークフローはRedisサービスコンテナ上のポート6379をDockerホストにマップします。 portsキーワードに関する詳しい情� �については「サービスコンテナについて」を参照してく� さい。
jobs: # ランナージョブのラベル runner-job: # サービスコンテナもしくはコンテナジョブを使う際にはLinux環境を使わなければならない runs-on: ubuntu-latest # `runner-job`と実行するサービスコンテナ services: # サービスコンテナへのアクセスに使うラベル redis: # Docker Hubのイメージ image: redis # redisが起動するまで待つヘルスチェックの設定 options: >- --health-cmd "redis-cli ping" --health-interval 10s --health-timeout 5s --health-retries 5 ports: # サービスコンテナ上のポート6379をホストにマップ - 6379:6379ステップの設定
ワークフローは以下のステップを実行します。
- ランナー上にリポジトリをチェックアウト
- 依存関係のインストール
- クライアントを作成するスクリプトの実行
steps: # Downloads a copy of the code in your repository before running CI tests - name: Check out repository code uses: actions/checkout@v2 # Performs a clean installation of all dependencies in the `package.json` file # For more information, see https://docs.npmjs.com/cli/ci.html - name: Install dependencies run: npm ci - name: Connect to Redis # Runs a script that creates a Redis client, populates # the client with data, and retrieves data run: node client.js # Environment variable used by the `client.js` script to create # a new Redis client. env: # Redisサービスコンテナとの通信に使われるホスト名 REDIS_HOST: localhost # デフォルトのRedisポート REDIS_PORT: 6379client.jsスクリプトは、クライアントを作成するために環境変数のREDIS_HOSTとREDIS_PORTを探します。 ワークフローは、これら2つの環境変数を"Connect to Redis"ステップの一部として設定し、client.jsスクリプトから利用できるようにします。 このスクリプトに関する詳しい情� �については「Redisサービスコンテナのテスト」を参照してく� さい。
ホスト名はlocalhostもしくは127.0.0.1です。
Redisサービスコンテナのテスト
ワークフローを以下のスクリプトでテストできます。このスクリプトはRedisクライアントを作成し、いくつかのプレースホルダーデータをクライアントに展開します。 そしてこのスクリプトは、Redisクライアント内に保存された値をターミナルに出力します。 スクリプトには好きな言語を使えますが、この例ではNode.jsとnpmモジュールのredisを使っています。 詳しい情� �についてはnpm redisモジュールを参照してく� さい。
client.jsを修正して、ワークフローで必要なRedisの操作を含めることができます。 この例では、スクリプトはRedisクライアントのインスタンスを作成し、プレースホルダーデータを追� し、そしてそのデータを取り出します。
以下のコードで、client.js と名付けた新しいファイルをリポジトリに追� してく� さい。
const redis = require("redis"); // 新しいRedisクライアントの作成 // REDIS_HOSTが設定されていなければ、デフォルトのホストはlocalhost // REDIS_PORTが設定されていなければ、デフォルトのポートは6379 const redisClient = redis.createClient({ host: process.env.REDIS_HOST, port: process.env.REDIS_PORT }); redisClient.on("error", function(err) { console.log("Error " + err); }); // キー"octocat"に"Mona the octocat"という値を設定 redisClient.set("octocat", "Mona the Octocat", redis.print); // キーを"octocat"、フィールドを"species"、"value"を"Cat and Octopus"に設定 redisClient.hset("species", "octocat", "Cat and Octopus", redis.print); // キーを"octocat"、フィールドを"species"、"value"を"Dinosaur and Octopus"に設定 redisClient.hset("species", "dinotocat", "Dinosaur and Octopus", redis.print); // キーを"octocat"、フィールドを"species"、 "value"を"Cat and Robot"に設定 redisClient.hset(["species", "robotocat", "Cat and Robot"], redis.print); // キー"species"のすべてのフィールドを取得 redisClient.hkeys("species", function (err, replies) { console.log(replies.length + " replies:"); replies.forEach(function (reply, i) { console.log(" " + i + ": " + reply); }); redisClient.quit(); });このスクリプトは新しいRedisクライアントをcreateClientメソッドを使って作成します。これは、パラメーターとしてhostとportを受け付けます。 スクリプトは環境変数のREDIS_HOSTとREDIS_PORTを使って、クライアントのIPアドレスとポートを設定します。 hostとportが定義されていない� �合、デフォルトのホストはlocalhostで、デフォルトのポートは6379になります。
このスクリプトは、set及びhsetメソッドを使ってデータベースにいくつかのキー、フィールド、値を展開します。 Redisデータベースがデータを含んでいることを確認するために、スクリプトはデータベースの内容をコンソールログに出力します。
このワークフローを実行すると、"Connect to Redis"ステップで以下のように出力され、Redisのクライアントが作成され、データが追� されたことが確認できます。
Reply: OK Reply: 1 Reply: 1 Reply: 1 3 replies: 0: octocat 1: dinotocat 2: robotocat