-
Notifications
You must be signed in to change notification settings - Fork 0
150 lines (126 loc) · 5.28 KB
/
rollback.yml
File metadata and controls
150 lines (126 loc) · 5.28 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
name: Rollback Infrastructure
on:
workflow_dispatch:
inputs:
target_environment:
description: "Name of the SAM config environment to rollback (e.g., development, testing and production)."
required: true
type: choice
options:
- development
- testing
- production
rollback_commit:
description: "Commit hash to rollback to (e.g., abc1234 or HEAD~1) or branch name"
required: true
type: string
confirm_rollback:
description: 'Type "CONFIRM" to confirm rollback. This is an extra safety measure.'
required: true
type: string
concurrency:
group: ${{ github.workflow }}-${{ github.event.inputs.target_environment }}
cancel-in-progress: true
jobs:
rollback-infrastructure:
environment: ${{ github.event.inputs.target_environment }}
if: github.event.inputs.confirm_rollback == 'CONFIRM'
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.12"
cache: pip
- name: Upgrade pip and install AWS SAM CLI
run: |
python -m pip install --upgrade pip
pip install aws-sam-cli
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: eu-west-2
- name: Determine SAM Config Env Name
id: sam_env
run: |
if [ "${{ github.event.inputs.target_environment }}" == "production" ]; then
echo "sam_config_name=prod" >> $GITHUB_OUTPUT
elif [ "${{ github.event.inputs.target_environment }}" == "development" ]; then
echo "sam_config_name=dev" >> $GITHUB_OUTPUT
elif [ "${{ github.event.inputs.target_environment }}" == "testing" ]; then
echo "sam_config_name=test" >> $GITHUB_OUTPUT
else
echo "Error: unsupported target_environment=${{ github.event.inputs.target_environment }}"
exit 1
fi
- name: Validate Rollback Commit
id: validate_commit
run: |
COMMIT="${{ github.event.inputs.rollback_commit }}"
# Fetch all commits
git fetch --all --tags --quiet
RESOLVED_COMMIT=$(git rev-parse "$COMMIT")
if ! git cat-file -e "$RESOLVED_COMMIT^{commit}" 2>/dev/null; then
echo "Error: Invalid commit: $COMMIT"
exit 1
fi
echo "Valid commit found: $COMMIT (resolved to $RESOLVED_COMMIT)"
echo "resolved_commit=$RESOLVED_COMMIT" >> "$GITHUB_OUTPUT"
- name: Display Rollback Target
run: |
echo "Rolling back infrastructure:"
echo "Environment: ${{ github.event.inputs.target_environment }}"
echo "SAM Config: ${{ steps.sam_env.outputs.sam_config_name }}"
echo "Target Commit: ${{ steps.validate_commit.outputs.resolved_commit }}"
- name: Get Current Deployment Info
id: current_deployment
run: |
echo "Fetching current deployment information..."
CURRENT_COMMIT=$(git rev-parse HEAD)
echo "current_commit=$CURRENT_COMMIT" >> $GITHUB_OUTPUT
echo "Current deployment - Commit: $CURRENT_COMMIT"
- name: Verify Rollback is Different
run: |
if [ "${{ steps.validate_commit.outputs.resolved_commit }}" == "${{ steps.current_deployment.outputs.current_commit }}" ]; then
echo "Warning: Rollback commit is the same as current deployment"
echo "Current: ${{ steps.current_deployment.outputs.current_commit }}"
echo "Rollback: ${{ steps.validate_commit.outputs.resolved_commit }}"
echo "This rollback will not change the infrastructure"
else
echo "Rollback commit is different from current deployment - proceeding"
fi
- name: Checkout Rollback Commit
run: |
git checkout ${{ steps.validate_commit.outputs.resolved_commit }}
echo "Checked out commit: ${{ steps.validate_commit.outputs.resolved_commit }}"
- name: Validate SAM Template
run: |
echo "Validating SAM template..."
sam validate
- name: Build SAM Application
run: |
echo "Building SAM application..."
sam build
- name: Deploy Rollback Version
run: |
echo "Deploying rollback version..."
sam deploy --config-env "${{ steps.sam_env.outputs.sam_config_name }}" --no-confirm-changeset --no-fail-on-empty-changeset
- name: Rollback Summary
if: always()
run: |
echo "=== ROLLBACK SUMMARY ==="
echo "Environment: ${{ github.event.inputs.target_environment }}"
echo "From commit: ${{ steps.current_deployment.outputs.current_commit }}"
echo "To commit: ${{ steps.validate_commit.outputs.resolved_commit }}"
echo "Status: ${{ job.status }}"
if [ "${{ job.status }}" == "success" ]; then
echo "Rollback completed successfully"
else
echo "Rollback failed"
fi