通过 Github 进行的 Nodejs 操作测试不起作用,因为需要明确提供方言
Nodejs actions testing via Github not working, because Dialect needs to be explicitly supplied
所以对于这个项目,我需要自动化我的测试。我可以 运行 在本地毫无问题地进行测试;他们都通过了。但是一旦我发出拉取请求,测试就会在 Github 操作中失败。我想确保测试也通过 Github 操作。
Github 操作出错
我收到以下错误:
node.js.yml
我使用以下工作流程来 运行 测试:
# 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
我在本地也遇到过这个问题,但我找到了解决它的方法。但是,在 Github 上,我不确定为什么会导致此问题。我已经在存储库机密中添加了 .env 文件中的所有值,但看起来它没有使用它们,因为测试仍然要求方言。
package.json
我运行下面的脚本来测试系统
"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
这是我在本地解决问题的地方,但它也可能导致 Github 操作出现问题。因为我需要添加或删除 {path: '../.env'}
部分才能使代码正常工作。
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"
}
};
如果您想查看更多代码,可以在此处找到存储库:https://github.com/s3-db01/ISAAC-floor-back-end
我使用 Docker 启动我的 MySQL 数据库。
编辑:
我尝试对方言进行硬编码,但 Github 操作没有结果。我使用以下代码对方言进行硬编码:
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'
});
}
不确定我是否完全理解你的问题,但我认为如果你在 env: 下添加 DIALECT(而不是 DB_DIALECT)在你的工作流 yml 中你应该没问题。
至少,sequelize-cli GitHub 测试操作是这样设置的;
https://github.com/sequelize/cli/blob/main/.github/workflows/ci.yml
也许试试这个?这是 Sequelize 自己的 GitHub 页面
的版本
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 }}
所以对于这个项目,我需要自动化我的测试。我可以 运行 在本地毫无问题地进行测试;他们都通过了。但是一旦我发出拉取请求,测试就会在 Github 操作中失败。我想确保测试也通过 Github 操作。
Github 操作出错
我收到以下错误:
node.js.yml
我使用以下工作流程来 运行 测试:
# 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
我在本地也遇到过这个问题,但我找到了解决它的方法。但是,在 Github 上,我不确定为什么会导致此问题。我已经在存储库机密中添加了 .env 文件中的所有值,但看起来它没有使用它们,因为测试仍然要求方言。
package.json
我运行下面的脚本来测试系统
"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
这是我在本地解决问题的地方,但它也可能导致 Github 操作出现问题。因为我需要添加或删除 {path: '../.env'}
部分才能使代码正常工作。
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"
}
};
如果您想查看更多代码,可以在此处找到存储库:https://github.com/s3-db01/ISAAC-floor-back-end
我使用 Docker 启动我的 MySQL 数据库。
编辑:
我尝试对方言进行硬编码,但 Github 操作没有结果。我使用以下代码对方言进行硬编码:
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'
});
}
不确定我是否完全理解你的问题,但我认为如果你在 env: 下添加 DIALECT(而不是 DB_DIALECT)在你的工作流 yml 中你应该没问题。
至少,sequelize-cli GitHub 测试操作是这样设置的; https://github.com/sequelize/cli/blob/main/.github/workflows/ci.yml
也许试试这个?这是 Sequelize 自己的 GitHub 页面
的版本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 }}