Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create container workflows #1

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions .github/workflows/service-containers-on-runner.yml
Original file line number Diff line number Diff line change
@@ -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 and on Pull Requests
on: [workflow_dispatch, pull_request]

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
56 changes: 56 additions & 0 deletions .github/workflows/service-containers-with-job-container.yml
Original file line number Diff line number Diff line change
@@ -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 and on Pull Requests
on: [workflow_dispatch, pull_request]

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
35 changes: 33 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
31 changes: 31 additions & 0 deletions client.js
Original file line number Diff line number Diff line change
@@ -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();
});