Skip to content

Commit cf52ee6

Browse files
committed
Using portainer webhook to trigger deployment
1 parent 82ef232 commit cf52ee6

File tree

1 file changed

+27
-69
lines changed

1 file changed

+27
-69
lines changed

.github/workflows/deploy.yml

Lines changed: 27 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ on:
88
env:
99
REGISTRY: ghcr.io
1010
IMAGE_NAME: ${{ github.repository }}
11+
PORTAINER_WEBHOOK_URL: ${{ secrets.PORTAINER_WEBHOOK_URL }}
1112

1213
jobs:
1314
check-changes:
@@ -165,92 +166,49 @@ jobs:
165166
- name: Verify Portainer Connection
166167
run: |
167168
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
181172
echo "❌ Failed to connect to Portainer"
182173
exit 1
183174
fi
184175
185176
- name: Trigger Portainer Stack Redeployment
186177
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 }}
190179
BACKEND_CHANGED: ${{ needs.check-changes.outputs.backend-changed }}
191180
FRONTEND_CHANGED: ${{ needs.check-changes.outputs.frontend-changed }}
192181
run: |
193-
set -e
194-
195182
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"
211185
212-
echo "📋 Found stack ID: $STACK_ID"
186+
# Trigger redeployment via webhook
187+
echo "🔄 Triggering stack redeployment via webhook..."
213188
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 \
225190
-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")
231193
232194
HTTP_CODE="${RESPONSE: -3}"
233195
RESPONSE_BODY="${RESPONSE%???}"
234196
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"
254212
255213
- name: Deployment Summary
256214
if: always()

0 commit comments

Comments
 (0)