Deploy from CI with an approval step¶
This page shows how to run piceli deploy from GitHub Actions: on every push
the pushed commit is planned, a reviewer approves that exact plan, and a
second job applies it, from the same commit, with the same inputs, on any
runner: the jobs share no disk and no build cache, only the cluster (where
the deployment state lives, see Share deployment state between runners) and the plan file.
Maturity: preview
The recipe uses piceli deploy --ref (preview, see Deploy a commit, not the working tree). The
sample workflow is run by Piceli’s own tests against a fake cluster, so its
commands match the CLI of this version.
How it works¶
Job |
Runs |
Approval |
|---|---|---|
|
|
None: |
|
|
A protected GitHub environment: required reviewers approve after reading the plan |
|
|
The same environment |
The approval is bound twice. GitHub lets apply start only after a reviewer
approved the production environment for this workflow run, and Piceli runs
it only when the plan file’s hash is the approved one and planning again
on the apply runner, against live state, gives the same combined hash. The hash covers the commit (--ref pins it by SHA), the
staged build inputs, the builders, the delivery, the release’s actions (or
the rendered model while the images are not built yet), the checks and the
target. When anything changed in between (a new object in the cluster,
another commit, a different model), apply is refused with
pipeline-plan-changed (exit 2) and nothing runs: push again or re-run
the workflow to get a new plan.
When the commit’s images were never built, the plan still shows the
release as a preview with placeholder images: what it would create,
adopt, replace or delete, and any blocking objects (see
Preview the release before the images exist). The hash then binds the build inputs, the rendered
model and the preview’s adopt/replace/delete set. The real release plan is
computed after the build and delivery, under the same approval, and it is
refused (pipeline-preview-changed) if it would adopt, replace or delete
anything the reviewed preview did not show.
Prerequisites¶
A pipeline module whose builds declare their sources in an
inputs.toml(see Source identity), for exampledeploy/app.py, with shared state (state="cluster"). It reads the kubeconfig path and the state directory (a per-job working copy) from the environment and names its context explicitly:import os from piceli import Pipeline, Target target = Target.kubeconfig( os.environ.get("DEPLOY_KUBECONFIG", "cluster.kubeconfig"), context="my-cluster", # explicit; never the current context namespace="my-app", ) pipeline = Pipeline( app, target, build=images, deliver=..., state="cluster", # journal, catalog, secrets and lock in the namespace state_dir=os.environ.get("DEPLOY_STATE_DIR", ".piceli-deploy"), )
Self-hosted runners that reach the cluster’s API server and have Docker with
buildx(andkubectlfor a node-loopback registry), with a label such asdeploy-my-app. Any of them can run any job.A repository secret
DEPLOY_KUBECONFIGwith a kubeconfig that holds only the context the pipeline declares. The plan needs it too: it reads the cluster, runs server-side dry runs (the same rights as the apply) and writes the shared state. Besides the release’s own objects it needsget,create,patchanddeleteonsecretsandleasesin the namespace (see Share deployment state between runners).An environment
production(Settings → Environments) with required reviewers, and “Prevent self-review” if the pusher must not approve alone.
Steps¶
Copy
examples/ci/github-actions-deploy.ymlto.github/workflows/deploy.ymlin your repository and adaptPIPELINE, the runner labels and the concurrency group:# Deploy on push with Piceli: plan the pushed commit, wait for a reviewer, # then apply exactly the plan they approved. # # Copy this file to .github/workflows/deploy.yml in your repository and adapt # the `env` block and the runner labels. The guide is docs/ci.md # (https://docs.pynenc.org/projects/piceli/en/stable/ci.html). # # The apply and resume jobs post the run's summary (summary.md) as the job # summary and keep summary.json (for agents) in their artifact. # # Piceli's own tests run the `piceli deploy` command lines of this file, in # this order, each job in its own checkout and state directory, against a # fake cluster (tests/acceptance/test_ci_recipe.py), so the recipe cannot # drift from the CLI. # # Needs, once per repository: # - a pipeline with shared state: Pipeline(..., state="cluster") keeps the run # journal, release catalog and secret store in the release namespace, behind # a release lock, so plan, apply and resume may run on different runners; # - self-hosted runners that reach the cluster API and have Docker, labelled # `deploy-my-app` (they share nothing: no disk, no build cache); # - a repository secret DEPLOY_KUBECONFIG holding a kubeconfig with the one # context the pipeline declares (Target.kubeconfig(..., context=...)); # - an environment `production` with required reviewers. name: deploy on: push: branches: [main] workflow_dispatch: inputs: resume: description: Continue the latest interrupted deploy run (same commits) type: boolean default: false # One deploy of this target at a time; never cancel a deploy that is applying. concurrency: group: deploy-my-app cancel-in-progress: false permissions: contents: read env: # The pipeline, relative to the repository root. PIPELINE: deploy/app.py:pipeline jobs: plan: if: ${{ !inputs.resume }} runs-on: [self-hosted, deploy-my-app] outputs: combined_hash: ${{ steps.plan.outputs.combined_hash }} steps: - uses: actions/checkout@v5 - uses: astral-sh/setup-uv@v7 - name: Write the kubeconfig (mode 0600, never printed) env: KUBECONFIG_CONTENT: ${{ secrets.DEPLOY_KUBECONFIG }} run: | umask 077 printf '%s\n' "$KUBECONFIG_CONTENT" > "$RUNNER_TEMP/deploy.kubeconfig" echo "DEPLOY_KUBECONFIG=$RUNNER_TEMP/deploy.kubeconfig" >> "$GITHUB_ENV" # A fresh working copy of the shared state (Pipeline(state_dir=...)). echo "DEPLOY_STATE_DIR=$RUNNER_TEMP/piceli-state" >> "$GITHUB_ENV" - name: Plan the pushed commit id: plan run: | uv run --frozen piceli deploy "$PIPELINE" --ref "$GITHUB_SHA" --plan --out deploy-plan.json --json > plan.jsonl 2> plan.txt || { cat plan.txt >&2; exit 1; } jq -c 'select(.event == "result")' plan.jsonl > plan.json echo "combined_hash=$(jq -r .combined_hash plan.json)" >> "$GITHUB_OUTPUT" { echo "### Deploy plan for \`$GITHUB_SHA\`" echo echo "Combined hash: \`$(jq -r .combined_hash plan.json)\`" echo echo '```' cat plan.txt echo '```' } >> "$GITHUB_STEP_SUMMARY" - uses: actions/upload-artifact@v6 with: name: deploy-plan path: | deploy-plan.json plan.json plan.txt - name: Remove the kubeconfig and the state copy if: always() run: rm -rf "$RUNNER_TEMP/deploy.kubeconfig" "$RUNNER_TEMP/piceli-state" apply: needs: plan # Any runner with the label: the plan file and the shared state are all # it needs from the plan job. runs-on: [self-hosted, deploy-my-app] # Required reviewers approve this environment after reading the plan in # the plan job's summary; the job then applies exactly that plan. environment: production steps: - uses: actions/checkout@v5 - uses: astral-sh/setup-uv@v7 - uses: actions/download-artifact@v6 with: name: deploy-plan - name: Write the kubeconfig (mode 0600, never printed) env: KUBECONFIG_CONTENT: ${{ secrets.DEPLOY_KUBECONFIG }} run: | umask 077 printf '%s\n' "$KUBECONFIG_CONTENT" > "$RUNNER_TEMP/deploy.kubeconfig" echo "DEPLOY_KUBECONFIG=$RUNNER_TEMP/deploy.kubeconfig" >> "$GITHUB_ENV" # A fresh working copy of the shared state (Pipeline(state_dir=...)). echo "DEPLOY_STATE_DIR=$RUNNER_TEMP/piceli-state" >> "$GITHUB_ENV" - name: Apply the approved plan env: COMBINED_HASH: ${{ needs.plan.outputs.combined_hash }} run: | uv run --frozen piceli deploy --apply deploy-plan.json --approve "$COMBINED_HASH" --json > apply.jsonl - name: Post the run summary # The run's summary.md (release, commits, images, plan, checks, failure # cause, stage timings) becomes the job summary; summary.json, the same # data for agents (docs/schemas/piceli-run-summary-v1.schema.json), is # kept in the artifact. Neither ever contains a secret value. if: always() env: RESULT_FILE: apply.jsonl run: | summary=$(jq -r 'select(.event == "result") | .summary.markdown // empty' "$RESULT_FILE" 2>/dev/null | tail -n 1 || true) if [ -n "$summary" ] && [ -f "$summary" ]; then cat "$summary" >> "$GITHUB_STEP_SUMMARY" cp "$summary" run-summary.md cp "${summary%.md}.json" run-summary.json fi - uses: actions/upload-artifact@v6 if: always() with: name: deploy-result path: | apply.jsonl run-summary.md run-summary.json - name: Remove the kubeconfig and the state copy if: always() run: rm -rf "$RUNNER_TEMP/deploy.kubeconfig" "$RUNNER_TEMP/piceli-state" resume: if: ${{ inputs.resume }} runs-on: [self-hosted, deploy-my-app] environment: production steps: - uses: actions/checkout@v5 - uses: astral-sh/setup-uv@v7 - name: Write the kubeconfig (mode 0600, never printed) env: KUBECONFIG_CONTENT: ${{ secrets.DEPLOY_KUBECONFIG }} run: | umask 077 printf '%s\n' "$KUBECONFIG_CONTENT" > "$RUNNER_TEMP/deploy.kubeconfig" echo "DEPLOY_KUBECONFIG=$RUNNER_TEMP/deploy.kubeconfig" >> "$GITHUB_ENV" # A fresh working copy of the shared state (Pipeline(state_dir=...)). echo "DEPLOY_STATE_DIR=$RUNNER_TEMP/piceli-state" >> "$GITHUB_ENV" - name: Resume the interrupted run at its failed stage run: | uv run --frozen piceli deploy "$PIPELINE" --resume --json > resume.jsonl - name: Post the run summary # The run's summary.md (release, commits, images, plan, checks, failure # cause, stage timings) becomes the job summary; summary.json, the same # data for agents (docs/schemas/piceli-run-summary-v1.schema.json), is # kept in the artifact. Neither ever contains a secret value. if: always() env: RESULT_FILE: resume.jsonl run: | summary=$(jq -r 'select(.event == "result") | .summary.markdown // empty' "$RESULT_FILE" 2>/dev/null | tail -n 1 || true) if [ -n "$summary" ] && [ -f "$summary" ]; then cat "$summary" >> "$GITHUB_STEP_SUMMARY" cp "$summary" run-summary.md cp "${summary%.md}.json" run-summary.json fi - uses: actions/upload-artifact@v6 if: always() with: name: deploy-resume path: | resume.jsonl run-summary.md run-summary.json - name: Remove the kubeconfig and the state copy if: always() run: rm -rf "$RUNNER_TEMP/deploy.kubeconfig" "$RUNNER_TEMP/piceli-state"
Push to
main. Theplanjob’s summary shows the plan (inputs ref shop: <sha> = commit <sha>, the builds, the deliveries, the release’s changes and the checks) and the combined hash; thedeploy-planartifact holdsdeploy-plan.json(the portable plan file: hash, commits, stages, target identity, receipts),plan.json(the result object) andplan.txt.A reviewer reads the summary and approves the
productiondeployment of the run. Theapplyjob downloads the artifact, andpiceli deploy --apply deploy-plan.jsonchecks the file against the approved hash, plans again from the same commits against live state, checks the hash, and runs the stages. Its JSON lines are kept as thedeploy-resultartifact.Expected output: the
applyjob ends withdeploy ready: release <name>on stderr and a last JSON line with"state": "ready". Its job summary shows the run’s summary (below).
Run summaries in the job summary¶
Every deploy run writes summary.md and summary.json next to its journal
(see Run summaries), and the result line names them
(summary.markdown, summary.json). The apply and resume jobs’
Post the run summary step (if: always(), so a failed run is posted too)
appends summary.md to $GITHUB_STEP_SUMMARY: status, release, commits,
image digests and sizes (blobs uploaded and reused), the plan’s action
classes and changed objects with their changed fields, the checks, the
failure’s code and piceli explain command, and stage timings. It copies
both files into the job’s artifact (run-summary.md, run-summary.json):
an agent reviewing the deploy reads the JSON
(docs/schemas/piceli-run-summary-v1.schema.json), not the Markdown. A
summary never contains a secret value or a kubeconfig path.
To also comment on the pull request that was merged into main, add this
step after Post the run summary and grant the job
pull-requests: write (the sample keeps contents: read only):
- name: Comment the run summary on the merged pull request
if: always()
env:
GH_TOKEN: ${{ github.token }}
run: |
[ -f run-summary.md ] || exit 0
pr=$(gh pr list --state merged --search "$GITHUB_SHA" --json number --jq '.[0].number')
[ -z "$pr" ] || gh pr comment "$pr" --body-file run-summary.md
For a workflow that runs on pull_request instead, comment on
${{ github.event.pull_request.number }}.
Secrets and the kubeconfig¶
The kubeconfig is written with
umask 077(mode0600) to$RUNNER_TEMP, from an environment variable, never from a command line argument; nothing echoes it, and a finalif: always()step deletes it. The model gets only its path (DEPLOY_KUBECONFIG).Piceli never prints secret values, and its errors carry codes, not server messages, so the plan text is safe to publish in a job summary.
Never pass
--allow-execor an ambientKUBECONFIG; an exec credential plugin must be allowed in theTargetitself (see Connect to managed clusters (GKE, EKS, AKS)).Do not run this workflow for pull requests from forks: a self-hosted runner with cluster credentials must only run code from branches you trust.
Self-hosted runners and private clusters¶
Put the runner inside the network that reaches the cluster’s API server (or the node, for
NodeImportandNodeLoopbackRegistry); no inbound access to the cluster is needed from GitHub.plan,applyandresumemay run on different runners (ephemeral ones too). Each job gets a fresh working copy of the state in$RUNNER_TEMP(removed at the end of the job); the state itself is in the namespace. An image built and delivered by an earlier run is found by its delivery receipt and its digest in the registry, so the apply runner does not build it again; an image that was never delivered is built by the apply job, from the pinned commit.--ref "$GITHUB_SHA"makes the build independent of the runner’s workspace: even a runner that reuses a dirty workspace builds the exact commit, from a temporary worktree that is removed at the end (also when the job is cancelled:SIGTERMis handled likeCtrl-C). The checkout’sdeploy/*.pymust equal the commit (deploy-ref-model-differsotherwise), whichactions/checkoutguarantees.A shallow checkout (the default
fetch-depth: 1) is enough: the commit is$GITHUB_SHAitself.
Concurrency¶
concurrency: {group: deploy-my-app, cancel-in-progress: false} queues
deploys of one target instead of cancelling a running one. Piceli also holds
the release lock (a Lease in the namespace) for every command: a second
deployer of the same release, from any runner, workflow or laptop, is
refused with pipeline-locked (exit 2, with the holder and when its lease
expires) rather than racing the first. A cancelled job frees the lock; a
runner that died frees it when its lease expires (state_lease_seconds,
default 60). Use one group per target (cluster, namespace and app).
Resume¶
When apply fails at a stage (a registry was down, the release did not
become ready), fix the cause and run the workflow by hand with resume
checked (Actions → deploy → Run workflow). The resume job runs
piceli deploy "$PIPELINE" --resume --json: it continues the same run at its
failed stage with the approved plan and the commits the run recorded, not
the branch’s newer head, on whichever runner picks it up (the run journal is
in the shared state). It needs no new plan, but the environment approval
still applies. Re-running the failed apply job instead plans again and
usually stops with pipeline-plan-changed, because the first attempt already
built or delivered something.
If it fails¶
Output |
Meaning |
Next step |
|---|---|---|
|
Something changed between the plan and the approval |
Re-run the workflow (new plan, new approval) |
|
A stage ran but did not succeed |
Fix it, then run the workflow with resume |
|
Another deploy of the same release runs |
Wait; keep one concurrency group per target |
|
|
Use the artifact and output of the same workflow run |
|
The kubeconfig reaches another cluster than the plan did |
Fix the |
|
The kubeconfig may not write the shared state |
Grant the Secret and Lease verbs (see Share deployment state between runners) |
|
The runner’s clone does not have the commit |
Check the checkout step ( |
|
The checkout’s pipeline module is not the commit’s |
Check out |
|
The last run finished; nothing to resume |
Push or re-run the workflow for a new plan |
A build fails with a full disk |
The runner’s disk filled up |
Run |
Every code is explained by piceli explain <code> and in
Error codes.