How to Gate a Deploy on Migration Success
The deploy must not proceed if migrations fail; wire the rollout to depend on the migration step so a failure halts the release.
If migrations fail but the app still deploys, new code runs against an old schema and errors at runtime. Put migrations in a step or job that the rollout depends on, and let a non-zero exit stop the pipeline before any traffic hits the new version.
Same job: order matters
.github/workflows/ci.yml
steps:
- name: Migrate (must pass)
run: npx prisma migrate deploy
env:
DATABASE_URL: ${{ secrets.DATABASE_URL }}
- name: Roll out (only runs if migrate succeeded)
run: ./deploy.shSeparate jobs: use needs
.github/workflows/ci.yml
jobs:
migrate:
runs-on: ubuntu-latest
steps:
- run: npx prisma migrate deploy
deploy:
needs: migrate # skipped if migrate fails
runs-on: ubuntu-latest
steps:
- run: ./deploy.shGotchas
- A step fails the job by default; do not add
continue-on-errorto the migrate step. - A downstream job is skipped when its
needs:target fails, which is what you want here.
Related guides
How to Run Migrations as a Separate CI Job With the Right CredsRun migrations in a dedicated CI job with a scoped migration role and secret, separate from the app deploy, s…
How to Run Database Migrations in the Deploy PipelineRun database migrations as a dedicated deploy step in CI/CD, deciding whether to migrate before or after the…