首页 > 解决方案 > Github Actions - 未处理的拒绝 SequelizeConnectionError:启动数据包中未指定 PostgreSQL 用户名

问题描述

我为 CI/CD 设置了一个 Github 操作,但我在数据库连接方面遇到了困难。
由于 sequelize 允许指定另一个数据库 url(使用“use_env_variable”选项),我像这样更新了我的配置:

{
  "test": {
    "dialect": "postgres",
    "schema": "exercises_library",
    "logging": false,
    "use_env_variable": "DATABASE_URL"
  },
  "production": {
    "dialect": "postgres",
    "schema": "exercises_library",
    "logging": false,
    "use_env_variable": "DATABASE_URL"
  }
}

所以我在 Github Actions 中编写了我的工作流程:

name: Source Code CI/CD

on:
  push:
    branches:
      - master
  pull_request:
    branches:
      - master

jobs:
  ci:
    runs-on: ubuntu-latest
    container:
      image: node:12
    services:
# More explanation about that here :
# https://github.com/actions/example-services/blob/master/.github/workflows/postgres-service.yml
      postgres:
        image: postgres:12-alpine
        env:
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: jy95
          POSTGRES_DB: sourcecode
        ports: ["5432:5432"]
        options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
    env:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: jy95
      POSTGRES_DB: sourcecode
      # use postgres 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
      POSTGRES_HOST: postgres
    steps:
      - uses: actions/checkout@v2
      - name: Set DB Port
        env:
          POSTGRES_PORT: ${{ job.services.postgres.ports[5432] }}
        run: |
          echo "::set-env name=POSTGRES_PORT::$POSTGRES_PORT"
      - name: Install
        run: |
          npm install -g npm@latest
          npm install -g codecov
          npm ci
      - name: Tests
        env:
          DATABASE_URL: '${{ env.POSTGRES_HOST }}://${{ env.POSTGRES_USER}}:${{env.POSTGRES_PASSWORD}}:${{env.POSTGRES_PORT}}/${{env.POSTGRES_DB}}'
        run: |
          npm test
      - name: Upload code coverage
        run: |
          npx codecov
  cd:
    runs-on: ubuntu-latest
    needs: ci

    steps:
      - uses: actions/checkout@v2
      - name: Docker login
        run: docker login -u ${{ secrets.DOCKER_USER }} -p ${{ secrets.DOCKER_PASSWORD }}
      - name: Build
        run: docker build -t sourcecode_api .
      - name: Tags
        run: |
          docker tag sourcecode_api ${{ secrets.DOCKER_USER }}/sourcecode_api:${{ github.sha }}
          docker tag sourcecode_api ${{ secrets.DOCKER_USER }}/sourcecode_api:latest
      - name: Push
        run: |
          docker push ${{ secrets.DOCKER_USER }}/sourcecode_api:${{ github.sha }}
          docker push ${{ secrets.DOCKER_USER }}/sourcecode_api:latest

我的日志中出现此错误:

错误

我看到 env 变量设置正确:

配置

我已经阅读了很多资源(关于 Github 问题或 Stackoverflow),但没有一个能解决我的问题。

提前致谢

标签: node.jspostgresqlsequelize.jsgithub-actions

解决方案


这是我的错字 - 如果它可以帮助任何人:

DATABASE_URL: 'postgresql://${{ env.POSTGRES_USER }}:${{ env.POSTGRES_PASSWORD }}@${{ env.POSTGRES_HOST }}:${{env.POSTGRES_PORT}}/${{env.POSTGRES_DB}}'

推荐阅读