From cb701800a3d86fd5aee577f5aa70ae9e8f7efdd4 Mon Sep 17 00:00:00 2001 From: carlpartridge Date: Fri, 21 Feb 2025 09:28:52 -0500 Subject: [PATCH] BCDA-8760: Add migrate db workflow --- .github/workflows/migrate-db.yml | 109 +++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 .github/workflows/migrate-db.yml diff --git a/.github/workflows/migrate-db.yml b/.github/workflows/migrate-db.yml new file mode 100644 index 0000000..f98b50f --- /dev/null +++ b/.github/workflows/migrate-db.yml @@ -0,0 +1,109 @@ +name: Migrate DB + +on: + workflow_call: + inputs: + release_version: + description: 'Release version (or branch name)' + required: true + type: string + env: + description: 'Environment you want to migrate for (dev, test, sbx, prod)' + required: true + type: string + workflow_dispatch: + inputs: + release_version: + description: 'Release version (or branch name)' + required: true + type: string + env: + description: 'Environment you want to migrate for (dev, test, sbx, prod)' + required: true + default: dev + type: choice + options: + - dev + - test + - sbx + - prod + +permissions: + id-token: write + contents: read + +jobs: + migrate_db: + name: Migrate DB + runs-on: self-hosted + steps: + - name: Checkout SSAS + uses: actions/checkout@v4 + with: + repository: CMSgov/bcda-ssas-app + ref: ${{ inputs.release_version }} + - name: Get AWS params + uses: cmsgov/ab2d-bcda-dpc-platform/actions/aws-params-env-action@main + env: + AWS_REGION: ${{ vars.AWS_REGION }} + with: + params: | + DB_URL=/bcda/${{ inputs.env }}/api/DATABASE_URL + - name: Get Go + uses: actions/setup-go@v5 + with: + go-version: '>=1.23.6' + - name: Get golang-migrate + run: go install -tags 'postgres' github.com/golang-migrate/migrate/v4/cmd/migrate@latest + - name: Set DB name + if: ${{ inputs.env == 'dev' }} + run: echo "DB_NAME=bcda-dev-rds" >> "$GITHUB_OUTPUT" + - name: Set DB name + if: ${{ inputs.env == 'test' }} + run: echo "DB_NAME=bcda-test-rds" >> "$GITHUB_OUTPUT" + - name: Set DB name + if: ${{ inputs.env == 'sbx' }} + run: echo "DB_NAME=bcda-opensbx-rds-20190311" >> "$GITHUB_OUTPUT" + - name: Set DB name + if: ${{ inputs.env == 'prod' }} + run: echo "DB_NAME=bcda-prod-rds-20190201" >> "$GITHUB_OUTPUT" + - uses: aws-actions/configure-aws-credentials@v3 + with: + aws-region: ${{ vars.AWS_REGION }} + role-to-assume: arn:aws:iam::${{ secrets.ACCOUNT_ID }}:role/delegatedadmin/developer/bcda-${{ inputs.env }}-github-actions + - name: Migrate DB + env: + PGSSLMODE: require + # CAUTION: if changing the script below, validate that sensitive information is not printed in the workflow + run: | + HOST=$(aws rds describe-db-instances --db-instance-identifier ${{ env.DB_NAME }} 2>&1 | jq -r '.DBInstances[0].Endpoint.Address' 2>&1) + CONNECTION_URL=$(echo $DB_URL 2>&1 | sed -E "s/@.*\/bcda/\@$HOST\/bcda/" 2>&1) + migrate -database ${CONNECTION_URL} -path db/migrations/bcda up + - name: Success Alert + if: ${{ success() }} + uses: slackapi/slack-github-action@v2.0.0 + with: + method: chat.postMessage + token: ${{ secrets.SLACK_BOT_TOKEN }} + # Sends to bcda-deploy + payload: | + channel: "C03S23MJFJS" + attachments: + - color: good + text: "SUCCESS: SSAS DB Migrations in ${{ inputs.env }} (run: <${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}|${{ github.run_id }})>" + mrkdown_in: + - text + - name: Failure Alert + if: ${{ failure() }} + uses: slackapi/slack-github-action@v2.0.0 + with: + method: chat.postMessage + token: ${{ secrets.SLACK_BOT_TOKEN }} + # Sends to bcda-alerts + payload: | + channel: "C034CFU945C" + attachments: + - color: danger + text: "FAILURE: SSAS DB Migrations in ${{ inputs.env }} (run: <${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}|${{ github.run_id }})>" + mrkdown_in: + - text