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:
- Admin permissions for the repository where you want to implement the action.
- Access to a database where migrations will be executed.
This guide covers different methods for executing migrations:
- Running migrations using SSH access to a remote server.
- Running migrations on a remote database using connection strings.
- Running migrations on a remote database via the Cohere Toolkit's API migrations endpoint.
Follow these steps to configure the GitHub environment and secrets:
- Navigate to your repository on GitHub.
- Click on the
Settings
tab. - Select the
Environments
menu on the left. - Click on the
New environment
button or choose an existing environment (we will use theProduction
environment in this guide). - Enter the environment name and click on the
Configure environment
button. - (Optional) Set rules for the environment, such as restricting the
Production
environment to themain
branch. - Click on the
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:
- If the Toolkit is deployed on a remote server using Docker, remove the line
sh $SSH_PROJECT_DIR/.github/scripts/run_src_migration.sh
from the workflow file. - If using source code, remove
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 aspostgresql+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 ashttps://<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.
- If all the required secrets are configured, the action will default to the remote database migrations type.
- If certain variables are not set, the migration type will be determined based on the available variables.
- If the script cannot identify a migration type (due to unset variables), it will exit with an error message.