Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a3c4ae5edf |
No files matched your search
@@ -104,3 +104,31 @@ binary is verified by SHA-256.
|
|||||||
`v1` image gate + SBOM; `v2` fs scans, binary runtime, misconfig, gitleaks;
|
`v1` image gate + SBOM; `v2` fs scans, binary runtime, misconfig, gitleaks;
|
||||||
`v3` gitleaks fetches the parent of a single commit in a depth-1 checkout
|
`v3` gitleaks fetches the parent of a single commit in a depth-1 checkout
|
||||||
(dispatch/cron runs scanned the whole tree as a root commit before).
|
(dispatch/cron runs scanned the whole tree as a root commit before).
|
||||||
|
|
||||||
|
## portainer-deploy
|
||||||
|
|
||||||
|
Updates a Portainer Swarm stack from a compose file in the repository - the
|
||||||
|
last step of the build → `trivy-gate` → move `latest` → deploy pipeline. The
|
||||||
|
compose file in git 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 kept and never logged; only `vars` are overwritten. Images are
|
||||||
|
re-pulled (`pullImage`).
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
- uses: https://git.rfa.cz/tth-pub/ci-actions/portainer-deploy@v4
|
||||||
|
with:
|
||||||
|
url: ${{ secrets.PORTAINER_URL }}
|
||||||
|
token: ${{ secrets.PORTAINER_TOKEN }}
|
||||||
|
stack-id: ${{ secrets.PORTAINER_STACK_ID }} # empty = skip with a notice
|
||||||
|
compose-file: docker-compose.swarm.yml
|
||||||
|
vars: |
|
||||||
|
IMAGE_TAG=sha-${{ steps.build.outputs.sha }}
|
||||||
|
smoke-url: https://app.example.org/health # optional
|
||||||
|
smoke-status: "200"
|
||||||
|
smoke-timeout: "180"
|
||||||
|
```
|
||||||
|
|
||||||
|
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
|
||||||
|
`latest` points at when Swarm pulls.
|
||||||
|
|
||||||
@@ -0,0 +1,135 @@
|
|||||||
|
name: portainer-deploy
|
||||||
|
description: >
|
||||||
|
Update a Portainer Swarm stack from a compose file in the repository. The
|
||||||
|
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
|
||||||
|
kept and only the given variables are overwritten. Images are re-pulled.
|
||||||
|
Optionally waits until a URL answers with the expected status.
|
||||||
|
|
||||||
|
inputs:
|
||||||
|
url:
|
||||||
|
description: Portainer base URL.
|
||||||
|
required: true
|
||||||
|
token:
|
||||||
|
description: Portainer API key (X-API-Key).
|
||||||
|
required: true
|
||||||
|
stack-id:
|
||||||
|
description: >
|
||||||
|
Portainer stack id. Empty = skip the deploy with a notice (a repo that
|
||||||
|
has no stack yet).
|
||||||
|
default: ""
|
||||||
|
compose-file:
|
||||||
|
description: Compose file in the checkout to deploy.
|
||||||
|
required: true
|
||||||
|
endpoint-id:
|
||||||
|
description: Portainer environment (endpoint) id.
|
||||||
|
default: "1"
|
||||||
|
vars:
|
||||||
|
description: >
|
||||||
|
Stack variables to set, one NAME=value per line (e.g. IMAGE_TAG=sha-1234abcd).
|
||||||
|
Every other stack variable is kept as it is.
|
||||||
|
default: ""
|
||||||
|
prune:
|
||||||
|
description: Remove services that are no longer in the compose file.
|
||||||
|
default: "false"
|
||||||
|
smoke-url:
|
||||||
|
description: URL polled after the deploy until it answers smoke-status. Empty = no check.
|
||||||
|
default: ""
|
||||||
|
smoke-status:
|
||||||
|
description: Expected HTTP status of smoke-url.
|
||||||
|
default: "200"
|
||||||
|
smoke-timeout:
|
||||||
|
description: Seconds to wait for smoke-url.
|
||||||
|
default: "180"
|
||||||
|
|
||||||
|
runs:
|
||||||
|
using: composite
|
||||||
|
steps:
|
||||||
|
- shell: bash
|
||||||
|
env:
|
||||||
|
PORTAINER_URL: ${{ inputs.url }}
|
||||||
|
PORTAINER_TOKEN: ${{ inputs.token }}
|
||||||
|
STACK_ID: ${{ inputs.stack-id }}
|
||||||
|
COMPOSE_FILE: ${{ inputs.compose-file }}
|
||||||
|
ENDPOINT_ID: ${{ inputs.endpoint-id }}
|
||||||
|
SET_VARS: ${{ inputs.vars }}
|
||||||
|
PRUNE: ${{ inputs.prune }}
|
||||||
|
SMOKE_URL: ${{ inputs.smoke-url }}
|
||||||
|
SMOKE_STATUS: ${{ inputs.smoke-status }}
|
||||||
|
SMOKE_TIMEOUT: ${{ inputs.smoke-timeout }}
|
||||||
|
run: |
|
||||||
|
set -euo pipefail
|
||||||
|
if [ -z "$STACK_ID" ]; then
|
||||||
|
echo "::notice::portainer-deploy: no stack id, deploy skipped"
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
: "${PORTAINER_URL:?url input is empty}" "${PORTAINER_TOKEN:?token input is empty}"
|
||||||
|
[ -f "$COMPOSE_FILE" ] || { echo "::error::compose file $COMPOSE_FILE not found"; exit 1; }
|
||||||
|
work="${RUNNER_TEMP:-/tmp}/portainer-deploy"; mkdir -p "$work"; chmod 700 "$work"
|
||||||
|
trap 'rm -rf "$work"' EXIT
|
||||||
|
base="${PORTAINER_URL%/}"
|
||||||
|
|
||||||
|
# The stack body carries the env (secrets): it goes from this GET into
|
||||||
|
# the PUT body through a private temp file and never to the log.
|
||||||
|
code=$(curl -sS -o "$work/stack.json" -w '%{http_code}' \
|
||||||
|
-H "X-API-Key: $PORTAINER_TOKEN" "$base/api/stacks/$STACK_ID") || code=000
|
||||||
|
if [ "$code" != 200 ]; then
|
||||||
|
echo "::error::Portainer GET /api/stacks/$STACK_ID returned HTTP $code"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
python3 - "$work/stack.json" "$COMPOSE_FILE" "$work/body.json" <<'PY'
|
||||||
|
import json, os, sys
|
||||||
|
stack_path, compose_path, out_path = sys.argv[1:]
|
||||||
|
with open(stack_path) as f:
|
||||||
|
stack = json.load(f)
|
||||||
|
sets = {}
|
||||||
|
for line in os.environ.get("SET_VARS", "").splitlines():
|
||||||
|
line = line.strip()
|
||||||
|
if not line:
|
||||||
|
continue
|
||||||
|
name, sep, value = line.partition("=")
|
||||||
|
if not sep or not name:
|
||||||
|
sys.exit(f"portainer-deploy: bad vars line (want NAME=value): {name!r}")
|
||||||
|
sets[name.strip()] = value
|
||||||
|
env = [e for e in (stack.get("Env") or []) if e.get("name") not in sets]
|
||||||
|
env += [{"name": k, "value": v} for k, v in sets.items()]
|
||||||
|
with open(compose_path) as f:
|
||||||
|
compose = f.read()
|
||||||
|
body = {
|
||||||
|
"stackFileContent": compose,
|
||||||
|
"env": env,
|
||||||
|
"prune": os.environ.get("PRUNE", "false").lower() == "true",
|
||||||
|
"pullImage": True,
|
||||||
|
}
|
||||||
|
fd = os.open(out_path, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600)
|
||||||
|
with os.fdopen(fd, "w") as f:
|
||||||
|
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 \
|
||||||
|
"$base/api/stacks/$STACK_ID?endpointId=$ENDPOINT_ID" \
|
||||||
|
-H "X-API-Key: $PORTAINER_TOKEN" -H "Content-Type: application/json" \
|
||||||
|
--data-binary "@$work/body.json") || code=000
|
||||||
|
if [ "$code" != 200 ]; then
|
||||||
|
echo "::error::Portainer stack PUT returned HTTP $code: $(head -c 300 "$work/resp.json" 2>/dev/null)"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
echo "stack $STACK_ID updated from $COMPOSE_FILE"
|
||||||
|
|
||||||
|
[ -n "$SMOKE_URL" ] || exit 0
|
||||||
|
deadline=$(( $(date +%s) + SMOKE_TIMEOUT ))
|
||||||
|
while :; do
|
||||||
|
got=$(curl -sk -o /dev/null -w '%{http_code}' --max-time 10 "$SMOKE_URL" 2>/dev/null) || got=000
|
||||||
|
if [ "$got" = "$SMOKE_STATUS" ]; then
|
||||||
|
echo "smoke: $SMOKE_URL answered $got"
|
||||||
|
break
|
||||||
|
fi
|
||||||
|
if [ "$(date +%s)" -ge "$deadline" ]; then
|
||||||
|
echo "::error::smoke: $SMOKE_URL answered $got, expected $SMOKE_STATUS (waited ${SMOKE_TIMEOUT}s)"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
sleep 10
|
||||||
|
done
|
||||||
Reference in new issue
Block a user