|
| 1 | +name: ChatOps Migration Deployment |
| 2 | + |
| 3 | +on: |
| 4 | + issue_comment: |
| 5 | + types: [created] |
| 6 | + |
| 7 | +env: |
| 8 | + BYTEBASE_URL: https://demo.bytebase.com |
| 9 | + BYTEBASE_SERVICE_ACCOUNT: api@service.bytebase.com |
| 10 | + BYTEBASE_SERVICE_ACCOUNT_SECRET: ${{ secrets.BYTEBASE_SERVICE_ACCOUNT_SECRET }} |
| 11 | + BYTEBASE_PROJECT: "projects/hr" |
| 12 | + |
| 13 | +jobs: |
| 14 | + parse-command: |
| 15 | + if: github.event.issue.pull_request && startsWith(github.event.comment.body, '/migrate') |
| 16 | + runs-on: ubuntu-latest |
| 17 | + outputs: |
| 18 | + environment: ${{ steps.parse.outputs.environment }} |
| 19 | + valid-command: ${{ steps.parse.outputs.valid-command }} |
| 20 | + steps: |
| 21 | + - name: Parse migrate command |
| 22 | + id: parse |
| 23 | + run: | |
| 24 | + COMMENT="${{ github.event.comment.body }}" |
| 25 | + echo "Comment: $COMMENT" |
| 26 | +
|
| 27 | + # Extract environment from "/migrate <environment>" |
| 28 | + if [[ $COMMENT =~ ^/migrate[[:space:]]+([a-zA-Z]+) ]]; then |
| 29 | + ENVIRONMENT="${BASH_REMATCH[1]}" |
| 30 | + echo "Parsed environment: $ENVIRONMENT" |
| 31 | + |
| 32 | + # Validate environment |
| 33 | + case $ENVIRONMENT in |
| 34 | + test|prod) |
| 35 | + echo "environment=$ENVIRONMENT" >> $GITHUB_OUTPUT |
| 36 | + echo "valid-command=true" >> $GITHUB_OUTPUT |
| 37 | + echo "✅ Valid environment: $ENVIRONMENT" |
| 38 | + ;; |
| 39 | + *) |
| 40 | + echo "valid-command=false" >> $GITHUB_OUTPUT |
| 41 | + echo "❌ Invalid environment: $ENVIRONMENT" |
| 42 | + ;; |
| 43 | + esac |
| 44 | + else |
| 45 | + echo "valid-command=false" >> $GITHUB_OUTPUT |
| 46 | + echo "❌ Invalid command format" |
| 47 | + fi |
| 48 | +
|
| 49 | + - name: Add reaction to comment |
| 50 | + if: steps.parse.outputs.valid-command == 'true' |
| 51 | + uses: peter-evans/create-or-update-comment@v3 |
| 52 | + with: |
| 53 | + comment-id: ${{ github.event.comment.id }} |
| 54 | + reactions: rocket |
| 55 | + |
| 56 | + - name: Comment on invalid command |
| 57 | + if: steps.parse.outputs.valid-command == 'false' |
| 58 | + uses: peter-evans/create-or-update-comment@v3 |
| 59 | + with: |
| 60 | + issue-number: ${{ github.event.issue.number }} |
| 61 | + body: | |
| 62 | + ❌ Invalid migrate command. |
| 63 | +
|
| 64 | + **Usage:** `/migrate <environment>` |
| 65 | + **Valid environments:** `test`, `prod` |
| 66 | +
|
| 67 | + **Example:** `/migrate test` |
| 68 | + reactions: confused |
| 69 | + |
| 70 | + deploy: |
| 71 | + needs: parse-command |
| 72 | + if: needs.parse-command.outputs.valid-command == 'true' |
| 73 | + runs-on: ubuntu-latest |
| 74 | + environment: ${{ needs.parse-command.outputs.environment }} |
| 75 | + container: |
| 76 | + image: bytebase/bytebase-action:latest |
| 77 | + steps: |
| 78 | + - name: Checkout PR merge commit |
| 79 | + uses: actions/checkout@v4 |
| 80 | + with: |
| 81 | + ref: refs/pull/${{ github.event.issue.number }}/merge |
| 82 | + |
| 83 | + - name: Comment deployment started |
| 84 | + uses: peter-evans/create-or-update-comment@v3 |
| 85 | + with: |
| 86 | + issue-number: ${{ github.event.issue.number }} |
| 87 | + body: | |
| 88 | + 🚀 **Migration deployment started** |
| 89 | +
|
| 90 | + **Environment:** `${{ needs.parse-command.outputs.environment }}` |
| 91 | + **PR:** #${{ github.event.issue.number }} |
| 92 | + **Triggered by:** @${{ github.event.comment.user.login }} |
| 93 | +
|
| 94 | + Deploying database changes... |
| 95 | +
|
| 96 | + - name: Set environment-specific targets |
| 97 | + id: env-config |
| 98 | + run: | |
| 99 | + case "${{ needs.parse-command.outputs.environment }}" in |
| 100 | + test) |
| 101 | + echo "bytebase-targets=instances/test-sample-instance/databases/hr_test" >> $GITHUB_OUTPUT |
| 102 | + echo "bytebase-stage=environments/test" >> $GITHUB_OUTPUT |
| 103 | + ;; |
| 104 | + prod) |
| 105 | + echo "bytebase-targets=instances/prod-sample-instance/databases/hr_prod" >> $GITHUB_OUTPUT |
| 106 | + echo "bytebase-stage=environments/prod" >> $GITHUB_OUTPUT |
| 107 | + ;; |
| 108 | + esac |
| 109 | +
|
| 110 | + - name: Create rollout plan |
| 111 | + id: create-rollout |
| 112 | + env: |
| 113 | + BYTEBASE_TARGETS: ${{ steps.env-config.outputs.bytebase-targets }} |
| 114 | + FILE_PATTERN: "migrations-semver/*.sql" |
| 115 | + BYTEBASE_OUTPUT: ${{ runner.temp }}/bytebase-metadata.json |
| 116 | + run: | |
| 117 | + echo "Creating rollout plan for ${{ needs.parse-command.outputs.environment }}..." |
| 118 | +
|
| 119 | + bytebase-action rollout \ |
| 120 | + --url=${{ env.BYTEBASE_URL }} \ |
| 121 | + --service-account=${{ env.BYTEBASE_SERVICE_ACCOUNT }} \ |
| 122 | + --service-account-secret=${{ env.BYTEBASE_SERVICE_ACCOUNT_SECRET }} \ |
| 123 | + --project=${{ env.BYTEBASE_PROJECT }} \ |
| 124 | + --file-pattern=${{ env.FILE_PATTERN }} \ |
| 125 | + --targets=${{ env.BYTEBASE_TARGETS }} \ |
| 126 | + --output=${{ env.BYTEBASE_OUTPUT }} |
| 127 | +
|
| 128 | + PLAN=$(jq -r .plan ${{ runner.temp }}/bytebase-metadata.json) |
| 129 | + echo "plan=$PLAN" >> $GITHUB_OUTPUT |
| 130 | +
|
| 131 | + - name: Execute rollout |
| 132 | + env: |
| 133 | + BYTEBASE_TARGET_STAGE: ${{ steps.env-config.outputs.bytebase-stage }} |
| 134 | + run: | |
| 135 | + echo "Executing rollout to ${{ needs.parse-command.outputs.environment }}..." |
| 136 | +
|
| 137 | + bytebase-action rollout \ |
| 138 | + --url=${{ env.BYTEBASE_URL }} \ |
| 139 | + --service-account=${{ env.BYTEBASE_SERVICE_ACCOUNT }} \ |
| 140 | + --service-account-secret=${{ env.BYTEBASE_SERVICE_ACCOUNT_SECRET }} \ |
| 141 | + --project=${{ env.BYTEBASE_PROJECT }} \ |
| 142 | + --target-stage=${{ env.BYTEBASE_TARGET_STAGE }} \ |
| 143 | + --plan=${{ steps.create-rollout.outputs.plan }} |
| 144 | +
|
| 145 | + - name: Comment deployment success |
| 146 | + if: success() |
| 147 | + uses: peter-evans/create-or-update-comment@v3 |
| 148 | + with: |
| 149 | + issue-number: ${{ github.event.issue.number }} |
| 150 | + body: | |
| 151 | + ✅ **Migration deployment completed successfully** |
| 152 | +
|
| 153 | + **Environment:** `${{ needs.parse-command.outputs.environment }}` |
| 154 | + **PR:** #${{ github.event.issue.number }} |
| 155 | + **Triggered by:** @${{ github.event.comment.user.login }} |
| 156 | +
|
| 157 | + Database schema has been updated in the `${{ needs.parse-command.outputs.environment }}` environment. |
| 158 | +
|
| 159 | + - name: Comment deployment failure |
| 160 | + if: failure() |
| 161 | + uses: peter-evans/create-or-update-comment@v3 |
| 162 | + with: |
| 163 | + issue-number: ${{ github.event.issue.number }} |
| 164 | + body: | |
| 165 | + ❌ **Migration deployment failed** |
| 166 | +
|
| 167 | + **Environment:** `${{ needs.parse-command.outputs.environment }}` |
| 168 | + **PR:** #${{ github.event.issue.number }} |
| 169 | + **Triggered by:** @${{ github.event.comment.user.login }} |
| 170 | +
|
| 171 | + Please check the [workflow run](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}) for details. |
0 commit comments