From eb2f8daee66c14c3d7bf6fe1fb4f8a87184060e6 Mon Sep 17 00:00:00 2001 From: Fred Gohsman Date: Fri, 20 Jan 2023 07:43:49 -0500 Subject: [PATCH 1/6] Create service-containers-with-job-container.yml --- .../service-containers-with-job-container.yml | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 .github/workflows/service-containers-with-job-container.yml diff --git a/.github/workflows/service-containers-with-job-container.yml b/.github/workflows/service-containers-with-job-container.yml new file mode 100644 index 0000000..c39a35e --- /dev/null +++ b/.github/workflows/service-containers-with-job-container.yml @@ -0,0 +1,56 @@ +# Example of using a job that runs in a container and +# also leverages a service container + +# This workflow contains a job that runs in a docker container +# Containers must run in Linux based operating systems + +# https://docs.github.com/en/actions/using-containerized-services/creating-redis-service-containers + +name: Service Container Example with Job Container + +# Manually triggered +on: workflow_dispatch + +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:16-bullseye + + # 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@v3 + + # 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: + # The hostname used to communicate with the Redis service container + REDIS_HOST: redis + # The default Redis port + REDIS_PORT: 6379 From 3ae87ea7b3d36a0355157adfb93fb336759fbc31 Mon Sep 17 00:00:00 2001 From: Fred Gohsman Date: Fri, 20 Jan 2023 07:47:21 -0500 Subject: [PATCH 2/6] Create service-containers-on-runner.yml --- .../service-containers-on-runner.yml | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 .github/workflows/service-containers-on-runner.yml diff --git a/.github/workflows/service-containers-on-runner.yml b/.github/workflows/service-containers-on-runner.yml new file mode 100644 index 0000000..b695869 --- /dev/null +++ b/.github/workflows/service-containers-on-runner.yml @@ -0,0 +1,55 @@ +# Example of using a service container for a job that runs directly on the runner + +# This workflow contains a job that uses a service container +# Containers must run in Linux based operating systems + +# https://docs.github.com/en/actions/using-containerized-services/creating-redis-service-containers + +name: Service Container Example on Runner + +# Manually triggered +on: workflow_dispatch + +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@v3 + + # 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: + # The hostname used to communicate with the Redis service container + REDIS_HOST: localhost + # The default Redis port + REDIS_PORT: 6379 From 001242974a591092d73047be10db11b6a0604d0a Mon Sep 17 00:00:00 2001 From: Fred Gohsman Date: Fri, 20 Jan 2023 07:48:10 -0500 Subject: [PATCH 3/6] Create client.js --- client.js | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 client.js diff --git a/client.js b/client.js new file mode 100644 index 0000000..c76f890 --- /dev/null +++ b/client.js @@ -0,0 +1,31 @@ +const redis = require("redis"); + +// Creates a new Redis client +// If REDIS_HOST is not set, the default host is localhost +// If REDIS_PORT is not set, the default port is 6379 +const redisClient = redis.createClient({ + host: process.env.REDIS_HOST, + port: process.env.REDIS_PORT +}); + +redisClient.on("error", function(err) { + console.log("Error " + err); +}); + +// Sets the key "octocat" to a value of "Mona the octocat" +redisClient.set("octocat", "Mona the Octocat", redis.print); +// Sets a key to "octocat", field to "species", and "value" to "Cat and Octopus" +redisClient.hset("species", "octocat", "Cat and Octopus", redis.print); +// Sets a key to "octocat", field to "species", and "value" to "Dinosaur and Octopus" +redisClient.hset("species", "dinotocat", "Dinosaur and Octopus", redis.print); +// Sets a key to "octocat", field to "species", and "value" to "Cat and Robot" +redisClient.hset(["species", "robotocat", "Cat and Robot"], redis.print); +// Gets all fields in "species" key + +redisClient.hkeys("species", function (err, replies) { + console.log(replies.length + " replies:"); + replies.forEach(function (reply, i) { + console.log(" " + i + ": " + reply); + }); + redisClient.quit(); +}); From 8a8b1a5b4253affae0ceec0a0de9fa9d92273ecb Mon Sep 17 00:00:00 2001 From: Fred Gohsman Date: Fri, 20 Jan 2023 08:01:33 -0500 Subject: [PATCH 4/6] Update README.md --- README.md | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index cd9a2c0..1d42776 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,33 @@ -# sample-docker-action -A sample GitHub Action that uses a docker container +# Samples using Docker in a GitHub Actions workflow +This repository contains three (3) sample workflows that demonstrate using docker containers in your GitHub Actions workflow. + +Workflow Name | File Name | Description +--- | --- | --- +Docker Action Example | .github/workflows/docker-action-example.yml | Runs a docker action +Service Container Example on Runner | .github/workflows/service-containers-on-runner.yml | Runs a job directly on the runner and leverages a service container +Service Container Example with Job Container | .github/workflows/service-containers-with-job-container.yml | Runs a job within a container and leverages a service container + +### Workflow examples leveraging a service container + +When you run this workflow, you should see the following output in the "Connect to Redis" step confirming you created the Redis client and added data: + +``` +Reply: OK +Reply: 1 +Reply: 1 +Reply: 1 +3 replies: + 0: octocat + 1: dinotocat + 2: robotocat +``` + + +### client.js +This script creates a Redis client and populates the client with some placeholder data. The script then prints the values stored in the Redis client to the terminal. Your script can use any language you'd like, but this example uses Node.js and the redis npm module. For more information, see the [npm redis module](https://www.npmjs.com/package/redis). + +You can modify client.js to include any Redis operations needed by your workflow. In this example, the script creates the Redis client instance, adds placeholder data, then retrieves the data. + +The script creates a new Redis client using the createClient method, which accepts a host and port parameter. The script uses the REDIS_HOST and REDIS_PORT environment variables to set the client's IP address and port. If host and port are not defined, the default host is localhost and the default port is 6379. + +The script uses the set and hset methods to populate the database with some keys, fields, and values. To confirm that the Redis client contains the data, the script prints the contents of the database to the console log. From 89886e8f1a89dc84f5ea1482c3f5454492266687 Mon Sep 17 00:00:00 2001 From: Fred Gohsman Date: Fri, 20 Jan 2023 08:04:32 -0500 Subject: [PATCH 5/6] Update service-containers-on-runner.yml --- .github/workflows/service-containers-on-runner.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/service-containers-on-runner.yml b/.github/workflows/service-containers-on-runner.yml index b695869..4e8a91b 100644 --- a/.github/workflows/service-containers-on-runner.yml +++ b/.github/workflows/service-containers-on-runner.yml @@ -7,8 +7,8 @@ name: Service Container Example on Runner -# Manually triggered -on: workflow_dispatch +# Manually triggered and on Pull Requests +on: [workflow_dispatch, pull_request] jobs: # Label of the runner job From 2b47e7be89eb6fe8a2967d3bd9e833fde90d17b7 Mon Sep 17 00:00:00 2001 From: Fred Gohsman Date: Fri, 20 Jan 2023 08:04:46 -0500 Subject: [PATCH 6/6] Update service-containers-with-job-container.yml --- .github/workflows/service-containers-with-job-container.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/service-containers-with-job-container.yml b/.github/workflows/service-containers-with-job-container.yml index c39a35e..c60e115 100644 --- a/.github/workflows/service-containers-with-job-container.yml +++ b/.github/workflows/service-containers-with-job-container.yml @@ -8,8 +8,8 @@ name: Service Container Example with Job Container -# Manually triggered -on: workflow_dispatch +# Manually triggered and on Pull Requests +on: [workflow_dispatch, pull_request] jobs: # Label of the container job