@@ -97,27 +97,108 @@ jobs:
9797 platforms : linux/amd64,linux/arm64
9898
9999 deploy-via-portainer :
100- name : Deploy via Portainer
100+ name : Deploy via Portainer
101101 runs-on : [self-hosted, home]
102102 needs : [build-and-push, build-frontend-image]
103103 if : success() && github.ref == 'refs/heads/main'
104104
105105 steps :
106- - name : Deploy to Portainer
106+ - name : Verify Portainer Connection
107+ run : |
108+ echo "π Verifying connection to Portainer..."
109+
110+ # Test connection to Portainer
111+ for i in {1..5}; do
112+ if curl -f -s --connect-timeout 10 https://portainer.sankalpnarula.com/api/status > /dev/null; then
113+ echo "β
Successfully connected to Portainer"
114+ break
115+ fi
116+ echo "β³ Attempt $i/5 - Waiting for Portainer connection..."
117+ sleep 3
118+ done
119+
120+ # Final connection test
121+ if ! curl -f -s --connect-timeout 10 https://portainer.sankalpnarula.com/api/status > /dev/null; then
122+ echo "β Failed to connect to Portainer"
123+ exit 1
124+ fi
125+
126+ - name : Trigger Portainer Stack Redeployment
107127 env :
108- PORTAINER_URL : https://portainer.sankalpnarula.com # or http://localhost:9000
128+ PORTAINER_URL : https://portainer.sankalpnarula.com
109129 PORTAINER_TOKEN : ${{ secrets.PORTAINER_ACCESS_TOKEN }}
110130 STACK_NAME : ocpp-chaos-sim
111131 run : |
112132 set -e
113- echo "π Starting deployment..."
114133
115- # Get stack ID and deploy (same script as before)
134+ echo "π Starting deployment process..."
135+
136+ # Get stack ID
137+ echo "π Looking for stack: $STACK_NAME"
116138 STACK_ID=$(curl -s -H "X-API-Key: $PORTAINER_TOKEN" \
117139 "$PORTAINER_URL/api/stacks" | \
118140 jq -r ".[] | select(.Name == \"$STACK_NAME\") | .Id")
119-
120- # ... rest of deployment logic
141+
142+ if [ "$STACK_ID" = "null" ] || [ -z "$STACK_ID" ]; then
143+ echo "β Stack '$STACK_NAME' not found. Available stacks:"
144+ curl -s -H "X-API-Key: $PORTAINER_TOKEN" \
145+ "$PORTAINER_URL/api/stacks" | jq -r '.[].Name'
146+ exit 1
147+ fi
148+
149+ echo "π Found stack ID: $STACK_ID"
150+
151+ # Get endpoint ID (usually 1 for local Docker, but let's be sure)
152+ ENDPOINT_ID=$(curl -s -H "X-API-Key: $PORTAINER_TOKEN" \
153+ "$PORTAINER_URL/api/stacks/$STACK_ID" | \
154+ jq -r '.EndpointId')
155+
156+ echo "π― Using endpoint ID: $ENDPOINT_ID"
157+
158+ # Trigger stack update (pull latest images and redeploy)
159+ echo "π Triggering stack redeployment with image pull..."
160+ RESPONSE=$(curl -s -w "%{http_code}" -X PUT \
161+ -H "X-API-Key: $PORTAINER_TOKEN" \
162+ -H "Content-Type: application/json" \
163+ -d '{
164+ "pullImage": true,
165+ "prune": true
166+ }' \
167+ "$PORTAINER_URL/api/stacks/$STACK_ID?endpointId=$ENDPOINT_ID")
168+
169+ HTTP_CODE="${RESPONSE: -3}"
170+ RESPONSE_BODY="${RESPONSE%???}"
171+
172+ if [ "$HTTP_CODE" = "200" ]; then
173+ echo "β
Stack redeployment triggered successfully!"
174+ echo "π Response: $RESPONSE_BODY"
175+ else
176+ echo "β Deployment failed with HTTP $HTTP_CODE"
177+ echo "π Response: $RESPONSE_BODY"
178+ exit 1
179+ fi
180+
181+ # Wait for deployment to process
182+ echo "β³ Waiting for deployment to complete..."
183+ sleep 15
184+
185+ # Check deployment status
186+ STACK_STATUS=$(curl -s -H "X-API-Key: $PORTAINER_TOKEN" \
187+ "$PORTAINER_URL/api/stacks/$STACK_ID" | \
188+ jq -r '.Status // "unknown"')
189+
190+ echo "π Stack status: $STACK_STATUS"
191+
192+ # Optional: Check if services are running
193+ echo "π Checking service status..."
194+ SERVICES=$(curl -s -H "X-API-Key: $PORTAINER_TOKEN" \
195+ "$PORTAINER_URL/api/endpoints/$ENDPOINT_ID/docker/services" | \
196+ jq -r '.[] | select(.Spec.Labels."com.docker.compose.project" == "'$STACK_NAME'") | "\(.Spec.Name): \(.ServiceStatus.RunningTasks)/\(.Spec.Mode.Replicated.Replicas)"' 2>/dev/null || echo "Service status unavailable")
197+
198+ if [ "$SERVICES" != "Service status unavailable" ]; then
199+ echo "π Services:"
200+ echo "$SERVICES"
201+ fi
121202
122203 - name : Deployment Summary
123204 if : always()
@@ -129,14 +210,13 @@ jobs:
129210 echo "π **Portainer**: [View Stack](https://portainer.sankalpnarula.com)" >> $GITHUB_STEP_SUMMARY
130211 echo "π¦ **Backend Image**: \`ghcr.io/${{ github.repository }}:latest\`" >> $GITHUB_STEP_SUMMARY
131212 echo "π¦ **Frontend Image**: \`ghcr.io/${{ github.repository }}-frontend:latest\`" >> $GITHUB_STEP_SUMMARY
213+ echo "" >> $GITHUB_STEP_SUMMARY
214+ echo "**Deployment included:**" >> $GITHUB_STEP_SUMMARY
215+ echo "- β
Pull latest images from GHCR" >> $GITHUB_STEP_SUMMARY
216+ echo "- β
Recreate containers with new images" >> $GITHUB_STEP_SUMMARY
217+ echo "- β
Prune old unused images" >> $GITHUB_STEP_SUMMARY
132218 else
133219 echo "β **Status**: Deployment failed" >> $GITHUB_STEP_SUMMARY
134220 echo "π **Check**: Review job logs for details" >> $GITHUB_STEP_SUMMARY
221+ echo "π **Portainer**: [Check Stack Status](https://portainer.sankalpnarula.com)" >> $GITHUB_STEP_SUMMARY
135222 fi
136-
137- - name : Cleanup Tailscale Connection
138- if : always()
139- run : |
140- echo "π Cleaning up Tailscale connection..."
141- sudo tailscale logout || true
142- echo "β
Tailscale disconnected"
0 commit comments