From 6d52273a718a7f6cd0e723b4cc0026cf25d85bdb Mon Sep 17 00:00:00 2001 From: Paul Craig Date: Mon, 18 Nov 2024 19:18:57 -0500 Subject: [PATCH] Add the migrate workflow --- .github/workflows/django_gcp_migrate.yml | 64 ++++++++++++++++++++ .github/workflows/main.yml | 10 ++- .github/workflows/old_django_gcp_migrate.yml | 3 + 3 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/django_gcp_migrate.yml diff --git a/.github/workflows/django_gcp_migrate.yml b/.github/workflows/django_gcp_migrate.yml new file mode 100644 index 00000000..1a672107 --- /dev/null +++ b/.github/workflows/django_gcp_migrate.yml @@ -0,0 +1,64 @@ +name: "Migrate Django database in Cloud Run" + +on: + workflow_call: # Allow this workflow to be called by other workflows + secrets: + DATABASE_URL: + required: true + GCP_CLOUD_CONNECTION_NAME: + required: true + GCP_IAM_SA_CREDENTIALS: + required: true + +jobs: + migrate: + runs-on: "ubuntu-latest" + defaults: + run: + working-directory: ./bloom_nofos + + permissions: + contents: "read" + id-token: "write" + + steps: + - name: "Checkout" + uses: "actions/checkout@v4" + + - name: "Set up Python" + uses: actions/setup-python@v4 + with: + python-version: "3.11.9" + + - name: "Set up poetry" + uses: Gr1N/setup-poetry@v8 + + - uses: actions/cache@v4 + with: + path: ~/.cache/pypoetry/virtualenvs + key: ${{ runner.os }}-poetry-${{ hashFiles('poetry.lock') }} + + - run: poetry --version + + - name: Install poetry dependencies + run: poetry install + + # Configure auth with Service Account with permissions to build and deploy on Google Cloud + - id: "auth" + name: "Authenticate to Google Cloud" + uses: "google-github-actions/auth@v2" + with: + credentials_json: ${{ secrets.GCP_IAM_SA_CREDENTIALS }} + + # BEGIN - Django migration (if needed) + # sudo mv cloud-sql-proxy /usr/local/bin/ + - name: "Install Cloud SQL Auth Proxy" + run: | + curl -o cloud-sql-proxy https://storage.googleapis.com/cloud-sql-connectors/cloud-sql-proxy/v2.14.0/cloud-sql-proxy.linux.amd64 + chmod +x cloud-sql-proxy + ./cloud-sql-proxy ${{ secrets.GCP_CLOUD_CONNECTION_NAME }} & + + - name: Run migration + env: + DATABASE_URL: ${{ secrets.DATABASE_URL }} + run: poetry run python manage.py migrate --plan diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 4162acf9..e8692b62 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -11,8 +11,16 @@ on: - main jobs: - run-ci: + ci: uses: ./.github/workflows/django_ci.yml # Call the CI workflow with: branch: ${{ github.ref_name }} secrets: inherit + + migrate: + needs: ci + uses: ./.github/workflows/django_gcp_migrate.yml + secrets: + DATABASE_URL: ${{ secrets.DATABASE_URL }} + GCP_CLOUD_CONNECTION_NAME: ${{ secrets.GCP_CLOUD_CONNECTION_NAME }} + GCP_IAM_SA_CREDENTIALS: ${{ secrets.GCP_IAM_SA_CREDENTIALS }} diff --git a/.github/workflows/old_django_gcp_migrate.yml b/.github/workflows/old_django_gcp_migrate.yml index dfe0f14f..e65a784d 100644 --- a/.github/workflows/old_django_gcp_migrate.yml +++ b/.github/workflows/old_django_gcp_migrate.yml @@ -58,6 +58,9 @@ jobs: chmod +x cloud-sql-proxy ./cloud-sql-proxy ${{ secrets.GCP_CLOUD_CONNECTION_NAME }} & + - name: Install poetry dependencies + run: poetry install + - name: Run migration env: DATABASE_URL: ${{ secrets.DATABASE_URL }}