Browse Source

fix(ci): Fix migration checks regression (#25633)

This fixes a regression introduced in #25499.

When `sentry django makemigrations --check` does not exit with exit code zero, the
conditional checks in the next line will not be evaluated.

This captures the stdout/stderr into a variable and the exit code into another one.
Armen Zambrano G 3 years ago
parent
commit
7b0ced1100

+ 4 - 8
.github/workflows/check-if-migration-is-required.yml

@@ -5,6 +5,8 @@ on:
       # Matches all python files regardless of directory depth.
       - '**.py'
       - requirements*.txt
+      - .github/workflows/check-if-migration-is-required.yml
+      - .github/workflows/scripts/migration-check.sh
 
 jobs:
   main:
@@ -40,15 +42,9 @@ jobs:
         uses: ./.github/actions/setup-sentry
         id: setup
 
-      - name: Check if a migration is required
+      - name: Migration & lockfile checks
         env:
           SENTRY_LOG_LEVEL: ERROR
           PGPASSWORD: postgres
         run: |
-          # Below will exit with non-zero status if model changes are missing migrations
-          sentry django makemigrations --check --dry-run --no-input
-          if [ "$?" == 1 ]; then
-            echo >&2 "::error::Error: Migration required -- to generate a migration, run `sentry django makemigrations -n <some_name> && git add migrations_lockfile.txt`" && exit 1
-          else:
-            echo >&2 "::error::Error: Migration lockfile mismatch -- run `sentry django makemigrations -n <some_name> && git add migrations_lockfile.txt`" && exit 1
-          fi
+          ./.github/workflows/scripts/migration-check.sh

+ 15 - 0
.github/workflows/scripts/migration-check.sh

@@ -0,0 +1,15 @@
+#!/bin/bash
+# This script expects devservices to be running. It's main purpose is to check
+# that migrations and lockfiles are correct
+set -eu
+# This will fail if a migration or the lockfile are missing in the PR
+exit_code=0
+sentry django makemigrations --check --dry-run --no-input || exit_code=$?
+if [ "$exit_code" == 1 ]; then
+    echo -e "::error::Error: Migration required -- to generate a migration, run:\n" \
+        "sentry django makemigrations -n <some_name> && git add migrations_lockfile.txt" >&2
+else
+    echo -e "::error::Error: Migration lockfile mismatch -- run:\n" \
+        "sentry django makemigrations -n <some_name> && git add migrations_lockfile.txt" >&2
+fi
+exit $exit_code