Alembic with Github Action

208 Views Asked by At

I am new to GitHub Actions.

I am trying to deploy Python FastAPI with SQLAlchemy and Alembic for database migration on GCP with App Engine and SQL Postgres.

My problem is with getting secrets with the DB connection string inserted into env.py files. Here is my set-up:

  • Github action secrets: DEV_DATABASE_URL: 'postgres+pg8000://....'
  • Workflow YML file:
      - name: Run Alembic migrations
          run: alembic upgrade head
          env:
            DATABASE_URL: ${{ secrets.DEV_DATABASE_URL }}
  • Alembic.ini file:
      dotenv_config = dotenv_values(".env")
      connection_string = dotenv_config["DATABASE_URL"]

Error, I am getting

connection_string = dotenv_config["DATABASE_URL"]
KeyError: 'DATABASE_URL'
1

There are 1 best solutions below

1
Ani bairy On

I dont think your yaml creates a .env file. try this in Alembic.ini

import os
connection_string = os.getenv('DATABASE_URL')

if you have multiple variables

env_dict = os.environ