Skip to content
This repository was archived by the owner on Oct 22, 2021. It is now read-only.
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
68 changes: 68 additions & 0 deletions .github/workflows/mysql-service.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
name: MySQL Service Example

on:
push:
branches:
- main
pull_request:
branches:
- main

defaults:
run:
working-directory: ./mysql

jobs:
container-job:
runs-on: ubuntu-latest

# runs all of the steps inside the specified container rather than on the VM host.
# Because of this the network configuration changes from host based network to a container network.
container:
image: node:10.16-jessie

services:
mysql:
image: mysql:5.7
env:
MYSQL_ALLOW_EMPTY_PASSWORD: yes
ports:
- 3306:3306
# needed because the mysql container does not provide a healthcheck
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=10s --health-retries=10

steps:
- uses: actions/checkout@v2
- run: npm ci
- run: node client.js
env:
# use mysql for the host here because we have specified a container for the job.
# If we were running the job on the VM this would be localhost
MYSQL_HOST: mysql
MYSQL_PORT: ${{ job.services.mysql.ports[3306] }}

# Runs all steps on the VM
# The service containers will use host port binding instead of container networking so you access them via localhost rather than the service name
vm-job:
runs-on: ubuntu-latest

services:
mysql:
image: mysql:5.7
env:
MYSQL_ALLOW_EMPTY_PASSWORD: yes
ports:
# will assign a random free host port
- 3306/tcp
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- 3306/tcp
- 3306:3306

# needed because the mysql container does not provide a healthcheck
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=10s --health-retries=10

steps:
- uses: actions/checkout@v2
- run: npm ci
- run: node client.js
env:
# use localhost for the host here because we are running the job on the VM.
# If we were running the job on in a container this would be mysql
MYSQL_HOST: localhost
MYSQL_PORT: ${{ job.services.mysql.ports[3306] }} # get randomly assigned published port
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
MYSQL_PORT: ${{ job.services.mysql.ports[3306] }} # get randomly assigned published port
MYSQL_PORT: 3306

17 changes: 17 additions & 0 deletions mysql/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const mysql = require('mysql');

const mysqlConnection = mysql.createConnection({
host: process.env.MYSQL_HOST,
port: process.env.MYSQL_PORT,
user: 'root',
password: '',
database: 'mysql'
});

mysqlConnection.connect();

mysqlConnection.query('SELECT NOW()', (err, res) => {
if (err) throw err
console.log(res)
mysqlConnection.end()
});
79 changes: 79 additions & 0 deletions mysql/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions mysql/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"mysql": "^2.18.1"
}
}