diff --git a/.github/workflows/mysql-service.yml b/.github/workflows/mysql-service.yml new file mode 100644 index 0000000..e724488 --- /dev/null +++ b/.github/workflows/mysql-service.yml @@ -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 + # 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 diff --git a/mysql/client.js b/mysql/client.js new file mode 100644 index 0000000..35f6108 --- /dev/null +++ b/mysql/client.js @@ -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() +}); diff --git a/mysql/package-lock.json b/mysql/package-lock.json new file mode 100644 index 0000000..ff069df --- /dev/null +++ b/mysql/package-lock.json @@ -0,0 +1,79 @@ +{ + "requires": true, + "lockfileVersion": 1, + "dependencies": { + "bignumber.js": { + "version": "9.0.0", + "resolved": "https://registry.npmjs.org/bignumber.js/-/bignumber.js-9.0.0.tgz", + "integrity": "sha512-t/OYhhJ2SD+YGBQcjY8GzzDHEk9f3nerxjtfa6tlMXfe7frs/WozhvCNoGvpM0P3bNf3Gq5ZRMlGr5f3r4/N8A==" + }, + "core-util-is": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz", + "integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=" + }, + "inherits": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", + "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==" + }, + "isarray": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", + "integrity": "sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE=" + }, + "mysql": { + "version": "2.18.1", + "resolved": "https://registry.npmjs.org/mysql/-/mysql-2.18.1.tgz", + "integrity": "sha512-Bca+gk2YWmqp2Uf6k5NFEurwY/0td0cpebAucFpY/3jhrwrVGuxU2uQFCHjU19SJfje0yQvi+rVWdq78hR5lig==", + "requires": { + "bignumber.js": "9.0.0", + "readable-stream": "2.3.7", + "safe-buffer": "5.1.2", + "sqlstring": "2.3.1" + } + }, + "process-nextick-args": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz", + "integrity": "sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag==" + }, + "readable-stream": { + "version": "2.3.7", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.7.tgz", + "integrity": "sha512-Ebho8K4jIbHAxnuxi7o42OrZgF/ZTNcsZj6nRKyUmkhLFq8CHItp/fy6hQZuZmP/n3yZ9VBUbp4zz/mX8hmYPw==", + "requires": { + "core-util-is": "~1.0.0", + "inherits": "~2.0.3", + "isarray": "~1.0.0", + "process-nextick-args": "~2.0.0", + "safe-buffer": "~5.1.1", + "string_decoder": "~1.1.1", + "util-deprecate": "~1.0.1" + } + }, + "safe-buffer": { + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" + }, + "sqlstring": { + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/sqlstring/-/sqlstring-2.3.1.tgz", + "integrity": "sha1-R1OT/56RR5rqYtyvDKPRSYOn+0A=" + }, + "string_decoder": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz", + "integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==", + "requires": { + "safe-buffer": "~5.1.0" + } + }, + "util-deprecate": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", + "integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=" + } + } +} diff --git a/mysql/package.json b/mysql/package.json new file mode 100644 index 0000000..49ed5f9 --- /dev/null +++ b/mysql/package.json @@ -0,0 +1,5 @@ +{ + "dependencies": { + "mysql": "^2.18.1" + } +}