-
Notifications
You must be signed in to change notification settings - Fork 303
227 lines (187 loc) · 8.53 KB
/
testserver.yml
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
name: Artemis Staging Deployment
on:
workflow_dispatch:
inputs:
branch:
description: 'Branch to deploy'
required: true
concurrency: staging
env:
build_workflow_name: build.yml
jobs:
check-build-status:
runs-on: ubuntu-latest
outputs:
build_workflow_run_id: ${{ steps.set_build_workflow_id.outputs.workflow_id }}
steps:
- name: Get latest build workflow run
id: get_workflow_run
uses: octokit/[email protected]
with:
route: GET /repos/${{ github.repository }}/actions/workflows/${{ env.build_workflow_name }}/runs?branch=${{ github.event.inputs.branch }}&per_page=1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Set build workflow ID
id: set_build_workflow_id
run: |
WORKFLOW_ID=$(echo '${{ steps.get_workflow_run.outputs.data }}' | jq -r '.workflow_runs[0].id')
echo "workflow_id=$WORKFLOW_ID" >> $GITHUB_OUTPUT
- name: Check for war artifact
id: verify_artifact
uses: octokit/[email protected]
with:
route: GET /repos/${{ github.repository }}/actions/runs/${{ steps.set_build_workflow_id.outputs.workflow_id }}/artifacts?name=Artemis.war
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Verify artifact exists
id: check_result
run: |
TOTAL_COUNT=$(echo '${{ steps.verify_artifact.outputs.data }}' | jq -r '.total_count')
if [ "$TOTAL_COUNT" -gt 0 ]; then
echo "Found Artemis.war artifact in latest build"
else
echo "::error::No Artemis.war artifact found in latest build!"
exit 1
fi
deploy:
needs: check-build-status
runs-on: [self-hosted, ase-large-ubuntu]
environment:
name: artemis-staging-localci.artemis.cit.tum.de
url: ${{ vars.DEPLOYMENT_URL }}
env:
DEPLOYMENT_HOSTS_PRIMARY: ${{ vars.DEPLOYMENT_HOSTS_PRIMARY}}
DEPLOYMENT_HOSTS_SECONDARY: ${{ vars.DEPLOYMENT_HOSTS_SECONDARY}}
DEPLOYMENT_USER: ${{ vars.DEPLOYMENT_USER }}
DEPLOYMENT_FOLDER: ${{ vars.DEPLOYMENT_FOLDER }}
HEALTH_CHECK_URL: "${{vars.DEPLOYMENT_URL}}/management/health"
WORKFLOW_RUN_ID: ${{needs.check-build-status.outputs.build_workflow_run_id}}
timeout-minutes: 10
steps:
- name: Clean workspace
run: |
echo "[INFO] Cleaning workspace..."
rm -rf artifacts/
rm -rf ./*
mkdir -p artifacts
- name: Download artifact
uses: actions/download-artifact@v4
with:
name: Artemis.war
path: artifacts
github-token: ${{ secrets.GITHUB_TOKEN }}
run-id: ${{ env.WORKFLOW_RUN_ID }}
- name: Setup SSH and Known Hosts
env:
DEPLOYMENT_SSH_KEY: ${{ secrets.DEPLOYMENT_SSH_KEY }}
SSH_AUTH_SOCK: /tmp/ssh_agent_${{ github.run_id }}.sock
DEPLOYMENT_HOST_PUBLIC_KEYS: ${{ vars.DEPLOYMENT_HOST_PUBLIC_KEYS }}
run: |
mkdir -p ~/.ssh
chmod 700 ~/.ssh
# Write private key
echo "$DEPLOYMENT_SSH_KEY" | sed 's/\\n/\n/g' > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
# Write known hosts
echo "$DEPLOYMENT_HOST_PUBLIC_KEYS" > ~/.ssh/known_hosts
chmod 644 ~/.ssh/known_hosts
# Test SSH connection
echo "Testing SSH connection..."
ssh -v -i ~/.ssh/id_rsa $DEPLOYMENT_USER@$DEPLOYMENT_HOSTS_PRIMARY 'echo "test"'
- name: Phase 1 - Stop Secondary Nodes
run: |
HOSTS_SPACE_SEPARATED=$(echo "$DEPLOYMENT_HOSTS_SECONDARY" | tr -d '\r' | tr '\n' ' ' | awk '{$1=$1};1')
echo "Debug: Hosts list: $HOSTS_SPACE_SEPARATED"
for node in $HOSTS_SPACE_SEPARATED
do
SSH="ssh -i ~/.ssh/id_rsa -l $DEPLOYMENT_USER $node"
echo "[INFO] Stop artemis.service on ${node} ..."
$SSH sudo systemctl stop artemis
done
- name: Phase 1 - Deploy to Primary Node
run: |
echo "[INFO] Deploy on $DEPLOYMENT_HOSTS_PRIMARY ..."
SSH="ssh -o LogLevel=ERROR -i ~/.ssh/id_rsa -l $DEPLOYMENT_USER $DEPLOYMENT_HOSTS_PRIMARY"
# Store the war file name
WAR_FILE=$(ls -1 artifacts/*.war | head -n 1)
# Check if artifacts directory contains the WAR file
echo "[INFO] Checking local artifacts..."
ls -la artifacts/
if [ ! -f "$WAR_FILE" ]; then
echo "Error: No WAR file found in artifacts directory"
exit 1
fi
# Check remote directory exists and is writable
echo "[INFO] Checking remote directory..."
$SSH "if [ ! -d /opt/artemis ]; then echo 'Error: /opt/artemis directory does not exist'; exit 1; fi"
$SSH "if [ ! -w /opt/artemis ]; then echo 'Error: /opt/artemis directory is not writable'; exit 1; fi"
# Remove old backup if exists
echo "[INFO] Remove old artemis.war ..."
$SSH "rm -f /opt/artemis/artemis.war.old"
# Copy new artemis.war to node
echo "[INFO] Copy new artemis.war ..."
scp -v -i ~/.ssh/id_rsa "$WAR_FILE" $DEPLOYMENT_USER@$DEPLOYMENT_HOSTS_PRIMARY:/opt/artemis/artemis.war.new
if [ $? -ne 0 ]; then
echo "Error: Failed to copy WAR file"
exit 1
fi
# Verify the file was copied successfully
echo "[INFO] Verify new WAR file..."
$SSH "if [ ! -f /opt/artemis/artemis.war.new ]; then echo 'Error: WAR file not found after copy'; exit 1; fi"
# Stop Artemis-Service on node
echo "[INFO] Stop artemis.service ..."
$SSH sudo systemctl stop artemis
# Replace old artemis.war
echo "[INFO] Rename old artemis.war ..."
$SSH mv /opt/artemis/artemis.war /opt/artemis/artemis.war.old || true
echo "[INFO] Rename new artemis.war ..."
$SSH mv /opt/artemis/artemis.war.new /opt/artemis/artemis.war
# Start Artemis-Service on node
echo "[INFO] Start artemis.service ..."
$SSH sudo systemctl start artemis
- name: Verify Primary Node Deployment
id: verify_deployment
run: |
while true; do
echo "Performing health check..."
RESPONSE=$(curl -s -f $HEALTH_CHECK_URL || echo '{"status":"DOWN"}')
STATUS=$(echo $RESPONSE | grep -o '"status":"[^"]*"' | cut -d'"' -f4)
if [ "$STATUS" = "UP" ]; then
echo "Health check passed! Application is UP"
exit 0
else
echo "Health check failed. Status: $STATUS"
echo "Waiting 10 seconds before next attempt..."
sleep 10
fi
done
- name: Phase 2 - Deploy to Secondary Nodes
run: |
HOSTS_SPACE_SEPARATED=$(echo "$DEPLOYMENT_HOSTS_SECONDARY" | tr -d '\r' | tr '\n' ' ' | awk '{$1=$1};1')
WAR_FILE=$(ls -1 artifacts/*.war | head -n 1)
echo "Debug: Hosts list: $HOSTS_SPACE_SEPARATED"
for node in $HOSTS_SPACE_SEPARATED
do
echo "##################################################################################################"
echo "[INFO] Deploy on $node ..."
echo "##################################################################################################"
# Build SSH-command
SSH="ssh -o LogLevel=ERROR -i ~/.ssh/id_rsa -l $DEPLOYMENT_USER $node"
# Remove old artemis.war
echo "[INFO] Remove old artemis.war ..."
$SSH "rm -f /opt/artemis/artemis.war.old"
# Copy new artemis.war to node
echo "[INFO] Copy new artemis.war ..."
scp -i ~/.ssh/id_rsa "$WAR_FILE" "$DEPLOYMENT_USER@$node:/opt/artemis/artemis.war.new"
# Stop Artemis-Service on node
echo "[INFO] Stop artemis.service ..."
$SSH "sudo systemctl stop artemis"
# Replace old artemis.war
echo "[INFO] Rename old artemis.war ..."
$SSH "mv /opt/artemis/artemis.war /opt/artemis/artemis.war.old || true"
echo "[INFO] Rename new artemis.war ..."
$SSH "mv /opt/artemis/artemis.war.new /opt/artemis/artemis.war"
# Start Artemis-Service on node
echo "[INFO] Start artemis.service ..."
$SSH "sudo systemctl start artemis"
done