Hi everyone,
This is a tutorial I am going to walk through how to run docker containers in GitHub Action workflows.
This task is often useful in testing workflows where we need a temporary database to run test cases. we will run a PostgreSQL database in this tutorial.
name: Running Tests
on: [push]
jobs:
Test:
runs-on: ubuntu-latest
services:
postgres:
image: postgres
env:
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
POSTGRES_DB: postgres
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 5432:5432
in the above workflow snippet, we do several things.
1.
on: [push]
will set when to run this workflow. here we have specified push
, so on every push
event, this workflow will run. keep in mind that this is an array so we can use multiple options like pull_request
etc.
2.
jobs:
service-container-demo:
this code snippet will specify our job . service-container-demo
is the name of our job.
3.
runs-on: ubuntu-latest
specifies, on what operating system we need to run this job. here we have specified to run it on ubuntu's latest operating system.
4.
services:
postgres:
here we specify the services we need to run, and the name of the service container. name of the service container is used to access it.
5.
image: postgres
this line will specify what image we need to run. these docker images are fetched from Docker Hub . so we need to specify the respective image name from Docker Hub.
6.
env:
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
POSTGRES_DB: postgres
here we have specified the environment variables we need to provide to the service container. we have provided the password, username, and the default database here, so we can later access the database via those credentials.
7.
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
we have to wait some time till the container is up and running .so to check that we have specified options to periodically check and after it is started proceed to the next steps.
8.
ports:
- 5432:5432
here we have specified the port mapping. 5432
host port
is mapped to 5432
container port.
next, we can define the steps of the workflow. here we can migrate to the service container database, and run the required tests.
# define steps of workflow
steps:
# run steps required in the workflow
- name: Checkout repository
uses: actions/checkout@v2
- name: Set up Node.js 14.x
uses: actions/setup-node@v1
with:
node-version: 14.x
- name: Install dependencies
run: npm install
- name: migrate database
run: node migrate.js
env:
DBURL: "postgres://postgres:postgres@localhost:5432/postgres"
- name: Run the mock tests function
run: node getuser.js
env:
DBURL: postgres://postgres:postgres@localhost:5432/postgres
in above snippet DBURL
is set to postgres://postgres:postgres@localhost:5432/postgres
. it is derived through the configurations we set in the service container earlier. the host is provided as localhost
because the docker and host machine both runs on localhost
. and here, we are passing it to the step, through an environment variable.
database migration
database query
complete workflow file .
example GitHub repo
Top comments (0)