|
| 1 | +name: Production DB table dump |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + inputs: |
| 6 | + table_name: |
| 7 | + description: "Table name to dump (e.g. envelopes)" |
| 8 | + type: string |
| 9 | + required: true |
| 10 | + |
| 11 | +permissions: |
| 12 | + id-token: write |
| 13 | + contents: read |
| 14 | + |
| 15 | +env: |
| 16 | + AWS_REGION: us-east-1 |
| 17 | + EKS_CLUSTER: ce-registry-eks |
| 18 | + S3_DUMP_BUCKET: cer-db-dumps-prod |
| 19 | + PRESIGNED_URL_TTL: 3600 |
| 20 | + |
| 21 | +jobs: |
| 22 | + dump: |
| 23 | + if: ${{ github.repository_owner == 'CredentialEngine' }} |
| 24 | + runs-on: ubuntu-latest |
| 25 | + steps: |
| 26 | + - name: Checkout code |
| 27 | + uses: actions/checkout@v4 |
| 28 | + |
| 29 | + - name: Configure AWS credentials |
| 30 | + uses: aws-actions/configure-aws-credentials@v4 |
| 31 | + with: |
| 32 | + role-to-assume: arn:aws:iam::${{ secrets.AWS_ACCOUNT }}:role/github-oidc-widget |
| 33 | + aws-region: ${{ env.AWS_REGION }} |
| 34 | + |
| 35 | + - name: Install kubectl |
| 36 | + uses: azure/setup-kubectl@v4 |
| 37 | + with: |
| 38 | + version: v1.29.6 |
| 39 | + |
| 40 | + - name: Update kubeconfig |
| 41 | + run: aws eks update-kubeconfig --name "${{ env.EKS_CLUSTER }}" --region "${{ env.AWS_REGION }}" |
| 42 | + |
| 43 | + - name: Validate table name |
| 44 | + run: | |
| 45 | + TABLE="${{ inputs.table_name }}" |
| 46 | + if [[ ! "$TABLE" =~ ^[a-zA-Z_][a-zA-Z0-9_]*$ ]]; then |
| 47 | + echo "Invalid table name: '$TABLE'. Only alphanumeric characters and underscores allowed." >&2 |
| 48 | + exit 1 |
| 49 | + fi |
| 50 | +
|
| 51 | + - name: Create dump job |
| 52 | + id: create_job |
| 53 | + run: | |
| 54 | + TABLE="${{ inputs.table_name }}" |
| 55 | + JOB_NAME=$( |
| 56 | + sed "s/__TABLE_NAME__/${TABLE}/" \ |
| 57 | + terraform/environments/eks/k8s-manifests-prod/db-table-dump-job.yaml | |
| 58 | + kubectl -n credreg-prod create -f - -o name | sed 's|job.batch/||' |
| 59 | + ) |
| 60 | + echo "job_name=${JOB_NAME}" >> "$GITHUB_OUTPUT" |
| 61 | + echo "Launched job: ${JOB_NAME}" |
| 62 | +
|
| 63 | + - name: Wait for job completion |
| 64 | + run: | |
| 65 | + kubectl -n credreg-prod wait \ |
| 66 | + --for=condition=complete \ |
| 67 | + "job/${{ steps.create_job.outputs.job_name }}" \ |
| 68 | + --timeout=30m |
| 69 | +
|
| 70 | + - name: Get presigned URL |
| 71 | + id: presign |
| 72 | + run: | |
| 73 | + S3_KEY=$( |
| 74 | + kubectl -n credreg-prod logs "job/${{ steps.create_job.outputs.job_name }}" | |
| 75 | + grep "^S3_KEY=" | tail -1 | cut -d= -f2- |
| 76 | + ) |
| 77 | + if [ -z "$S3_KEY" ]; then |
| 78 | + echo "Could not parse S3_KEY from job logs" >&2 |
| 79 | + exit 1 |
| 80 | + fi |
| 81 | + PRESIGNED_URL=$( |
| 82 | + aws s3 presign "s3://${{ env.S3_DUMP_BUCKET }}/${S3_KEY}" \ |
| 83 | + --expires-in "${{ env.PRESIGNED_URL_TTL }}" \ |
| 84 | + --region "${{ env.AWS_REGION }}" |
| 85 | + ) |
| 86 | + echo "presigned_url=${PRESIGNED_URL}" >> "$GITHUB_OUTPUT" |
| 87 | +
|
| 88 | + - name: Notify Slack |
| 89 | + if: always() |
| 90 | + env: |
| 91 | + SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} |
| 92 | + RUN_URL: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }} |
| 93 | + run: | |
| 94 | + if [ -z "${SLACK_WEBHOOK_URL}" ]; then |
| 95 | + echo "SLACK_WEBHOOK_URL not set; skipping" |
| 96 | + exit 0 |
| 97 | + fi |
| 98 | +
|
| 99 | + STATUS="${{ job.status }}" |
| 100 | + TABLE="${{ inputs.table_name }}" |
| 101 | + ACTOR="${{ github.actor }}" |
| 102 | +
|
| 103 | + if [ "$STATUS" = "success" ]; then |
| 104 | + PRESIGNED_URL="${{ steps.presign.outputs.presigned_url }}" |
| 105 | + MSG=":white_check_mark: *DB dump ready* | table: \`${TABLE}\` | triggered by: ${ACTOR}\nDownload (expires in 1h): ${PRESIGNED_URL}\n${RUN_URL}" |
| 106 | + else |
| 107 | + MSG=":x: *DB dump failed* | table: \`${TABLE}\` | triggered by: ${ACTOR}\n${RUN_URL}" |
| 108 | + fi |
| 109 | +
|
| 110 | + payload=$(jq -nc --arg text "$MSG" '{text:$text}') |
| 111 | + curl -sS -X POST -H 'Content-type: application/json' --data "$payload" "$SLACK_WEBHOOK_URL" || true |
0 commit comments