Search code examples
node.jsenvironment-variablessequelize.jsgithub-actionssequelize-cli

Nodejs actions testing via Github not working, because Dialect needs to be explicitly supplied


So for this project I need to automate my testing. I can run the tests locally without a problem; they all pass. But as soon as I do a pull request, the tests fail in Github actions. I want to make sure the tests also pass on Github actions.

Error on Github actions

I get the following error:

enter image description here

node.js.yml

I use the following workflow to run the tests:

# This workflow will do a clean install of node dependencies, cache/restore them, build 

the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: Node.js CI

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

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/

    steps:
    - uses: actions/checkout@v2
    - name: Use Node.js ${{ matrix.node-version }}
      env: 
        DB_HOST: ${{ secrets.DB_HOST }}
        DB_USER: ${{ secrets.DB_USER }}
        DB_PASSWORD: ${{ secrets.DB_PASSWORD }}
        DB_DATABASE: ${{ secrets.DB_DATABASE }}
        DB_PORT: ${{ secrets.DB_PORT }}
        DB_DIALECT: ${{ secrets.DB_DIALECT }}
        PORT: ${{ secrets.PORT }}
        DB_DATABASE_TEST: ${{ secrets.DB_DATABASE_TEST }}
      uses: actions/setup-node@v2
      with:
        node-version: ${{ matrix.node-version }}
        cache: 'npm'
    - run: npm ci
    - run: npm run build --if-present
    - run: npm test

I've had this problem also locally, but I found a way to fix it. On Github however, I'm not sure why this problem is caused. I've added all the values from the .env file in the repository secrets, but it looks like it's not using them, since the test still asks for the dialect.

package.json

I run the following scripts to test the system

  "scripts": {
    "devStart": "nodemon server.js",
    "migrate": "npx sequelize-cli db:migrate",
    "migrate:reset": "npx sequelize-cli db:migrate:undo:all && npm run migrate",
    "test": "cross-env NODE_ENV=test jest --testTimeout=10000",
    "pretest": "cross-env NODE_ENV=test npm run migrate:reset"
  },

config.js

This is where I solved the problem locally, but it could also cause a problem on Github actions. Since I need to add or remove the {path: '../.env'} part to get the code to work.

require('dotenv').config(); //When migrating include following in config(): {path: '../.env'}
module.exports = {
    development: {
        username: process.env.DB_USER,
        password: process.env.DB_PASSWORD,
        database: process.env.DB_DATABASE,
        host: process.env.DB_HOST,
        port: process.env.DB_PORT,
        dialect: process.env.DB_DIALECT,
    },
    test: {
      username: process.env.DB_USER,
      password: process.env.DB_PASSWORD,
      database: process.env.DB_DATABASE_TEST,
      host: process.env.DB_HOST,
      port: process.env.DB_PORT,
      dialect: process.env.DB_DIALECT,
      },
      "production": {
        "username": "root",
        "password": null,
        "database": "database_production",
        "host": "127.0.0.1",
        "dialect": "mysql"
      }
};

You can find the repository here if you want to see more code: https://github.com/s3-db01/ISAAC-floor-back-end

I use Docker to start my MySQL database(s).

EDIT:

I tried to hard code the dialect, but without result on Github actions. I used the following code to hardcode the dialect:

models/index.js

let sequelize;
if (config.use_env_variable) {
  console.log('Using environment variable');
  //sequelize = new Sequelize(process.env[config.use_env_variable], config);
  sequelize = new Sequelize(process.env[config.use_env_variable], {
    host: 'localhost',
    dialect: 'mysql'
  });
} else {
  console.log('im triggered');
  //sequelize = new Sequelize(config.database, config.username, config.password, config);
  sequelize = new Sequelize(config.database, config.username, config.password, {
    host: 'localhost',
    dialect: 'mysql'
  });
}

Solution

  • Maybe try this? This is the version of Sequelize there own GitHub page

    name: Node.js CI
    
    on:
      push:
        branches: [ main ]
      pull_request:
        branches: [ main ]
    
    jobs:
      build:
    
        runs-on: windows-latest
        env: 
            DB_HOST: ${{ secrets.DB_HOST }}
            DB_USER: ${{ secrets.DB_USER }}
            DB_PASSWORD: ${{ secrets.DB_PASSWORD }}
            DB_DATABASE: ${{ secrets.DB_DATABASE }}
            DB_PORT: ${{ secrets.DB_PORT }}
            DB_DIALECT: mysql
            PORT: ${{ secrets.PORT }}
            DB_DATABASE_TEST: ${{ secrets.DB_DATABASE_TEST }}
            
        strategy:
          fail-fast: false
          matrix:
            node-version: [12.x, 14.x, 16.x]
            # See supported Node.js release schedule at https://nodejs.org/en/about/releases/
    
        steps:
        - uses: actions/checkout@v2
        - name: Use Node.js ${{ matrix.node-version }}
          
          uses: actions/setup-node@v2
          with:
            node-version: ${{ matrix.node-version }}
            cache: 'npm'
        - run: npm install
        - run: npm run build --if-present
        - run: docker-compose up -d ${DB_DIALECT}
        - run: docker run --link ${DIALECT}:db -e CHECK_PORT=${SEQ_PORT::-1} -e CHECK_HOST=db --net cli_default giorgos/takis
        - run: npm test
    
     - run: npm install sequelize@${{ matrix.sequelize-version }}