Staging est vert. Vous promouvez en production. La production échoue trois minutes après le déploiement parce que la build de production a tiré une nouvelle release patch d’une dépendance transitive qui n’y était pas quand staging a buildé cinq jours plus tôt. La build de staging et celle de production n’ont jamais été le même artefact — elles partagent un commit, mais ce sont deux builds différentes. « Ça marchait sur staging » était une affirmation statistiquement vide. Le correctif : builder une seule fois, tagger l’artefact avec un hash de contenu, et promouvoir cet artefact-là exactement.
Ce qui se passe
Deux façons dont les environnements PaaS traitent la promotion staging vers production. Elles ont des modes d’échec très différents :
- Rebuild depuis le source (anti-pattern). Staging build depuis
git ref X. La production build aussi depuisgit ref X, sur un runner différent, possiblement plusieurs heures ou jours plus tard. Même avec un lockfile, l’image de base OS, les paquets système et toute dépendance transitive non épinglée peuvent dériver. L’artefact que vous avez testé n’est pas l’artefact que vous avez livré. - Promotion d’image immuable (correct). Staging build une fois. La build produit un artefact (image conteneur, jar JVM avec checksums, wheel Python + lockfile, tarball agnostique du langage) taggé avec un hash addressé par contenu (
sha256:abc123...). La production déploie ce même hash. La promotion est un changement de pointeur dans le hook de déploiement du PaaS, pas un rebuild. Il n’y a pas de fenêtre de dérive parce qu’il n’y a pas de deuxième build.
La raison pour laquelle les équipes par défaut rebuildent depuis le source, c’est que les PaaS infogérés font du rebuild le défaut — le workflow « déployer depuis git » est le chemin heureux marketé. Le correctif : utiliser le mode déploiement-par-image de la plateforme (la plupart des PaaS infogérés le supportent) et traiter le registry comme la source de vérité de ce qui est promouvable.
Comment vérifier
Comparez le hash de l’artefact sur staging et sur production. S’ils diffèrent, vous ne promouvez pas — vous buildez deux fois :
# Get the artifact hash currently serving on staging
curl -sS -H "Authorization: Bearer ${PAAS_TOKEN}" \
"https://app.paas.example.com/1.0/environment/control/rest/getenvinfo?envName=app-staging" \
| jq -r '.deployment.imageDigest // .deployment.artifactHash'
# Same on production
curl -sS -H "Authorization: Bearer ${PAAS_TOKEN}" \
"https://app.paas.example.com/1.0/environment/control/rest/getenvinfo?envName=app-prod" \
| jq -r '.deployment.imageDigest // .deployment.artifactHash'
# Both should be the SAME hash. If staging is sha256:abc... and prod is sha256:def...,
# you're rebuilding, not promoting.
Confirmez que le registry contient l’artefact et que personne n’a réécrit le tag :
# Pull the manifest by digest — this works because digests are content-addressable
# A tag like ":v1.42" can be overwritten; a digest cannot.
docker manifest inspect "registry.example.com/app@sha256:abc123def456..."
# Or for a non-container artifact, verify by hash
sha256sum app-v1.42.tar.gz
# Compare against the hash recorded in the deploy log
Le correctif
Builder une fois. Tagger par hash de contenu. Promouvoir le hash.
Étape 1 — CI build l’artefact, calcule le digest et garde les deux.
# .github/workflows/build-once.yaml
name: build-and-tag
on:
push:
branches: [main]
tags: ['v*']
jobs:
build:
runs-on: ubuntu-latest
outputs:
image_digest: ${{ steps.push.outputs.digest }}
image_ref: ${{ steps.push.outputs.image_ref }}
steps:
- uses: actions/checkout@v4
- name: Build container image
run: |
# Lockfile-driven build. No `:latest`. No floating tags inside the Dockerfile.
docker build \
--pull \
--tag "registry.example.com/app:${GITHUB_SHA}" \
--label "org.opencontainers.image.revision=${GITHUB_SHA}" \
--label "org.opencontainers.image.created=$(date -u +%Y-%m-%dT%H:%M:%SZ)" \
.
- name: Push and capture digest
id: push
run: |
docker push "registry.example.com/app:${GITHUB_SHA}"
DIGEST=$(docker inspect --format='{{index .RepoDigests 0}}' \
"registry.example.com/app:${GITHUB_SHA}" | awk -F'@' '{print $2}')
echo "digest=${DIGEST}" >> $GITHUB_OUTPUT
echo "image_ref=registry.example.com/app@${DIGEST}" >> $GITHUB_OUTPUT
echo "Built artifact: registry.example.com/app@${DIGEST}"
- name: Record build manifest
run: |
# Persist the (commit -> digest) map somewhere the promotion job can read it
cat > build-manifest.json <<EOF
{
"commit": "${GITHUB_SHA}",
"digest": "${{ steps.push.outputs.digest }}",
"image_ref": "${{ steps.push.outputs.image_ref }}",
"built_at": "$(date -u +%Y-%m-%dT%H:%M:%SZ)"
}
EOF
- uses: actions/upload-artifact@v4
with:
name: build-manifest
path: build-manifest.json
Étape 2 — Le déploiement staging consomme le digest, pas un tag, pas une branche.
# .github/workflows/deploy-staging.yaml
name: deploy-staging
on:
workflow_run:
workflows: [build-and-tag]
types: [completed]
jobs:
staging:
if: ${{ github.event.workflow_run.conclusion == 'success' }}
runs-on: ubuntu-latest
steps:
- uses: actions/download-artifact@v4
with:
name: build-manifest
run-id: ${{ github.event.workflow_run.id }}
- name: Deploy by digest to staging
run: |
DIGEST=$(jq -r '.digest' build-manifest.json)
IMAGE_REF="registry.example.com/app@${DIGEST}"
# PaaS deploy hook — points the env at this exact digest, no rebuild
curl -fsS -X POST \
-H "Authorization: Bearer ${{ secrets.PAAS_TOKEN }}" \
-d "envName=app-staging&imageRef=${IMAGE_REF}" \
https://app.paas.example.com/1.0/environment/control/rest/deployimage
echo "Staging now serving ${IMAGE_REF}"
Étape 3 — La promotion en production est une recopie de hash, jamais un rebuild.
# .github/workflows/promote-to-prod.yaml
name: promote-to-prod
on:
workflow_dispatch:
inputs:
staging_digest:
description: "Digest currently running on staging (sha256:...)"
required: true
type: string
jobs:
promote:
runs-on: ubuntu-latest
environment: production # requires manual approval per the env policy
steps:
- name: Verify staging is actually running this digest
run: |
STAGING_NOW=$(curl -fsS -H "Authorization: Bearer ${{ secrets.PAAS_TOKEN }}" \
"https://app.paas.example.com/1.0/environment/control/rest/getenvinfo?envName=app-staging" \
| jq -r '.deployment.imageDigest')
if [ "${STAGING_NOW}" != "${{ inputs.staging_digest }}" ]; then
echo "Staging is on ${STAGING_NOW}, not the digest you're promoting (${{ inputs.staging_digest }})."
echo "Refusing to promote a digest that wasn't actually validated on staging."
exit 1
fi
- name: Verify the artifact exists in the registry (defense against tag rewrite)
run: |
docker manifest inspect "registry.example.com/app@${{ inputs.staging_digest }}" > /dev/null
- name: Promote — point production at the same digest
run: |
IMAGE_REF="registry.example.com/app@${{ inputs.staging_digest }}"
curl -fsS -X POST \
-H "Authorization: Bearer ${{ secrets.PAAS_TOKEN }}" \
-d "envName=app-prod&imageRef=${IMAGE_REF}" \
https://app.paas.example.com/1.0/environment/control/rest/deployimage
- name: Verify production is now on the promoted digest
run: |
PROD_NOW=$(curl -fsS -H "Authorization: Bearer ${{ secrets.PAAS_TOKEN }}" \
"https://app.paas.example.com/1.0/environment/control/rest/getenvinfo?envName=app-prod" \
| jq -r '.deployment.imageDigest')
if [ "${PROD_NOW}" != "${{ inputs.staging_digest }}" ]; then
echo "Promotion did not take. Production is on ${PROD_NOW}."
exit 2
fi
echo "Production now serving ${{ inputs.staging_digest }} — identical bytes to staging."
Étape 4 — La configuration est la seule chose qui doit différer entre les environnements. Si vous gardez la config et les secrets hors de l’image, la même image tourne en staging et en production. Utilisez de la config liée à l’environnement dans le PaaS, pas de la config gravée dans l’image :
# env-config.yaml — promoted SEPARATELY from the image
environments:
staging:
DATABASE_URL: "postgres://staging-pg.internal:5432/app"
LOG_LEVEL: "debug"
FEATURE_FLAGS: "experimental=true"
prod:
DATABASE_URL: "postgres://prod-pg.internal:5432/app"
LOG_LEVEL: "info"
FEATURE_FLAGS: "experimental=false"
L’image reste identique. Les différences propres à la production vivent dans des variables d’environnement et des secrets, pas dans l’artefact.
Étape 5 — Health-checkez la promotion, pas seulement le déploiement. Juste après que le digest a basculé en production, confirmez que le conteneur qui tourne signale le même image_revision :
# Each pod exposes a /version endpoint that echoes the build-time labels
curl -sS https://app.example.com/version | jq .
# {
# "git_sha": "abc123...",
# "image_digest": "sha256:abc123def456...",
# "built_at": "2026-05-14T10:23:11Z"
# }
# In a deploy hook, assert it matches what you promoted:
RUNNING=$(curl -sS https://app.example.com/version | jq -r '.image_digest')
[ "${RUNNING}" = "${PROMOTED_DIGEST}" ] || exit 2
Si votre PaaS ne supporte pas le déploiement épinglé par digest, la deuxième meilleure option est un tag dont vous garantissez qu’il ne sera jamais réécrit (v1.42.7, jamais latest, jamais prod) plus une politique d’immutabilité côté registry qui refuse d’écraser ce tag. Le digest reste meilleur — un tag peut être bougé par quiconque a le droit push ; un digest ne peut pas mentir sur son contenu.
Pour les pipelines de build, l’hygiène de registry et les hooks de déploiement PaaS qui refusent de promouvoir un tag plutôt qu’un digest, le plan deployment management de Stack Harbor câble ça sur du PaaS infogéré.