db-migrate / node-db-migrate

Database migration framework for node
Other
2.32k stars 360 forks source link

Referring to Githhub Secrets from database.json #754

Closed dschinkel closed 3 years ago

dschinkel commented 3 years ago

database.json

{
  "prod": {
    "driver": "pg",
    "addIfNotExists": {
      "ssl": false
    },
    "user": {"ENV": "{{$secrets.DB_USER}}"},
    "password": {"ENV": "${{secrets.DB_PASS}}"},
    "host": {"ENV": "${{secrets.DB_HOST}}"},
    "database": {"ENV": "${{secrets.DB_NAME}}"},
    "port": {"ENV": "${{secrets.DB_PORT}}"},
    "schema": {"ENV": "${{secrets.DB_SCHEMA_NAME}}"}
  },
  "sql-file" : true
}

When I run this during my CI pipeline from my Github Workflow yml, I get no errors: node node_modules/db-migrate/bin/db-migrate up:production -config database.json -e prod

Screen Shot 2021-09-29 at 12 44 42 AM

But when I look at the prod database, I see no migrations table and the migrations have not been applied.

How do I properly reference github secrets? I assumed this would work but it appears not to, and also appears I get no error either way (I expected an error if it can't connect to my DB) to tell me if it can even find these environment variables successfully.

dschinkel commented 3 years ago

Answer

Setup environment variables in your Github Actions workflow yaml that point to your secrets

env: #
  DB_USER: ${{ secrets.DB_USER }}
  DB_PASS: ${{ secrets.DB_PASS }}
  DB_NAME: ${{ secrets.DB_NAME }}
  DB_PORT: ${{ secrets.DB_PORT }}
  DB_SCHEMA_NAME: ${{ secrets.DB_SCHEMA_NAME }}

Have db-migrate pick them up:

  "prod": {
    "driver": "pg",
    "addIfNotExists": {
      "ssl": false
    },
    "user":{"ENV": "DB_USER"},
    "password": {"ENV": "DB_PASS"},
    "host": {"ENV": "DB_HOST"},
    "database": {"ENV": "DB_NAME"},
    "port": {"ENV": "DB_PORT"},
    "schema": {"ENV": "DB_SCHEMA_NAME"}
  },