Skip to content

Commit 2c2e810

Browse files
authored
chore: update chatops (#43)
1 parent 871dd86 commit 2c2e810

File tree

1 file changed

+111
-38
lines changed

1 file changed

+111
-38
lines changed

.github/workflows/chatops-migrate.yml

Lines changed: 111 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,58 @@ jobs:
1717
outputs:
1818
environment: ${{ steps.parse.outputs.environment }}
1919
valid-command: ${{ steps.parse.outputs.valid-command }}
20+
stage: ${{ steps.parse.outputs.stage }}
21+
targets: ${{ steps.parse.outputs.targets }}
2022
steps:
23+
- name: Write command config
24+
# write a config.yaml file to runner temp directory
25+
run: |
26+
cat <<EOF > ${{ runner.temp }}/config.yaml
27+
test:
28+
stage: environments/test
29+
targets:
30+
- instances/test-sample-instance/databases/hr_test
31+
prod:
32+
stage: environments/prod
33+
targets:
34+
- instances/prod-sample-instance/databases/hr_prod
35+
EOF
2136
- name: Parse migrate command
2237
id: parse
2338
run: |
2439
COMMENT="${{ github.event.comment.body }}"
2540
echo "Comment: $COMMENT"
41+
42+
CONFIG_FILE="${{ runner.temp }}/config.yaml"
43+
44+
# Get list of valid environments from config
45+
VALID_ENVS=$(yq eval 'keys | join(", ")' "$CONFIG_FILE")
46+
echo "valid-envs=$VALID_ENVS" >> $GITHUB_OUTPUT
2647
2748
# Extract environment from "/migrate <environment>"
2849
if [[ $COMMENT =~ ^/migrate[[:space:]]+([a-zA-Z]+) ]]; then
2950
ENVIRONMENT="${BASH_REMATCH[1]}"
3051
echo "Parsed environment: $ENVIRONMENT"
3152
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
53+
# Check if environment exists in config using yq
54+
if yq eval "has(\"$ENVIRONMENT\")" "$CONFIG_FILE" | grep -q true; then
55+
echo "environment=$ENVIRONMENT" >> $GITHUB_OUTPUT
56+
echo "valid-command=true" >> $GITHUB_OUTPUT
57+
58+
# Extract stage and targets from config for the environment
59+
STAGE=$(yq eval ".$ENVIRONMENT.stage" "$CONFIG_FILE")
60+
TARGETS=$(yq eval ".$ENVIRONMENT.targets | join(\",\")" "$CONFIG_FILE")
61+
62+
echo "stage=$STAGE" >> $GITHUB_OUTPUT
63+
echo "targets=$TARGETS" >> $GITHUB_OUTPUT
64+
echo "✅ Valid environment: $ENVIRONMENT"
65+
echo " Stage: $STAGE"
66+
echo " Targets: $TARGETS"
67+
else
68+
echo "valid-command=false" >> $GITHUB_OUTPUT
69+
echo "❌ Invalid environment: $ENVIRONMENT"
70+
echo " Valid environments are: $VALID_ENVS"
71+
fi
4472
else
4573
echo "valid-command=false" >> $GITHUB_OUTPUT
4674
echo "❌ Invalid command format"
@@ -62,7 +90,7 @@ jobs:
6290
❌ Invalid migrate command.
6391
6492
**Usage:** `/migrate <environment>`
65-
**Valid environments:** `test`, `prod`
93+
**Valid environments:** `${{ steps.parse.outputs.valid-envs }}`
6694
6795
**Example:** `/migrate test`
6896
reactions: confused
@@ -81,36 +109,25 @@ jobs:
81109
ref: refs/pull/${{ github.event.issue.number }}/merge
82110

83111
- name: Comment deployment started
112+
id: deployment-comment
84113
uses: peter-evans/create-or-update-comment@v3
85114
with:
86115
issue-number: ${{ github.event.issue.number }}
87116
body: |
88-
🚀 **Migration deployment started**
117+
🚀 **Migration deployment starting**
89118
90119
**Environment:** `${{ needs.parse-command.outputs.environment }}`
91120
**PR:** #${{ github.event.issue.number }}
92121
**Triggered by:** @${{ github.event.comment.user.login }}
93122
94-
Deploying database changes...
123+
Creating rollout plan...
95124
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
125+
Please check the [workflow run](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}) for details.
109126
110127
- name: Create rollout plan
111128
id: create-rollout
112129
env:
113-
BYTEBASE_TARGETS: ${{ steps.env-config.outputs.bytebase-targets }}
130+
BYTEBASE_TARGETS: ${{ needs.parse-command.outputs.targets }}
114131
FILE_PATTERN: "migrations-semver/*.sql"
115132
BYTEBASE_OUTPUT: ${{ runner.temp }}/bytebase-metadata.json
116133
run: |
@@ -128,9 +145,51 @@ jobs:
128145
PLAN=$(jq -r .plan ${{ runner.temp }}/bytebase-metadata.json)
129146
echo "plan=$PLAN" >> $GITHUB_OUTPUT
130147
148+
- name: Extract rollout metadata
149+
id: extract-metadata
150+
run: |
151+
# Read the metadata file
152+
METADATA_FILE="${{ runner.temp }}/bytebase-metadata.json"
153+
154+
# Extract release, plan, and rollout from metadata
155+
RELEASE=$(jq -r .release "$METADATA_FILE")
156+
PLAN=$(jq -r .plan "$METADATA_FILE")
157+
ROLLOUT=$(jq -r .rollout "$METADATA_FILE")
158+
159+
# Trim trailing slash from BYTEBASE_URL if present
160+
BASE_URL="${{ env.BYTEBASE_URL }}"
161+
BASE_URL="${BASE_URL%/}"
162+
163+
# Output for use in comment
164+
echo "release=$RELEASE" >> $GITHUB_OUTPUT
165+
echo "plan=$PLAN" >> $GITHUB_OUTPUT
166+
echo "rollout=$ROLLOUT" >> $GITHUB_OUTPUT
167+
echo "base-url=$BASE_URL" >> $GITHUB_OUTPUT
168+
169+
- name: Update comment with rollout details
170+
uses: peter-evans/create-or-update-comment@v3
171+
with:
172+
comment-id: ${{ steps.deployment-comment.outputs.comment-id }}
173+
edit-mode: replace
174+
body: |
175+
🚀 **Migration deployment in progress**
176+
177+
**Environment:** `${{ needs.parse-command.outputs.environment }}`
178+
**PR:** #${{ github.event.issue.number }}
179+
**Triggered by:** @${{ github.event.comment.user.login }}
180+
181+
**Bytebase Resources:**
182+
- 📦 **Release:** ${{ steps.extract-metadata.outputs.base-url }}/${{ steps.extract-metadata.outputs.release }}
183+
- 📝 **Plan:** ${{ steps.extract-metadata.outputs.base-url }}/${{ steps.extract-metadata.outputs.plan }}
184+
- 🚀 **Rollout:** ${{ steps.extract-metadata.outputs.base-url }}/${{ steps.extract-metadata.outputs.rollout }}
185+
186+
Executing database changes...
187+
188+
Please check the [workflow run](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}) for details.
189+
131190
- name: Execute rollout
132191
env:
133-
BYTEBASE_TARGET_STAGE: ${{ steps.env-config.outputs.bytebase-stage }}
192+
BYTEBASE_TARGET_STAGE: ${{ needs.parse-command.outputs.stage }}
134193
run: |
135194
echo "Executing rollout to ${{ needs.parse-command.outputs.environment }}..."
136195
@@ -146,26 +205,40 @@ jobs:
146205
if: success()
147206
uses: peter-evans/create-or-update-comment@v3
148207
with:
149-
issue-number: ${{ github.event.issue.number }}
208+
comment-id: ${{ steps.deployment-comment.outputs.comment-id }}
209+
edit-mode: replace
150210
body: |
151211
✅ **Migration deployment completed successfully**
152-
153-
**Environment:** `${{ needs.parse-command.outputs.environment }}`
212+
213+
**Environment:** `${{ needs.parse-command.outputs.environment }}`
154214
**PR:** #${{ github.event.issue.number }}
155215
**Triggered by:** @${{ github.event.comment.user.login }}
216+
217+
**Bytebase Resources:**
218+
- 📦 **Release:** ${{ steps.extract-metadata.outputs.base-url }}/${{ steps.extract-metadata.outputs.release }}
219+
- 📝 **Plan:** ${{ steps.extract-metadata.outputs.base-url }}/${{ steps.extract-metadata.outputs.plan }}
220+
- 🚀 **Rollout:** ${{ steps.extract-metadata.outputs.base-url }}/${{ steps.extract-metadata.outputs.rollout }}
221+
222+
Database schema has been successfully updated.
156223
157-
Database schema has been updated in the `${{ needs.parse-command.outputs.environment }}` environment.
224+
Please check the [workflow run](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}) for details.
158225
159226
- name: Comment deployment failure
160227
if: failure()
161228
uses: peter-evans/create-or-update-comment@v3
162229
with:
163-
issue-number: ${{ github.event.issue.number }}
230+
comment-id: ${{ steps.deployment-comment.outputs.comment-id }}
231+
edit-mode: replace
164232
body: |
165233
❌ **Migration deployment failed**
166-
167-
**Environment:** `${{ needs.parse-command.outputs.environment }}`
234+
235+
**Environment:** `${{ needs.parse-command.outputs.environment }}`
168236
**PR:** #${{ github.event.issue.number }}
169237
**Triggered by:** @${{ github.event.comment.user.login }}
170-
238+
239+
**Bytebase Resources:**
240+
- 📦 **Release:** ${{ steps.extract-metadata.outputs.base-url }}/${{ steps.extract-metadata.outputs.release }}
241+
- 📝 **Plan:** ${{ steps.extract-metadata.outputs.base-url }}/${{ steps.extract-metadata.outputs.plan }}
242+
- 🚀 **Rollout:** ${{ steps.extract-metadata.outputs.base-url }}/${{ steps.extract-metadata.outputs.rollout }}
243+
171244
Please check the [workflow run](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}) for details.

0 commit comments

Comments
 (0)