Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ab177ecb46 | ||
|
|
c0d6ecf9f6 | ||
|
|
979335fe31 | ||
|
|
6ee8b2da6a | ||
|
|
26468ba73d | ||
|
|
b90b98bafb | ||
|
|
7c8359099c | ||
|
|
94d0ba6b78 | ||
|
|
52fab24049 | ||
|
|
1d363f0eac | ||
|
|
dfe768f871 | ||
|
|
fc400e752d | ||
|
|
78d295d616 | ||
|
|
55b6b50672 | ||
|
|
e82c65a3c4 | ||
|
|
ade9877f07 | ||
|
|
371eedc0ef | ||
|
|
2656687937 | ||
|
|
6a93557620 | ||
|
|
632d2d7465 | ||
|
|
380988c194 |
No files matched your search
@@ -0,0 +1,47 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
# Recompute the pinned SHA-256 of the Trivy and gitleaks release tarballs
|
||||||
|
# after a version bump. Renovate runs it as a postUpgradeTask (tth/renovate
|
||||||
|
# config.json), so a bump PR carries matching checksums; by hand:
|
||||||
|
# bash .gitea/update-checksums.sh
|
||||||
|
#
|
||||||
|
# Each tarball is downloaded and hashed here, and the hash must ALSO match
|
||||||
|
# the release's own checksums file - the pin is what we verified at bump
|
||||||
|
# time, the action then refuses any other bytes.
|
||||||
|
set -euo pipefail
|
||||||
|
cd "$(dirname "$0")/.."
|
||||||
|
work=$(mktemp -d)
|
||||||
|
trap 'rm -rf "$work"' EXIT
|
||||||
|
|
||||||
|
# pin <action.yml> <version var> <repo> <name prefix> <arch suffix> <sha var>...
|
||||||
|
pin() {
|
||||||
|
local file=$1 vvar=$2 repo=$3 prefix=$4
|
||||||
|
shift 4
|
||||||
|
local ver
|
||||||
|
ver=$(sed -n "s/^ *$vvar: *\"\([0-9.]*\)\".*/\1/p" "$file")
|
||||||
|
[ -n "$ver" ] || {
|
||||||
|
echo "no $vvar in $file" >&2
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
local base="https://github.com/$repo/releases/download/v$ver"
|
||||||
|
curl -fsSL --retry 3 -o "$work/sums" "$base/${prefix}_${ver}_checksums.txt"
|
||||||
|
while [ "$#" -gt 0 ]; do
|
||||||
|
local arch=$1 svar=$2
|
||||||
|
shift 2
|
||||||
|
local tgz="${prefix}_${ver}_${arch}.tar.gz"
|
||||||
|
curl -fsSL --retry 3 -o "$work/$tgz" "$base/$tgz"
|
||||||
|
local got want
|
||||||
|
got=$(sha256sum "$work/$tgz" | cut -d' ' -f1)
|
||||||
|
want=$(awk -v f="$tgz" '$2 == f {print $1}' "$work/sums")
|
||||||
|
[ "$got" = "$want" ] || {
|
||||||
|
echo "$tgz: downloaded $got, release says ${want:-nothing}" >&2
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
sed -i "s/^\( *$svar: *\)[0-9a-f]\{64\}/\1$got/" "$file"
|
||||||
|
echo "$tgz $got"
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
|
pin trivy-gate/action.yml TRIVY_VERSION aquasecurity/trivy trivy \
|
||||||
|
Linux-64bit TRIVY_SHA256_AMD64 Linux-ARM64 TRIVY_SHA256_ARM64
|
||||||
|
pin gitleaks/action.yml GITLEAKS_VERSION gitleaks/gitleaks gitleaks \
|
||||||
|
linux_x64 GITLEAKS_SHA256_X64 linux_arm64 GITLEAKS_SHA256_ARM64
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
# PR check (Renovate bumps of Trivy / gitleaks, and any other PR): the
|
||||||
|
# actions of THIS branch must actually run. Both runtimes of trivy-gate:
|
||||||
|
# the Trivy container (docker job) and the pinned binary with its checksum
|
||||||
|
# check (container job without Docker) - a bump with a wrong checksum fails
|
||||||
|
# here, not in every pipeline that uses the next tag. Required by branch
|
||||||
|
# protection, so a green PR automerges and tag.yml cuts the next vN.
|
||||||
|
name: pr-check
|
||||||
|
on:
|
||||||
|
pull_request:
|
||||||
|
|
||||||
|
concurrency:
|
||||||
|
group: pr-check-${{ github.head_ref }}
|
||||||
|
cancel-in-progress: true
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
docker:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v7
|
||||||
|
- name: trivy-gate, Trivy container, image scan
|
||||||
|
id: image
|
||||||
|
uses: ./trivy-gate
|
||||||
|
with:
|
||||||
|
image: alpine:3.22@sha256:2e0b81d60597f449baa56dc25d355367869d2dca4cd57e866f1e2b52af3eb55f
|
||||||
|
runtime: docker
|
||||||
|
mode: warn
|
||||||
|
- name: The report is real
|
||||||
|
run: |
|
||||||
|
set -euo pipefail
|
||||||
|
jq -e '.Results | length > 0' "${{ steps.image.outputs.report }}" >/dev/null
|
||||||
|
echo "blocking=${{ steps.image.outputs.blocking }}"
|
||||||
|
- name: gitleaks
|
||||||
|
uses: ./gitleaks
|
||||||
|
with:
|
||||||
|
mode: block
|
||||||
|
|
||||||
|
binary:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
# No Docker CLI here, so trivy-gate falls back to the pinned binary.
|
||||||
|
container:
|
||||||
|
image: node:26-trixie-slim@sha256:e6bedef217677b276c591795e169840edd9096dbf13f29df8ef8d1856b8820bc
|
||||||
|
steps:
|
||||||
|
- name: Tools the actions expect
|
||||||
|
run: apt-get update -qq && apt-get install -y -qq --no-install-recommends ca-certificates curl git jq >/dev/null
|
||||||
|
- uses: actions/checkout@v7
|
||||||
|
- name: trivy-gate, pinned binary, fs scan
|
||||||
|
id: fs
|
||||||
|
uses: ./trivy-gate
|
||||||
|
with:
|
||||||
|
scan-type: fs
|
||||||
|
runtime: binary
|
||||||
|
mode: warn
|
||||||
|
- name: The report is real
|
||||||
|
run: jq -e 'has("Results") or has("ArtifactName")' "${{ steps.fs.outputs.report }}" >/dev/null
|
||||||
|
- name: gitleaks, pinned binary
|
||||||
|
uses: ./gitleaks
|
||||||
|
with:
|
||||||
|
mode: block
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
# Cut the next major tag (vN) when a Renovate bump of a pinned tool lands on
|
||||||
|
# main, so the pipelines get it through their own Renovate PR ("update
|
||||||
|
# ci-actions to vN"). Only renovate-bot's merges: hand-made changes keep
|
||||||
|
# being tagged by hand, with the README note.
|
||||||
|
name: tag
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: [main]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
tag:
|
||||||
|
if: github.actor == 'renovate-bot'
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
permissions:
|
||||||
|
contents: write
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v7
|
||||||
|
with:
|
||||||
|
fetch-depth: 0
|
||||||
|
- name: Next vN on HEAD
|
||||||
|
run: |
|
||||||
|
set -euo pipefail
|
||||||
|
git fetch -q --tags
|
||||||
|
if git tag --points-at HEAD | grep -qE '^v[0-9]+$'; then
|
||||||
|
echo "HEAD already tagged: $(git tag --points-at HEAD | tr '\n' ' ')"; exit 0
|
||||||
|
fi
|
||||||
|
last=$(git tag -l 'v[0-9]*' | grep -E '^v[0-9]+$' | sed 's/^v//' | sort -n | tail -1)
|
||||||
|
next="v$((${last:-0} + 1))"
|
||||||
|
git tag "$next" HEAD
|
||||||
|
git push origin "refs/tags/$next"
|
||||||
|
echo "tagged $next on $(git rev-parse --short HEAD): $(git log -1 --format=%s)"
|
||||||
@@ -104,6 +104,20 @@ 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).
|
||||||
|
`v8` dtrack-sbom really never fails the job: the runner's `bash -e` turned
|
||||||
|
an unreachable Dependency-Track into a failed build instead of a warning.
|
||||||
|
`v12` portainer-deploy waits for the Swarm rollout before the smoke check
|
||||||
|
and fails on a paused or rolled-back update: the smoke URL used to answer
|
||||||
|
from the old task, so a new image that never started still gave a green
|
||||||
|
deploy.
|
||||||
|
|
||||||
|
Pinned tools (Trivy binary + image, gitleaks) are bumped by Renovate
|
||||||
|
(tth/renovate). Its post-upgrade task `.gitea/update-checksums.sh`
|
||||||
|
recomputes the tarball SHA-256 pins (downloaded and checked against the
|
||||||
|
release's checksums file); `.gitea/workflows/pr-check.yml` runs the actions
|
||||||
|
of the PR branch with both Trivy runtimes; after the merge `tag.yml` cuts
|
||||||
|
the next `vN`, which the pipelines then pick up through their own Renovate
|
||||||
|
PR. Hand-made changes are tagged by hand as before.
|
||||||
|
|
||||||
## portainer-deploy
|
## portainer-deploy
|
||||||
|
|
||||||
@@ -112,10 +126,14 @@ last step of the build → `trivy-gate` → move `latest` → deploy pipeline. T
|
|||||||
compose file in git is the single source of truth: Portainer's stored
|
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
|
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
|
live) are kept and never logged; only `vars` are overwritten. Images are
|
||||||
re-pulled (`pullImage`).
|
re-pulled (`pullImage`). The step then waits until every service the deploy
|
||||||
|
changed has finished its Swarm update (`wait-rollout`, `rollout-timeout`,
|
||||||
|
default 600 s incl. image pulls) and fails when the update is paused or
|
||||||
|
rolled back; only then is `smoke-url` checked. Services the deploy did not
|
||||||
|
touch are ignored, even when an older update of theirs is still paused.
|
||||||
|
|
||||||
```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 }}
|
||||||
@@ -126,6 +144,24 @@ re-pulled (`pullImage`).
|
|||||||
smoke-url: https://app.example.org/health # optional
|
smoke-url: https://app.example.org/health # optional
|
||||||
smoke-status: "200"
|
smoke-status: "200"
|
||||||
smoke-timeout: "180"
|
smoke-timeout: "180"
|
||||||
|
rollout-timeout: "600" # optional; wait-rollout: "false" = old behaviour
|
||||||
|
```
|
||||||
|
|
||||||
|
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
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ inputs:
|
|||||||
default: "5"
|
default: "5"
|
||||||
trivy-image:
|
trivy-image:
|
||||||
description: Trivy container image, pinned by digest.
|
description: Trivy container image, pinned by digest.
|
||||||
default: aquasec/trivy:0.75.0@sha256:af6acf9a6b85dfe389a1941505c0ce9efef52a4719635e1a962f022a3d855daa
|
default: aquasec/trivy:0.76.2@sha256:8f6bb947dbc528902e11f2f5be52f7b519f6a39d5d148f41166f2040499768fb
|
||||||
|
|
||||||
runs:
|
runs:
|
||||||
using: composite
|
using: composite
|
||||||
@@ -49,7 +49,11 @@ runs:
|
|||||||
MIN: ${{ inputs.min-components }}
|
MIN: ${{ inputs.min-components }}
|
||||||
TRIVY: ${{ inputs.trivy-image }}
|
TRIVY: ${{ inputs.trivy-image }}
|
||||||
run: |
|
run: |
|
||||||
set -uo pipefail
|
# The runner starts composite bash steps with -e; this script handles
|
||||||
|
# every failure itself (warn, exit 0), so -e only turns an unguarded
|
||||||
|
# command into a failed job - e.g. the cat of a resp.json curl never
|
||||||
|
# wrote when Dependency-Track was unreachable.
|
||||||
|
set +e -uo pipefail
|
||||||
warn() { echo "::warning::SBOM $PROJECT/$VERSION: $*"; exit 0; }
|
warn() { echo "::warning::SBOM $PROJECT/$VERSION: $*"; exit 0; }
|
||||||
[ -n "$REPORT" ] && [ -f "$REPORT" ] || warn "no report (gate skipped?)"
|
[ -n "$REPORT" ] && [ -f "$REPORT" ] || warn "no report (gate skipped?)"
|
||||||
[ -n "$DT_KEY" ] || warn "no API key"
|
[ -n "$DT_KEY" ] || warn "no API key"
|
||||||
|
|||||||
+197
-69
@@ -4,7 +4,10 @@ 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. Waits until Swarm has finished rolling out the services the
|
||||||
|
deploy changed (a paused or rolled-back update fails the step), then
|
||||||
|
optionally until a URL answers with the expected status.
|
||||||
|
|
||||||
inputs:
|
inputs:
|
||||||
url:
|
url:
|
||||||
@@ -29,11 +32,35 @@ 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"
|
||||||
|
wait-rollout:
|
||||||
|
description: >
|
||||||
|
Wait until every service this deploy changed has finished its Swarm
|
||||||
|
update; fail when the update is paused or rolled back. "false" = return
|
||||||
|
right after the stack update (the old behaviour).
|
||||||
|
default: "true"
|
||||||
|
rollout-timeout:
|
||||||
|
description: Seconds to wait for the rollout (image pulls included).
|
||||||
|
default: "600"
|
||||||
smoke-url:
|
smoke-url:
|
||||||
description: URL polled after the deploy until it answers smoke-status. Empty = no check.
|
description: >
|
||||||
|
URL polled after the rollout until it answers smoke-status. Empty = no
|
||||||
|
check.
|
||||||
default: ""
|
default: ""
|
||||||
smoke-status:
|
smoke-status:
|
||||||
description: Expected HTTP status of smoke-url.
|
description: Expected HTTP status of smoke-url.
|
||||||
@@ -53,7 +80,11 @@ 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 }}
|
||||||
|
WAIT_ROLLOUT: ${{ inputs.wait-rollout }}
|
||||||
|
ROLLOUT_TIMEOUT: ${{ inputs.rollout-timeout }}
|
||||||
SMOKE_URL: ${{ inputs.smoke-url }}
|
SMOKE_URL: ${{ inputs.smoke-url }}
|
||||||
SMOKE_STATUS: ${{ inputs.smoke-status }}
|
SMOKE_STATUS: ${{ inputs.smoke-status }}
|
||||||
SMOKE_TIMEOUT: ${{ inputs.smoke-timeout }}
|
SMOKE_TIMEOUT: ${{ inputs.smoke-timeout }}
|
||||||
@@ -63,73 +94,170 @@ 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()
|
||||||
line = line.strip()
|
except urllib.error.HTTPError as e:
|
||||||
if not line:
|
status, raw = e.code, e.read()
|
||||||
continue
|
if status not in ok:
|
||||||
name, sep, value = line.partition("=")
|
raise RuntimeError(f"{method} {path.split('?')[0]} -> HTTP {status}: {raw[:300].decode(errors='replace')}")
|
||||||
if not sep or not name:
|
return status, (json.loads(raw) if raw.strip() else None)
|
||||||
sys.exit(f"portainer-deploy: bad vars line (want NAME=value): {name!r}")
|
|
||||||
sets[name.strip()] = value
|
def lines(name):
|
||||||
env = [e for e in (stack.get("Env") or []) if e.get("name") not in sets]
|
out = {}
|
||||||
env += [{"name": k, "value": v} for k, v in sets.items()]
|
for line in E.get(name, "").splitlines():
|
||||||
with open(compose_path) as f:
|
line = line.strip()
|
||||||
compose = f.read()
|
if not line:
|
||||||
body = {
|
continue
|
||||||
"stackFileContent": compose,
|
key, sep, value = line.partition("=")
|
||||||
"env": env,
|
if not sep or not key.strip():
|
||||||
"prune": os.environ.get("PRUNE", "false").lower() == "true",
|
sys.exit(f"::error::portainer-deploy: bad {name.lower()} line (want NAME=value): {key!r}")
|
||||||
"pullImage": True,
|
out[key.strip()] = value.strip() if name == "CONFIGS" else value
|
||||||
}
|
return out
|
||||||
fd = os.open(out_path, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600)
|
|
||||||
with os.fdopen(fd, "w") as f:
|
try:
|
||||||
json.dump(body, f)
|
sets = lines("SET_VARS")
|
||||||
kept = len(env) - len(sets)
|
docker = f"/api/endpoints/{endpoint}/docker"
|
||||||
print(f"stack {stack.get('Name', '?')}: setting {', '.join(f'{k}={v}' for k, v in sets.items()) or 'nothing'}; {kept} other variable(s) kept")
|
|
||||||
|
# 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}")
|
||||||
|
|
||||||
|
# Rollout: remember each service's last update start before the
|
||||||
|
# PUT; afterwards only the services whose update start changed are
|
||||||
|
# this deploy's. Comparing with the previous value, not with the
|
||||||
|
# clock, keeps runner/Swarm clock skew out of it. Without this the
|
||||||
|
# smoke check below answered from the OLD task (birdnet-go
|
||||||
|
# 2026-11-03: green CI, then 1 min of 503) and a paused update
|
||||||
|
# went unnoticed (revaj-crm-stage 2026-11-02).
|
||||||
|
wait = E.get("WAIT_ROLLOUT", "true").lower() == "true"
|
||||||
|
sflt = urllib.parse.quote(json.dumps(
|
||||||
|
{"label": [f"com.docker.stack.namespace={stack.get('Name', '')}"]}))
|
||||||
|
|
||||||
|
def services():
|
||||||
|
_, out = api("GET", f"{docker}/services?filters={sflt}")
|
||||||
|
return {s["ID"]: s for s in out or []}
|
||||||
|
|
||||||
|
before = {i: (s.get("UpdateStatus") or {}).get("StartedAt")
|
||||||
|
for i, s in services().items()} if wait else {}
|
||||||
|
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()]
|
||||||
|
with open(E["COMPOSE_FILE"]) as f:
|
||||||
|
compose = f.read()
|
||||||
|
api("PUT", f"/api/stacks/{stack_id}?endpointId={endpoint}", {
|
||||||
|
"stackFileContent": compose,
|
||||||
|
"env": env,
|
||||||
|
"prune": E.get("PRUNE", "false").lower() == "true",
|
||||||
|
"pullImage": True,
|
||||||
|
})
|
||||||
|
shown = ", ".join(f"{k}={v}" for k, v in sets.items()) or "nothing"
|
||||||
|
print(f"stack {stack.get('Name', stack_id)} updated from {E['COMPOSE_FILE']}: {shown}; {kept} other variable(s) kept")
|
||||||
|
|
||||||
|
if wait:
|
||||||
|
rollout_timeout = float(E.get("ROLLOUT_TIMEOUT") or 600)
|
||||||
|
start = time.time()
|
||||||
|
deadline, grace, shown_at = start + rollout_timeout, start + 20, 0.0
|
||||||
|
while True:
|
||||||
|
pending, failed, done = [], [], []
|
||||||
|
for i, s in services().items():
|
||||||
|
u = s.get("UpdateStatus") or {}
|
||||||
|
if not u or (i in before and u.get("StartedAt") == before[i]):
|
||||||
|
continue # untouched by this deploy, or newly created
|
||||||
|
name, state = s["Spec"]["Name"], u.get("State", "")
|
||||||
|
if state == "completed":
|
||||||
|
done.append(name)
|
||||||
|
elif state == "updating":
|
||||||
|
pending.append(name)
|
||||||
|
else: # paused, rollback_started / _paused / _completed
|
||||||
|
failed.append(f"{name}: {state} ({u.get('Message', '')})")
|
||||||
|
if failed:
|
||||||
|
raise RuntimeError("rollout failed - " + "; ".join(failed))
|
||||||
|
if pending:
|
||||||
|
if time.time() >= deadline:
|
||||||
|
raise RuntimeError(f"rollout not finished after {rollout_timeout:.0f} s: {', '.join(pending)}")
|
||||||
|
if time.time() - shown_at >= 30:
|
||||||
|
print(f"rollout: waiting for {', '.join(pending)}", flush=True)
|
||||||
|
shown_at = time.time()
|
||||||
|
elif done:
|
||||||
|
print(f"rollout: completed in {time.time() - start:.0f} s: {', '.join(done)}")
|
||||||
|
break
|
||||||
|
elif time.time() >= grace:
|
||||||
|
# The PUT can return before Swarm registers the update
|
||||||
|
# (birdnet-go: PUT logged 00:29:25.908, update started
|
||||||
|
# 25.929), hence the grace. Nothing after it = same
|
||||||
|
# image digest and configs, Swarm had nothing to update.
|
||||||
|
print("rollout: no service was updated by this deploy")
|
||||||
|
break
|
||||||
|
time.sleep(5)
|
||||||
|
|
||||||
|
# Older versions go once nothing mounts them; Docker refuses to
|
||||||
|
# delete a config in use (the previous task may still be draining),
|
||||||
|
# the next deploy retries.
|
||||||
|
for name, cid in stale:
|
||||||
|
try:
|
||||||
|
api("DELETE", f"{docker}/configs/{cid}", ok=(200, 204))
|
||||||
|
print(f"config {name} removed (superseded)")
|
||||||
|
except RuntimeError:
|
||||||
|
print(f"config {name} still in use, kept for the next deploy")
|
||||||
|
except (RuntimeError, OSError) as exc:
|
||||||
|
sys.exit(f"::error::portainer-deploy: {exc}")
|
||||||
|
|
||||||
|
url = E.get("SMOKE_URL", "")
|
||||||
|
if url:
|
||||||
|
want, deadline = E.get("SMOKE_STATUS", "200"), time.time() + float(E.get("SMOKE_TIMEOUT") or 180)
|
||||||
|
ctx = ssl.create_default_context()
|
||||||
|
ctx.check_hostname, ctx.verify_mode = False, ssl.CERT_NONE
|
||||||
|
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
|
||||||
|
if time.time() >= deadline:
|
||||||
|
sys.exit(f"::error::smoke: {url} answered {got}, expected {want}")
|
||||||
|
time.sleep(10)
|
||||||
PY
|
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
|
|
||||||
@@ -50,7 +50,7 @@ inputs:
|
|||||||
default: "false"
|
default: "false"
|
||||||
trivy-image:
|
trivy-image:
|
||||||
description: Trivy container image, pinned by digest.
|
description: Trivy container image, pinned by digest.
|
||||||
default: aquasec/trivy:0.75.0@sha256:af6acf9a6b85dfe389a1941505c0ce9efef52a4719635e1a962f022a3d855daa
|
default: aquasec/trivy:0.76.2@sha256:8f6bb947dbc528902e11f2f5be52f7b519f6a39d5d148f41166f2040499768fb
|
||||||
cache-volume:
|
cache-volume:
|
||||||
description: >
|
description: >
|
||||||
Docker named volume for the vulnerability DB. A named volume, not a
|
Docker named volume for the vulnerability DB. A named volume, not a
|
||||||
@@ -77,9 +77,9 @@ runs:
|
|||||||
SCAN_REF: ${{ inputs.scan-ref }}
|
SCAN_REF: ${{ inputs.scan-ref }}
|
||||||
RUNTIME: ${{ inputs.runtime }}
|
RUNTIME: ${{ inputs.runtime }}
|
||||||
SCANNERS: ${{ inputs.scanners }}
|
SCANNERS: ${{ inputs.scanners }}
|
||||||
TRIVY_VERSION: "0.75.0"
|
TRIVY_VERSION: "0.76.2"
|
||||||
TRIVY_SHA256_AMD64: c6e65abddb348e25f10549df887045629cf28cc72453cd1c63acb717316b3f3f
|
TRIVY_SHA256_AMD64: bc0fc726e017b8927a62b2d3531d7a84ee01769daf5c99ed866dced54089fdbf
|
||||||
TRIVY_SHA256_ARM64: a1ee9f6ffb7d112b64ff726a2a0717c21175c1114361391f4a132956751a13b3
|
TRIVY_SHA256_ARM64: ab9d51f86e6a5fb936f605faff6b593ac51bc378fff4d3e8b9d58358b9c367c3
|
||||||
TRIVY_USERNAME: ${{ inputs.username }}
|
TRIVY_USERNAME: ${{ inputs.username }}
|
||||||
TRIVY_PASSWORD: ${{ inputs.password }}
|
TRIVY_PASSWORD: ${{ inputs.password }}
|
||||||
PLATFORM: ${{ inputs.platform }}
|
PLATFORM: ${{ inputs.platform }}
|
||||||
|
|||||||
Reference in new issue
Block a user