1 Commits
v4 ... v5
Author SHA1 Message Date
tthandClaude Opus 5.5 380988c194 portainer-deploy: content-addressed Swarm configs from repo files (v5)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-26 14:24:36 +01:00
2 changed files with 133 additions and 58 deletions

No files matched your search

+18 -1
View File
@@ -115,7 +115,7 @@ live) are kept and never logged; only `vars` are overwritten. Images are
re-pulled (`pullImage`). re-pulled (`pullImage`).
```yaml ```yaml
- uses: https://git.rfa.cz/tth-pub/ci-actions/portainer-deploy@v4 - uses: https://git.rfa.cz/tth-pub/ci-actions/portainer-deploy@v5
with: with:
url: ${{ secrets.PORTAINER_URL }} url: ${{ secrets.PORTAINER_URL }}
token: ${{ secrets.PORTAINER_TOKEN }} token: ${{ secrets.PORTAINER_TOKEN }}
@@ -128,6 +128,23 @@ re-pulled (`pullImage`).
smoke-timeout: "180" smoke-timeout: "180"
``` ```
Config files from the repo can ride along as Swarm configs. Each becomes a
config named after its content hash, created when missing; the stack
variable points at it, so a changed file rolls the services that mount it,
and superseded versions are removed once nothing uses them:
```yaml
configs: |
CFG_NGINX=deploy/nginx.conf
config-prefix: myapp_
```
```yaml
# compose
configs:
nginx_conf: {external: true, name: "${CFG_NGINX}"}
```
Pin images in the compose file to the variable (`image: …:${IMAGE_TAG}`), so Pin images in the compose file to the variable (`image: …:${IMAGE_TAG}`), so
the deployed version is the commit that passed the gate, not whatever the deployed version is the commit that passed the gate, not whatever
`latest` points at when Swarm pulls. `latest` points at when Swarm pulls.
+115 -57
View File
@@ -4,7 +4,8 @@ description: >
compose file is the single source of truth: Portainer's stored compose is compose file is the single source of truth: Portainer's stored compose is
replaced on every deploy, the stack's env vars (where the secrets live) are replaced on every deploy, the stack's env vars (where the secrets live) are
kept and only the given variables are overwritten. Images are re-pulled. kept and only the given variables are overwritten. Images are re-pulled.
Optionally waits until a URL answers with the expected status. Config files from the repo can be shipped as content-addressed Swarm
configs. Optionally waits until a URL answers with the expected status.
inputs: inputs:
url: url:
@@ -29,6 +30,19 @@ inputs:
Stack variables to set, one NAME=value per line (e.g. IMAGE_TAG=sha-1234abcd). Stack variables to set, one NAME=value per line (e.g. IMAGE_TAG=sha-1234abcd).
Every other stack variable is kept as it is. Every other stack variable is kept as it is.
default: "" default: ""
configs:
description: >
Files shipped as Swarm configs, one VAR=path per line. Each file becomes
a config named <config-prefix><var>-<sha256[:12] of the content>
(created when missing) and the stack variable VAR is set to that name,
so the compose file references it as
`{external: true, name: "${VAR}"}`. A changed file = a new config =
the service is updated; older versions of the same VAR are removed once
no service uses them.
default: ""
config-prefix:
description: Prefix of the config names (e.g. the stack name and an underscore).
default: ""
prune: prune:
description: Remove services that are no longer in the compose file. description: Remove services that are no longer in the compose file.
default: "false" default: "false"
@@ -53,6 +67,8 @@ runs:
COMPOSE_FILE: ${{ inputs.compose-file }} COMPOSE_FILE: ${{ inputs.compose-file }}
ENDPOINT_ID: ${{ inputs.endpoint-id }} ENDPOINT_ID: ${{ inputs.endpoint-id }}
SET_VARS: ${{ inputs.vars }} SET_VARS: ${{ inputs.vars }}
CONFIGS: ${{ inputs.configs }}
CONFIG_PREFIX: ${{ inputs.config-prefix }}
PRUNE: ${{ inputs.prune }} PRUNE: ${{ inputs.prune }}
SMOKE_URL: ${{ inputs.smoke-url }} SMOKE_URL: ${{ inputs.smoke-url }}
SMOKE_STATUS: ${{ inputs.smoke-status }} SMOKE_STATUS: ${{ inputs.smoke-status }}
@@ -63,73 +79,115 @@ runs:
echo "::notice::portainer-deploy: no stack id, deploy skipped" echo "::notice::portainer-deploy: no stack id, deploy skipped"
exit 0 exit 0
fi fi
: "${PORTAINER_URL:?url input is empty}" "${PORTAINER_TOKEN:?token input is empty}" # The stack env carries the secrets: it only ever lives in this
[ -f "$COMPOSE_FILE" ] || { echo "::error::compose file $COMPOSE_FILE not found"; exit 1; } # process's memory, between the GET and the PUT, never in the log.
work="${RUNNER_TEMP:-/tmp}/portainer-deploy"; mkdir -p "$work"; chmod 700 "$work" python3 - <<'PY'
trap 'rm -rf "$work"' EXIT import base64, hashlib, json, os, ssl, sys, time, urllib.error, urllib.parse, urllib.request
base="${PORTAINER_URL%/}"
# The stack body carries the env (secrets): it goes from this GET into E = os.environ
# the PUT body through a private temp file and never to the log. base = E["PORTAINER_URL"].rstrip("/")
code=$(curl -sS -o "$work/stack.json" -w '%{http_code}' \ token = E["PORTAINER_TOKEN"]
-H "X-API-Key: $PORTAINER_TOKEN" "$base/api/stacks/$STACK_ID") || code=000 stack_id, endpoint = E["STACK_ID"], E.get("ENDPOINT_ID") or "1"
if [ "$code" != 200 ]; then if not base or not token:
echo "::error::Portainer GET /api/stacks/$STACK_ID returned HTTP $code" sys.exit("::error::portainer-deploy: url and token are required")
exit 1
fi
python3 - "$work/stack.json" "$COMPOSE_FILE" "$work/body.json" <<'PY' def api(method, path, body=None, ok=(200,)):
import json, os, sys req = urllib.request.Request(base + path, method=method,
stack_path, compose_path, out_path = sys.argv[1:] headers={"X-API-Key": token, "Content-Type": "application/json"},
with open(stack_path) as f: data=None if body is None else json.dumps(body).encode())
stack = json.load(f) try:
sets = {} with urllib.request.urlopen(req, timeout=120) as r:
for line in os.environ.get("SET_VARS", "").splitlines(): status, raw = r.status, r.read()
except urllib.error.HTTPError as e:
status, raw = e.code, e.read()
if status not in ok:
raise RuntimeError(f"{method} {path.split('?')[0]} -> HTTP {status}: {raw[:300].decode(errors='replace')}")
return status, (json.loads(raw) if raw.strip() else None)
def lines(name):
out = {}
for line in E.get(name, "").splitlines():
line = line.strip() line = line.strip()
if not line: if not line:
continue continue
name, sep, value = line.partition("=") key, sep, value = line.partition("=")
if not sep or not name: if not sep or not key.strip():
sys.exit(f"portainer-deploy: bad vars line (want NAME=value): {name!r}") sys.exit(f"::error::portainer-deploy: bad {name.lower()} line (want NAME=value): {key!r}")
sets[name.strip()] = value out[key.strip()] = value.strip() if name == "CONFIGS" else value
return out
try:
sets = lines("SET_VARS")
docker = f"/api/endpoints/{endpoint}/docker"
# Content-addressed configs: the name changes with the content, so
# a changed file rolls the services that mount it.
prefix = E.get("CONFIG_PREFIX", "")
stale = []
for var, path in lines("CONFIGS").items():
with open(path, "rb") as f:
data = f.read()
stem = f"{prefix}{var.lower()}"
name = f"{stem}-{hashlib.sha256(data).hexdigest()[:12]}"
flt = urllib.parse.quote(json.dumps({"label": [f"portainer-deploy.config={stem}"]}))
_, existing = api("GET", f"{docker}/configs?filters={flt}")
names = {c["Spec"]["Name"]: c["ID"] for c in existing or []}
if name in names:
print(f"config {name} ({path}) unchanged")
else:
api("POST", f"{docker}/configs/create", {
"Name": name,
"Data": base64.b64encode(data).decode(),
"Labels": {"portainer-deploy.config": stem},
}, ok=(200, 201))
print(f"config {name} created from {path}")
stale += [(n, i) for n, i in names.items() if n != name]
sets[var] = name
_, stack = api("GET", f"/api/stacks/{stack_id}")
env = [e for e in (stack.get("Env") or []) if e.get("name") not in sets] env = [e for e in (stack.get("Env") or []) if e.get("name") not in sets]
kept = len(env)
env += [{"name": k, "value": v} for k, v in sets.items()] env += [{"name": k, "value": v} for k, v in sets.items()]
with open(compose_path) as f: with open(E["COMPOSE_FILE"]) as f:
compose = f.read() compose = f.read()
body = { api("PUT", f"/api/stacks/{stack_id}?endpointId={endpoint}", {
"stackFileContent": compose, "stackFileContent": compose,
"env": env, "env": env,
"prune": os.environ.get("PRUNE", "false").lower() == "true", "prune": E.get("PRUNE", "false").lower() == "true",
"pullImage": True, "pullImage": True,
} })
fd = os.open(out_path, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600) shown = ", ".join(f"{k}={v}" for k, v in sets.items()) or "nothing"
with os.fdopen(fd, "w") as f: print(f"stack {stack.get('Name', stack_id)} updated from {E['COMPOSE_FILE']}: {shown}; {kept} other variable(s) kept")
json.dump(body, f)
kept = len(env) - len(sets)
print(f"stack {stack.get('Name', '?')}: setting {', '.join(f'{k}={v}' for k, v in sets.items()) or 'nothing'}; {kept} other variable(s) kept")
PY
code=$(curl -sS -o "$work/resp.json" -w '%{http_code}' -X PUT \ # Older versions go once nothing mounts them; Docker refuses to
"$base/api/stacks/$STACK_ID?endpointId=$ENDPOINT_ID" \ # delete a config in use (the previous task may still be draining),
-H "X-API-Key: $PORTAINER_TOKEN" -H "Content-Type: application/json" \ # the next deploy retries.
--data-binary "@$work/body.json") || code=000 for name, cid in stale:
if [ "$code" != 200 ]; then try:
echo "::error::Portainer stack PUT returned HTTP $code: $(head -c 300 "$work/resp.json" 2>/dev/null)" api("DELETE", f"{docker}/configs/{cid}", ok=(200, 204))
exit 1 print(f"config {name} removed (superseded)")
fi except RuntimeError:
echo "stack $STACK_ID updated from $COMPOSE_FILE" print(f"config {name} still in use, kept for the next deploy")
except (RuntimeError, OSError) as exc:
sys.exit(f"::error::portainer-deploy: {exc}")
[ -n "$SMOKE_URL" ] || exit 0 url = E.get("SMOKE_URL", "")
deadline=$(( $(date +%s) + SMOKE_TIMEOUT )) if url:
while :; do want, deadline = E.get("SMOKE_STATUS", "200"), time.time() + float(E.get("SMOKE_TIMEOUT") or 180)
got=$(curl -sk -o /dev/null -w '%{http_code}' --max-time 10 "$SMOKE_URL" 2>/dev/null) || got=000 ctx = ssl.create_default_context()
if [ "$got" = "$SMOKE_STATUS" ]; then ctx.check_hostname, ctx.verify_mode = False, ssl.CERT_NONE
echo "smoke: $SMOKE_URL answered $got" while True:
try:
with urllib.request.urlopen(url, timeout=10, context=ctx) as r:
got = str(r.status)
except urllib.error.HTTPError as e:
got = str(e.code)
except (urllib.error.URLError, OSError):
got = "000"
if got == want:
print(f"smoke: {url} answered {got}")
break break
fi if time.time() >= deadline:
if [ "$(date +%s)" -ge "$deadline" ]; then sys.exit(f"::error::smoke: {url} answered {got}, expected {want}")
echo "::error::smoke: $SMOKE_URL answered $got, expected $SMOKE_STATUS (waited ${SMOKE_TIMEOUT}s)" time.sleep(10)
exit 1 PY
fi
sleep 10
done