|
8 | 8 | env: |
9 | 9 | REGISTRY: ghcr.io |
10 | 10 | IMAGE_NAME: ${{ github.repository }} |
| 11 | + PORTAINER_WEBHOOK_URL: ${{ secrets.PORTAINER_WEBHOOK_URL }} |
11 | 12 |
|
12 | 13 | jobs: |
13 | 14 | check-changes: |
@@ -165,92 +166,49 @@ jobs: |
165 | 166 | - name: Verify Portainer Connection |
166 | 167 | run: | |
167 | 168 | echo "🔍 Verifying connection to Portainer..." |
168 | | - |
169 | | - # Test connection to Portainer |
170 | | - for i in {1..5}; do |
171 | | - if curl -f -s --connect-timeout 10 https://portainer.sankalpnarula.com/api/status > /dev/null; then |
172 | | - echo "✅ Successfully connected to Portainer" |
173 | | - break |
174 | | - fi |
175 | | - echo "⏳ Attempt $i/5 - Waiting for Portainer connection..." |
176 | | - sleep 3 |
177 | | - done |
178 | | - |
179 | | - # Final connection test |
180 | | - if ! curl -f -s --connect-timeout 10 https://portainer.sankalpnarula.com/api/status > /dev/null; then |
| 169 | + if curl -f -s --connect-timeout 10 https://portainer.sankalpnarula.com/api/status > /dev/null; then |
| 170 | + echo "✅ Successfully connected to Portainer" |
| 171 | + else |
181 | 172 | echo "❌ Failed to connect to Portainer" |
182 | 173 | exit 1 |
183 | 174 | fi |
184 | 175 |
|
185 | 176 | - name: Trigger Portainer Stack Redeployment |
186 | 177 | env: |
187 | | - PORTAINER_URL: https://portainer.sankalpnarula.com |
188 | | - PORTAINER_TOKEN: ${{ secrets.PORTAINER_ACCESS_TOKEN }} |
189 | | - STACK_NAME: ocpp-chaos-sim |
| 178 | + PORTAINER_WEBHOOK_URL: ${{ secrets.PORTAINER_WEBHOOK_URL }} |
190 | 179 | BACKEND_CHANGED: ${{ needs.check-changes.outputs.backend-changed }} |
191 | 180 | FRONTEND_CHANGED: ${{ needs.check-changes.outputs.frontend-changed }} |
192 | 181 | run: | |
193 | | - set -e |
194 | | - |
195 | 182 | echo "🚀 Starting deployment process..." |
196 | | - echo "Backend changed: $BACKEND_CHANGED" |
197 | | - echo "Frontend changed: $FRONTEND_CHANGED" |
198 | | - |
199 | | - # Get stack ID |
200 | | - echo "🔍 Looking for stack: $STACK_NAME" |
201 | | - STACK_ID=$(curl -s -H "X-API-Key: $PORTAINER_TOKEN" \ |
202 | | - "$PORTAINER_URL/api/stacks" | \ |
203 | | - jq -r ".[] | select(.Name == \"$STACK_NAME\") | .Id") |
204 | | - |
205 | | - if [ "$STACK_ID" = "null" ] || [ -z "$STACK_ID" ]; then |
206 | | - echo "❌ Stack '$STACK_NAME' not found. Available stacks:" |
207 | | - curl -s -H "X-API-Key: $PORTAINER_TOKEN" \ |
208 | | - "$PORTAINER_URL/api/stacks" | jq -r '.[].Name' |
209 | | - exit 1 |
210 | | - fi |
| 183 | + echo "📋 Backend changed: $BACKEND_CHANGED" |
| 184 | + echo "📋 Frontend changed: $FRONTEND_CHANGED" |
211 | 185 | |
212 | | - echo "📋 Found stack ID: $STACK_ID" |
| 186 | + # Trigger redeployment via webhook |
| 187 | + echo "🔄 Triggering stack redeployment via webhook..." |
213 | 188 | |
214 | | - # Get endpoint ID |
215 | | - ENDPOINT_ID=$(curl -s -H "X-API-Key: $PORTAINER_TOKEN" \ |
216 | | - "$PORTAINER_URL/api/stacks/$STACK_ID" | \ |
217 | | - jq -r '.EndpointId') |
218 | | - |
219 | | - echo "🎯 Using endpoint ID: $ENDPOINT_ID" |
220 | | - |
221 | | - # Trigger stack update (always pull images, even if only one service changed) |
222 | | - echo "🔄 Triggering stack redeployment with image pull..." |
223 | | - RESPONSE=$(curl -s -w "%{http_code}" -X PUT \ |
224 | | - -H "X-API-Key: $PORTAINER_TOKEN" \ |
| 189 | + RESPONSE=$(curl -s -w "%{http_code}" -X POST \ |
225 | 190 | -H "Content-Type: application/json" \ |
226 | | - -d '{ |
227 | | - "pullImage": true, |
228 | | - "prune": true |
229 | | - }' \ |
230 | | - "$PORTAINER_URL/api/stacks/$STACK_ID?endpointId=$ENDPOINT_ID") |
| 191 | + -d '{"pullImage": true}' \ |
| 192 | + "$PORTAINER_WEBHOOK_URL") |
231 | 193 | |
232 | 194 | HTTP_CODE="${RESPONSE: -3}" |
233 | 195 | RESPONSE_BODY="${RESPONSE%???}" |
234 | 196 | |
235 | | - if [ "$HTTP_CODE" = "200" ]; then |
236 | | - echo "✅ Stack redeployment triggered successfully!" |
237 | | - echo "📝 Response: $RESPONSE_BODY" |
238 | | - else |
239 | | - echo "❌ Deployment failed with HTTP $HTTP_CODE" |
240 | | - echo "📝 Response: $RESPONSE_BODY" |
241 | | - exit 1 |
242 | | - fi |
243 | | - |
244 | | - # Wait for deployment to process |
245 | | - echo "⏳ Waiting for deployment to complete..." |
246 | | - sleep 15 |
247 | | - |
248 | | - # Check deployment status |
249 | | - STACK_STATUS=$(curl -s -H "X-API-Key: $PORTAINER_TOKEN" \ |
250 | | - "$PORTAINER_URL/api/stacks/$STACK_ID" | \ |
251 | | - jq -r '.Status // "unknown"') |
252 | | - |
253 | | - echo "📊 Stack status: $STACK_STATUS" |
| 197 | + case $HTTP_CODE in |
| 198 | + 200|204) |
| 199 | + echo "✅ Stack redeployment triggered successfully!" |
| 200 | + [ -n "$RESPONSE_BODY" ] && echo "📝 Response: $RESPONSE_BODY" |
| 201 | + ;; |
| 202 | + *) |
| 203 | + echo "❌ Deployment webhook failed with HTTP $HTTP_CODE" |
| 204 | + echo "📝 Response: $RESPONSE_BODY" |
| 205 | + echo "🔗 Check Portainer for more details" |
| 206 | + exit 1 |
| 207 | + ;; |
| 208 | + esac |
| 209 | + |
| 210 | + echo "⏳ Deployment initiated via webhook - check Portainer for progress" |
| 211 | + echo "🔗 Monitor at: https://portainer.sankalpnarula.com" |
254 | 212 |
|
255 | 213 | - name: Deployment Summary |
256 | 214 | if: always() |
|
0 commit comments