Report migration results #51
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: "Report migration results" | |
on: | |
workflow_dispatch: | |
inputs: | |
environment: | |
description: GitHub environment to run from | |
type: choice | |
default: production | |
options: | |
- production | |
required: true | |
schedule: | |
- cron: "0 8 * * *" # Run at 8am, daily | |
jobs: | |
report_results: | |
name: "Report migration results" | |
environment: production | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v4 | |
name: Checkout Code | |
- name: Login | |
uses: azure/login@v2 | |
with: | |
creds: ${{ secrets.AZURE_CREDENTIALS }} | |
- uses: DFE-Digital/github-actions/set-kubelogin-environment@master | |
with: | |
azure-credentials: ${{ secrets.AZURE_CREDENTIALS }} | |
- name: Install kubectl | |
uses: DFE-Digital/github-actions/set-kubectl@master | |
- name: Set AKS credentials (migration) | |
shell: bash | |
run: make ci migration get-cluster-credentials | |
- name: Get migration duration | |
shell: bash | |
run: | | |
DURATION=$(kubectl -n cpd-production exec -ti --tty deployment/npq-registration-migration-web -- /bin/sh -c "cd /app && bundle exec rails runner \"puts ActiveSupport::Duration.build((Migration::DataMigration.failed.maximum(:completed_at) - Migration::DataMigration.failed.minimum(:started_at)).to_i).inspect\"") | |
echo "Duration: $DURATION" | |
echo "DURATION=$DURATION" >> $GITHUB_ENV | |
- name: Check for data migration failures | |
shell: bash | |
run: | | |
ANY_FAILURES=$(kubectl -n cpd-production exec -ti --tty deployment/npq-registration-migration-web -- /bin/sh -c "cd /app && bundle exec rails runner \"puts Migration::DataMigration.failed.any?\"") | |
echo "Failed data migrations? $ANY_FAILURES" | |
if [ "$ANY_FAILURES" = "true" ]; then | |
echo "Data migration failures detected. Failing the step." | |
exit 1 | |
fi | |
- uses: Azure/get-keyvault-secrets@v1 | |
if: always() | |
id: key-vault-secrets | |
with: | |
keyvault: s189p01-cpdnpq-pd-app-kv | |
secrets: "SLACK-WEBHOOK" | |
- name: Notify Slack channel of success | |
uses: rtCamp/action-slack-notify@v2 | |
env: | |
SLACK_USERNAME: CI Deployment | |
SLACK_TITLE: 🎉 Data migration success! (🕒 ${{ env.DURATION }}) | |
SLACK_MESSAGE: No failures were detected in the nightly data migration | |
SLACK_WEBHOOK: ${{ steps.key-vault-secrets.outputs.SLACK-WEBHOOK }} | |
SLACK_COLOR: success | |
SLACK_FOOTER: Sent from report-migration-results workflow | |
- name: Retrieve failure details | |
if: failure() | |
shell: bash | |
run: | | |
FAILURES=$(kubectl -n cpd-production exec -ti --tty deployment/npq-registration-migration-web -- /bin/sh -c "cd /app && bundle exec rails runner \"puts Migration::DataMigration.unscope(:order).failed.group(:model).sum(:failure_count).map { |k, v| %{#{k.humanize}: #{ActiveSupport::NumberHelper.number_to_delimited(v)}} }.join('\\n')\"") | |
echo "Failed data migrations:\n $FAILURES" | |
echo "FAILURES=$FAILURES" >> $GITHUB_ENV | |
- name: Notify Slack channel of failures | |
if: failure() | |
uses: rtCamp/action-slack-notify@v2 | |
env: | |
SLACK_USERNAME: CI Deployment | |
SLACK_TITLE: 🚨 Data migration failure! (🕒 ${{ env.DURATION }}) | |
SLACK_MESSAGE: ${{ env.FAILURES }} | |
ENABLE_ESCAPES: true | |
SLACK_WEBHOOK: ${{ steps.key-vault-secrets.outputs.SLACK-WEBHOOK }} | |
SLACK_COLOR: failure | |
SLACK_FOOTER: Sent from report-migration-results workflow |