Docker Build Tag Push Healthcheck
Script a full container release flow: build with cache pins, tag by digest and git SHA, push, and wait for registry health.
Code
#!/usr/bin/env bash
set -euo pipefail
readonly REGISTRY="registry.example.com"
readonly IMAGE="${REGISTRY}/billing-api"
readonly GIT_SHA="$(git rev-parse --short=12 HEAD)"
die() { echo "Error: $*" >&2; exit 1; }
command -v docker >/dev/null || die "docker 未安装"
git diff --quiet || die "工作区不干净,先提交再发布"
build() {
echo "构建 ${IMAGE}:${GIT_SHA}"
docker build \
--build-arg GIT_COMMIT="${GIT_SHA}" \
--label "org.opencontainers.image.revision=${GIT_SHA}" \
--label "org.opencontainers.image.created=$(date -u +%Y-%m-%dT%H:%M:%SZ)" \
-t "${IMAGE}:${GIT_SHA}" \
-t "${IMAGE}:latest" \
.
}
push_and_verify() {
docker push "${IMAGE}:${GIT_SHA}"
docker push "${IMAGE}:latest"
local digest
digest=$(docker inspect --format '{{index .RepoDigests 0}}' \
"${IMAGE}:${GIT_SHA}" | cut -d@ -f2)
echo "已推送: ${IMAGE}@${digest}"
# registry 健康探测:清单可拉取才算发布完成
local attempt=0
until docker manifest inspect "${IMAGE}:${GIT_SHA}" >/dev/null 2>&1; do
(( attempt++ )) || true
(( attempt < 5 )) || die "manifest 未就绪,registry 可能异常"
sleep 3
done
echo "发布完成:${GIT_SHA}"
}
build
push_and_verify
Inside the snippet
set -euo pipefail removes the shell’s usual optimism — errors abort, empty variables abort, hidden pipeline failures abort.
The work is delegated to readonly, command, git, docker and sleep, chained through 9 pipes so each stage’s output feeds the next.
3 helper functions (die, build and push_and_verify) keep the repeated logic out of the main flow.
Caveats
- The pipeline is 9 stages deep — when it misbehaves, split the pipe and test the stages separately.
Key statements worth quoting
readonly GIT_SHA="$(git rev-parse --short=12 HEAD)"
command -v docker >/dev/null || die "docker 未安装"
git diff --quiet || die "工作区不干净,先提交再发布"
Interactive Tools for This Topic
Docker Run to Compose ConverterTransform docker run CLI commands with ports, environment variables, volumes, networks, and resource limits into clean compose.yaml service definitions.Git Conventional Commit BuilderBuild and validate Conventional Commits v1.0.0 messages with type, scope, breaking markers, 72-char subject counter, body, footers, and git CLI command.Glob Pattern TesterCompile glob patterns with *, **, ?, character classes, and braces into regex and test them against a list of candidate file paths.SemVer SorterParse and sort Semantic Version 2.0.0 strings with prerelease and build metadata, respecting prerelease-below-release precedence.
FAQ
- Does the script stop on the first error?
- Yes — `set -euo pipefail` aborts on the first failing command, treats unset variables as errors, and fails the whole pipeline if any stage fails. Nothing continues half-broken.
- Which external commands does the script rely on?
- `readonly`, `command`, `git`, `docker`, `sleep` must be available on the PATH environment variable; everything else is shell built-in syntax.
Report a problem with this exampleOpens a pre-filled issue on GitHub.