Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| f716b394c2 | |||
| 94477d46ef | |||
| 6ff7bc5fda | |||
| 89150ed41f | |||
| 687e4d9f2d | |||
| d14b719bd2 | |||
| dcfb7805cd | |||
| 06ad8edf18 |
+40
-47
@@ -62,60 +62,53 @@ jobs:
|
|||||||
# PORTAINER_ENDPOINT_ID Numeric endpoint ID (usually 1)
|
# PORTAINER_ENDPOINT_ID Numeric endpoint ID (usually 1)
|
||||||
# PORTAINER_STACK_ID Numeric stack ID to delete before recreating
|
# PORTAINER_STACK_ID Numeric stack ID to delete before recreating
|
||||||
# ----------------------------------------------------------------------
|
# ----------------------------------------------------------------------
|
||||||
- name: Redeploy stack on Portainer
|
- name: Redeploy stack on Portainer via SSH
|
||||||
if: github.event_name == 'workflow_dispatch' && inputs.deploy == true
|
if: github.event_name == 'workflow_dispatch' && inputs.deploy == true
|
||||||
env:
|
env:
|
||||||
PORTAINER_URL: ${{ secrets.PORTAINER_URL }}
|
NAS_SSH_KEY: ${{ secrets.NAS_SSH_KEY }}
|
||||||
PORTAINER_TOKEN: ${{ secrets.PORTAINER_TOKEN }}
|
NAS_HOST: ${{ secrets.NAS_HOST }}
|
||||||
PORTAINER_ENDPOINT_ID: ${{ secrets.PORTAINER_ENDPOINT_ID }}
|
NAS_USER: ${{ secrets.NAS_USER }}
|
||||||
PORTAINER_STACK_ID: ${{ secrets.PORTAINER_STACK_ID }}
|
STACK_NAME: davidaragon-portfolio
|
||||||
|
COMPOSE_FILE: docker-compose.prod.yml
|
||||||
run: |
|
run: |
|
||||||
|
# The act_runner job container can't reach 'portainer' because it
|
||||||
|
# doesn't share the runner's network namespace in v0.6.1. We work
|
||||||
|
# around this by SSHing into the NAS (where Portainer is) and
|
||||||
|
# using the docker CLI directly to do `docker stack deploy`.
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
|
|
||||||
echo "--- Step 1: pre-flight (delete existing stack if present) ---"
|
mkdir -p ~/.ssh
|
||||||
DELETE_HTTP_CODE=$(curl -sS -o /tmp/portainer-delete.json -w '%{http_code}' \
|
echo "$NAS_SSH_KEY" > ~/.ssh/id_ed25519
|
||||||
-X DELETE \
|
chmod 600 ~/.ssh/id_ed25519
|
||||||
-H "X-API-Key: ${PORTAINER_TOKEN}" \
|
|
||||||
"${PORTAINER_URL}/api/stacks/${PORTAINER_STACK_ID}?endpointId=${PORTAINER_ENDPOINT_ID}")
|
echo "--- Pre-flight: ensure NAS is reachable ---"
|
||||||
echo "DELETE HTTP ${DELETE_HTTP_CODE}"
|
# NAS_HOST can be either "host" or "host:port"
|
||||||
if [ "${DELETE_HTTP_CODE}" != "204" ] && [ "${DELETE_HTTP_CODE}" != "404" ]; then
|
NAS_SSH_PORT=$(echo "$NAS_HOST" | grep -q ':' && echo "${NAS_HOST##*:}" || echo "22")
|
||||||
echo "ERROR: Portainer rejected DELETE on stack ${PORTAINER_STACK_ID}:" >&2
|
NAS_SSH_HOST="${NAS_HOST%%:*}"
|
||||||
cat /tmp/portainer-delete.json >&2
|
echo "Using NAS_SSH_HOST=$NAS_SSH_HOST NAS_SSH_PORT=$NAS_SSH_PORT"
|
||||||
|
ssh -o StrictHostKeyChecking=no -o ConnectTimeout=5 -p "$NAS_SSH_PORT" \
|
||||||
|
"$NAS_USER@$NAS_SSH_HOST" \
|
||||||
|
'export PATH=/share/CACHEDEV1_DATA/.qpkg/container-station/usr/bin:$PATH; hostname && docker version --format "{{.Server.Version}}"' \
|
||||||
|
| head
|
||||||
|
|
||||||
|
echo "--- Reading docker-compose.prod.yml from the repo ---"
|
||||||
|
if [ ! -f "$COMPOSE_FILE" ]; then
|
||||||
|
echo "ERROR: $COMPOSE_FILE not found in repo root" >&2
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "--- Step 2: read docker-compose.prod.yml ---"
|
echo "--- Redeploying stack '$STACK_NAME' on the NAS ---"
|
||||||
if [ ! -f docker-compose.prod.yml ]; then
|
# The NAS has Portainer and docker compose (v2) available. We stream
|
||||||
echo "ERROR: docker-compose.prod.yml is missing from the repo root" >&2
|
# the compose file over SSH and let docker compose recreate the
|
||||||
exit 1
|
# project. We pin the project name to 'davidaragon-portfolio' so the
|
||||||
fi
|
# volumes and networks of the existing stack are reused.
|
||||||
# Inline the compose file. Portainer expects `composeFileContent` as raw text.
|
cat "$COMPOSE_FILE" | ssh -o StrictHostKeyChecking=no -p "$NAS_SSH_PORT" \
|
||||||
COMPOSE_BODY=$(jq -Rs --arg compose "$(cat docker-compose.prod.yml)" \
|
"$NAS_USER@$NAS_SSH_HOST" \
|
||||||
'{composeFileContent: $compose, env: []}' < /dev/null)
|
"export PATH=/share/CACHEDEV1_DATA/.qpkg/container-station/usr/bin:\$PATH; cd /tmp && docker compose -p '$STACK_NAME' -f - up -d"
|
||||||
|
|
||||||
echo "--- Step 3: create fresh stack from docker-compose.prod.yml ---"
|
echo "--- Stack '$STACK_NAME' redeployed. Smoke-test: ---"
|
||||||
CREATE_HTTP_CODE=$(curl -sS -o /tmp/portainer-create.json -w '%{http_code}' \
|
sleep 5
|
||||||
-X POST \
|
curl -sS -o /dev/null -w 'http://localhost:3001/ -> HTTP=%{http_code}\n' \
|
||||||
-H "X-API-Key: ${PORTAINER_TOKEN}" \
|
--max-time 5 http://localhost:3001/ || true
|
||||||
-H "Content-Type: application/json" \
|
|
||||||
--data "${COMPOSE_BODY}" \
|
|
||||||
"${PORTAINER_URL}/api/stacks?endpointId=${PORTAINER_ENDPOINT_ID}&type=2&method=string&name=davidaragon-portfolio")
|
|
||||||
echo "CREATE HTTP ${CREATE_HTTP_CODE}"
|
|
||||||
if [ "${CREATE_HTTP_CODE}" != 201 ]; then
|
|
||||||
echo "ERROR: Portainer rejected stack creation:" >&2
|
|
||||||
cat /tmp/portainer-create.json >&2
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
echo "--- Step 4: smoke-test the freshly deployed stack ---"
|
echo "--- Done ---"
|
||||||
# Give the container a brief window to start before checking.
|
|
||||||
sleep 8
|
|
||||||
HEALTH=$(curl -sS -o /dev/null -w '%{http_code}' \
|
|
||||||
--max-time 5 \
|
|
||||||
"http://${PORTAINER_URL#http://}:3001/" 2>/dev/null || true)
|
|
||||||
echo "Health check on http://<host>:3001/ returned: ${HEALTH:-<timeout/unreachable>}"
|
|
||||||
# We log but don't fail the job if 3001 isn't reachable — the upstream
|
|
||||||
# proxy (Nginx Proxy Manager → davidaragon.impresion3d.pro) is a better
|
|
||||||
# place to wire a hard-fail check in a future iteration.
|
|
||||||
|
|
||||||
echo "--- Stack recreated successfully. ---"
|
|
||||||
Reference in New Issue
Block a user