This guide provides instructions for setting up a GitHub Action to automatically run database migrations whenever you push changes to your repository.
To set up the GitHub Action, ensure you have the following:
This guide covers different methods for executing migrations:
Follow these steps to configure the GitHub environment and secrets:
Settings
tab.Environments
menu on the left.New environment
button or choose an existing environment (we will use the Production
environment in this guide).Configure environment
button.Production
environment to the main
branch.Add secret
button to define the required secrets.To run migrations via SSH to a remote server, set up the following secrets:
SSH_PRIVATE_KEY
: The private key for SSH authentication.SSH_HOST
: The hostname or IP address of the remote server.SSH_PORT
: The port for SSH authentication.SSH_USER_NAME
: The username for SSH authentication.Use the sample GitHub Action workflow file located at .github/workflows/run_alembic_migrations.yml
. Set the SSH_PROJECT_DIR
variable to the path of your project directory on the remote server.
The action scripts are located in the .github/scripts
directory:
run_docker_migration.sh
: Executes migrations using Docker.run_src_migration.sh
: Executes migrations using the source code.Note:
sh $SSH_PROJECT_DIR/.github/scripts/run_src_migration.sh
from the workflow file.sh $SSH_PROJECT_DIR/.github/scripts/run_docker_migration.sh
.To run migrations on a remote database using connection strings, configure the following secret:
MIGRATION_DATABASE_URL
: The connection string for your database, formatted as postgresql+psycopg2://username:password@host:port/database
. For example:
postgresql+psycopg2://postgres:postgres@db:5432
If you’re using a private database, you may need to establish a VPN or SSH tunnel for access.
For migrations using the Cohere Toolkit’s API migrations endpoint, set up these secrets:
MIGRATION_API_ENDPOINT
: The URL for the Cohere Toolkit API migrations endpoint, formatted as https://<Your Cohere Toolkit hostname>:<API port, 8000 by default>/migrate
.MIGRATION_API_TOKEN
: The API token for authentication with the Cohere Toolkit API migrations endpoint, retrievable from the Toolkit’s .env
file.