Skip to content

Instantly share code, notes, and snippets.

@Unalo
Created August 16, 2022 11:05
Show Gist options
  • Save Unalo/588f869187b491429c79bb5cb399d09e to your computer and use it in GitHub Desktop.
Save Unalo/588f869187b491429c79bb5cb399d09e to your computer and use it in GitHub Desktop.

This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node

name: NodeJS CI with PostgreSQL

on: push: branches: [ master ] pull_request: branches: [ master ]

jobs: build:

runs-on: ubuntu-latest

strategy:
  matrix:
    node-version: [12.x, 14.x, 16.x]
    # See supported Node.js release schedule at https://nodejs.org/en/about/releases/
services:
  postgres:
    image: postgres:latest
    env: 
      POSTGRES_USER: <your_database_user>
      POSTGRES_PASSWORD: <your_password>
      POSTGRES_DB: <your_database_name>
    ports:
    - 5432:5432
    # Set health checks to wait until postgres has started
    options: >-
        --health-cmd pg_isready
        --health-interval 10s
        --health-timeout 5s
        --health-retries 5
    
steps:
- uses: actions/checkout@v3
- name: Install dependencies
  run: npm install
- name: create postgresql tables
  run: PGPASSWORD=<your_databse_password> psql -h localhost -U <your_databse_user> -d <your_databse_name> -a -f <path_to_sql_script eg - ./table.sql >
- name: run tests with postgresql
  run:
    npm test
  env:
    DATABASE_URL: <your_databse_connection_string>

NB - use pg: "^8.7.1" or above

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment