-
Notifications
You must be signed in to change notification settings - Fork 28
240 lines (199 loc) · 10 KB
/
chatops-migrate.yml
File metadata and controls
240 lines (199 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
name: ChatOps Migration Deployment
on:
issue_comment:
types: [created]
env:
BYTEBASE_URL: https://demo.bytebase.com
BYTEBASE_SERVICE_ACCOUNT: api@service.bytebase.com # set service account via environment variable
BYTEBASE_SERVICE_ACCOUNT_SECRET: ${{ secrets.BYTEBASE_SERVICE_ACCOUNT_SECRET }} # set service account secret via environment variable
BYTEBASE_PROJECT: "projects/hr"
FILE_PATTERN: "migrations-semver/*.sql"
jobs:
parse-command:
if: github.event.issue.pull_request && startsWith(github.event.comment.body, '/migrate')
runs-on: ubuntu-latest
outputs:
environment: ${{ steps.parse.outputs.environment }}
valid-command: ${{ steps.parse.outputs.valid-command }}
stage: ${{ steps.parse.outputs.stage }}
targets: ${{ steps.parse.outputs.targets }}
steps:
- name: Write command config
# write a bytebase-action-config.yaml file to runner temp directory
run: |
cat <<EOF > ${{ runner.temp }}/bytebase-action-config.yaml
test:
stage: environments/test
targets:
- instances/test-sample-instance/databases/hr_test
prod:
stage: environments/prod
targets:
- instances/prod-sample-instance/databases/hr_prod
EOF
- name: Parse migrate command
id: parse
run: |
COMMENT="${{ github.event.comment.body }}"
echo "Comment: $COMMENT"
CONFIG_FILE="${{ runner.temp }}/bytebase-action-config.yaml"
# Get list of valid environments from config
VALID_ENVS=$(yq eval 'keys | join(", ")' "$CONFIG_FILE")
echo "valid-envs=$VALID_ENVS" >> $GITHUB_OUTPUT
# Extract environment from "/migrate <environment>"
if [[ $COMMENT =~ ^/migrate[[:space:]]+([a-zA-Z]+) ]]; then
ENVIRONMENT="${BASH_REMATCH[1]}"
echo "Parsed environment: $ENVIRONMENT"
# Check if environment exists in config using yq
if yq eval "has(\"$ENVIRONMENT\")" "$CONFIG_FILE" | grep -q true; then
echo "environment=$ENVIRONMENT" >> $GITHUB_OUTPUT
echo "valid-command=true" >> $GITHUB_OUTPUT
# Extract stage and targets from config for the environment
STAGE=$(yq eval ".$ENVIRONMENT.stage" "$CONFIG_FILE")
TARGETS=$(yq eval ".$ENVIRONMENT.targets | join(\",\")" "$CONFIG_FILE")
echo "stage=$STAGE" >> $GITHUB_OUTPUT
echo "targets=$TARGETS" >> $GITHUB_OUTPUT
echo "✅ Valid environment: $ENVIRONMENT"
echo " Stage: $STAGE"
echo " Targets: $TARGETS"
else
echo "valid-command=false" >> $GITHUB_OUTPUT
echo "❌ Invalid environment: $ENVIRONMENT"
echo " Valid environments are: $VALID_ENVS"
fi
else
echo "valid-command=false" >> $GITHUB_OUTPUT
echo "❌ Invalid command format"
fi
- name: Add reaction to comment
if: steps.parse.outputs.valid-command == 'true'
uses: peter-evans/create-or-update-comment@v3
with:
comment-id: ${{ github.event.comment.id }}
reactions: rocket
- name: Comment on invalid command
if: steps.parse.outputs.valid-command == 'false'
uses: peter-evans/create-or-update-comment@v3
with:
issue-number: ${{ github.event.issue.number }}
body: |
❌ Invalid migrate command.
**Usage:** `/migrate <environment>`
**Valid environments:** `${{ steps.parse.outputs.valid-envs }}`
**Example:** `/migrate test`
reactions: confused
deploy:
needs: parse-command
if: needs.parse-command.outputs.valid-command == 'true'
runs-on: ubuntu-latest
environment: ${{ needs.parse-command.outputs.environment }}
container:
image: bytebase/bytebase-action:latest
steps:
- name: Checkout PR merge commit
uses: actions/checkout@v4
with:
ref: refs/pull/${{ github.event.issue.number }}/merge
- name: Comment deployment started
id: deployment-comment
uses: peter-evans/create-or-update-comment@v3
with:
issue-number: ${{ github.event.issue.number }}
body: |
🚀 **Migration deployment starting**
**Environment:** `${{ needs.parse-command.outputs.environment }}`
**PR:** #${{ github.event.issue.number }}
**Triggered by:** @${{ github.event.comment.user.login }}
Creating rollout plan...
Please check the [workflow run](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}) for details.
- name: Create rollout plan
id: create-rollout
env:
BYTEBASE_TARGETS: ${{ needs.parse-command.outputs.targets }}
BYTEBASE_OUTPUT: ${{ runner.temp }}/bytebase-metadata.json
run: |
echo "Creating rollout plan for ${{ needs.parse-command.outputs.environment }}..."
bytebase-action rollout \
--url=${{ env.BYTEBASE_URL }} \
--project=${{ env.BYTEBASE_PROJECT }} \
--file-pattern=${{ env.FILE_PATTERN }} \
--targets=${{ env.BYTEBASE_TARGETS }} \
--output=${{ env.BYTEBASE_OUTPUT }}
PLAN=$(jq -r .plan ${{ runner.temp }}/bytebase-metadata.json)
echo "plan=$PLAN" >> $GITHUB_OUTPUT
- name: Extract rollout metadata
id: extract-metadata
run: |
# Read the metadata file
METADATA_FILE="${{ runner.temp }}/bytebase-metadata.json"
# Extract release, plan, and rollout from metadata
RELEASE=$(jq -r .release "$METADATA_FILE")
PLAN=$(jq -r .plan "$METADATA_FILE")
ROLLOUT=$(jq -r .rollout "$METADATA_FILE")
# Trim trailing slash from BYTEBASE_URL if present
BASE_URL="${{ env.BYTEBASE_URL }}"
BASE_URL="${BASE_URL%/}"
# Output for use in comment
echo "release=$RELEASE" >> $GITHUB_OUTPUT
echo "plan=$PLAN" >> $GITHUB_OUTPUT
echo "rollout=$ROLLOUT" >> $GITHUB_OUTPUT
echo "base-url=$BASE_URL" >> $GITHUB_OUTPUT
- name: Update comment with rollout details
uses: peter-evans/create-or-update-comment@v3
with:
comment-id: ${{ steps.deployment-comment.outputs.comment-id }}
edit-mode: replace
body: |
🚀 **Migration deployment in progress**
**Environment:** `${{ needs.parse-command.outputs.environment }}`
**PR:** #${{ github.event.issue.number }}
**Triggered by:** @${{ github.event.comment.user.login }}
**Bytebase Resources:**
- 📦 **Release:** ${{ steps.extract-metadata.outputs.base-url }}/${{ steps.extract-metadata.outputs.release }}
- 📝 **Plan:** ${{ steps.extract-metadata.outputs.base-url }}/${{ steps.extract-metadata.outputs.plan }}
- 🚀 **Rollout:** ${{ steps.extract-metadata.outputs.base-url }}/${{ steps.extract-metadata.outputs.rollout }}
Executing database changes...
Please check the [workflow run](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}) for details.
- name: Execute rollout
env:
BYTEBASE_TARGET_STAGE: ${{ needs.parse-command.outputs.stage }}
run: |
echo "Executing rollout to ${{ needs.parse-command.outputs.environment }}..."
bytebase-action rollout \
--url=${{ env.BYTEBASE_URL }} \
--project=${{ env.BYTEBASE_PROJECT }} \
--target-stage=${{ env.BYTEBASE_TARGET_STAGE }} \
--plan=${{ steps.create-rollout.outputs.plan }}
- name: Comment deployment success
if: success()
uses: peter-evans/create-or-update-comment@v3
with:
comment-id: ${{ steps.deployment-comment.outputs.comment-id }}
edit-mode: replace
body: |
✅ **Migration deployment completed successfully**
**Environment:** `${{ needs.parse-command.outputs.environment }}`
**PR:** #${{ github.event.issue.number }}
**Triggered by:** @${{ github.event.comment.user.login }}
**Bytebase Resources:**
- 📦 **Release:** ${{ steps.extract-metadata.outputs.base-url }}/${{ steps.extract-metadata.outputs.release }}
- 📝 **Plan:** ${{ steps.extract-metadata.outputs.base-url }}/${{ steps.extract-metadata.outputs.plan }}
- 🚀 **Rollout:** ${{ steps.extract-metadata.outputs.base-url }}/${{ steps.extract-metadata.outputs.rollout }}
Database schema has been successfully updated.
Please check the [workflow run](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}) for details.
- name: Comment deployment failure
if: failure()
uses: peter-evans/create-or-update-comment@v3
with:
comment-id: ${{ steps.deployment-comment.outputs.comment-id }}
edit-mode: replace
body: |
❌ **Migration deployment failed**
**Environment:** `${{ needs.parse-command.outputs.environment }}`
**PR:** #${{ github.event.issue.number }}
**Triggered by:** @${{ github.event.comment.user.login }}
**Bytebase Resources:**
- 📦 **Release:** ${{ steps.extract-metadata.outputs.base-url }}/${{ steps.extract-metadata.outputs.release }}
- 📝 **Plan:** ${{ steps.extract-metadata.outputs.base-url }}/${{ steps.extract-metadata.outputs.plan }}
- 🚀 **Rollout:** ${{ steps.extract-metadata.outputs.base-url }}/${{ steps.extract-metadata.outputs.rollout }}
Please check the [workflow run](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}) for details.