Lab: publish a versioned package from CI
Run a local OCI registry with scoped accounts and a retention rule, publish a versioned build with ORAS under a version tag and a commit tag, verify it by digest from a separate pull, watch an overwrite get refused, then apply the same publish to GHCR from GitHub Actions.
Contentsand lesson path
On this page
- What you will build
- Steps 1 to 4: registry, package, publish
- Steps 5 to 7: pull, overwrite, retention
- The same publish from GitHub Actions to GHCR
- Check your understanding
- Apply it
- Review prompt
Lessons of Artifact Management
- Artifacts, versions and the build-once rule Concept · 15 min
- Mutable names, immutable identities: tags, checksums and digests Worked example · 15 min
- Lab: publish a versioned package from CI Lab · 25 min
- Registries: access control, retention, immutability and pull-through caches Concept · 20 min
- Promoting a release to production without rebuilding it Worked example · 20 min
- Check: trace a deployed artifact back to its commit Knowledge check · 6 questions
What you will learn
- Publish build outputs from CI to a package or container registry with scoped credentials, retention rules and immutable versions.
01. What you will build
A build is only useful to later stages if they can all fetch exactly the same bytes. In this lab you run a real registry on your machine and publish to it the way a CI job does: with an account that may push but not overwrite, a version tag plus a commit tag, a recorded digest, and a retention rule.
zot is an OCI registry: a server that speaks the OCI Distribution protocol, the HTTP API that Docker Hub, GitHub Container Registry (GHCR) and Amazon ECR also speak. ORAS is a client that pushes any file, not only container images, to such a registry as an OCI artifact. The registry stores an artifact as a manifest, a small JSON document that lists the files (blobs) by SHA-256 digest. The manifest has its own digest, and a tag such as 1.4.2 is just a name pointing at one manifest digest.
By the end you will have:
- a registry where
ci-publishermay read and push,deployermay only read, andrelease-adminmay do everything; - release 1.4.2 published as
1.4.2andsha-3f9c2e1, with its digest recorded; - a deploy step that pulls by that digest and checks the archive;
- proof that CI cannot move the
1.4.2tag; - a retention rule that keeps releases and the two newest commit builds.
You need Linux (or WSL) with Bash, curl, GNU tar 1.28 or later and htpasswd (package apache2-utils on Debian and Ubuntu, httpd-tools on Fedora). Nothing needs root; the registry listens on 127.0.0.1:5050. Tested with zot v2.1.23 and ORAS 1.3.6.
02. Steps 1 to 4: registry, package, publish
Step 1: download the tools. The commands create artifact-lab/, remember it as $LAB, and check each download against the checksum list its project publishes. Run every later block in the same terminal, because the blocks use $LAB and the variables set along the way.
mkdir -p artifact-lab/bin && cd artifact-lab && export LAB="$PWD"
ZOT=https://github.com/project-zot/zot/releases/download/v2.1.23
ORAS=https://github.com/oras-project/oras/releases/download/v1.3.6
curl -fsSLo bin/zot "$ZOT/zot-linux-amd64-minimal"
curl -fsSLo oras.tar.gz "$ORAS/oras_1.3.6_linux_amd64.tar.gz"
# Check both downloads against the checksum lists the projects publish
{ curl -fsSL "$ZOT/checksums.sha256.txt" | grep ' \*zot-linux-amd64-minimal$' \
| awk '{print $1 " bin/zot"}'
curl -fsSL "$ORAS/oras_1.3.6_checksums.txt" | grep ' oras_1.3.6_linux_amd64.tar.gz$' \
| awk '{print $1 " oras.tar.gz"}'; } | sha256sum --check
tar -xzf oras.tar.gz -C bin oras && rm oras.tar.gz && chmod +x bin/zot
export PATH="$LAB/bin:$PATH"
oras version | head -1
htpasswd -bnB probe probe | head -1 | cut -c1-10bin/zot: OK
oras.tar.gz: OK
Version: 1.3.6
probe:$2y$Step 2: create the accounts. htpasswd -B stores bcrypt hashes, the only format zot accepts in an htpasswd file. The passwords are lab values; in a real setup they live in a secret store, never in a script.
cd "$LAB" && mkdir -p registry
export CI_PASS=ci-lab-pass DEPLOY_PASS=deploy-lab-pass ADMIN_PASS=admin-lab-pass
htpasswd -bBc registry/htpasswd ci-publisher "$CI_PASS"
htpasswd -bB registry/htpasswd deployer "$DEPLOY_PASS"
htpasswd -bB registry/htpasswd release-admin "$ADMIN_PASS"Adding password for user ci-publisher
Adding password for user deployer
Adding password for user release-adminSave the configuration below as registry/config.json and read it first:
accessControlgrants actions per repository pattern. In zot,createlets an account push new tags andupdatelets it overwrite an existing tag.ci-publishergetsreadandcreateonly, so it can publish a new version but not replace one.adminPolicygivesrelease-adminevery action.retentionkeeps tags that look like a release version and the two most recently pushedsha-tags. Once akeepTagsrule exists, zot deletes every tag that matches no rule, so list everything you want to keep.deleteUntaggedremoves manifests no tag points at.- The 10-second
gcIntervallets you watch retention work; production runs it every few hours. Test a new rule with"dryRun": true, which only logs the decisions.
{
"distSpecVersion": "1.1.1",
"storage": {
"rootDirectory": "registry/data",
"gc": true,
"gcDelay": "1s",
"gcInterval": "10s",
"retention": {
"delay": "1s",
"policies": [
{
"repositories": ["orders/**"],
"deleteUntagged": true,
"keepTags": [
{ "patterns": ["^[0-9]+\\.[0-9]+\\.[0-9]+$"] },
{ "patterns": ["^sha-"], "mostRecentlyPushedCount": 2 }
]
}
]
}
},
"http": {
"address": "127.0.0.1",
"port": "5050",
"auth": { "htpasswd": { "path": "registry/htpasswd" } },
"accessControl": {
"repositories": {
"orders/**": {
"policies": [
{ "users": ["ci-publisher"], "actions": ["read", "create"] },
{ "users": ["deployer"], "actions": ["read"] }
]
}
},
"adminPolicy": {
"users": ["release-admin"],
"actions": ["read", "create", "update", "delete"]
}
}
},
"log": { "level": "info" }
}Start zot in the background and check that it asks for credentials. Your shell may also print a job number.
cd "$LAB"
zot verify registry/config.json 2>&1 | grep -o '"message":"[^"]*"' | tail -1
zot serve registry/config.json > registry/zot.log 2>&1 &
echo $! > registry/zot.pid
URL=http://127.0.0.1:5050/v2/
until curl -s -o /dev/null "$URL"; do sleep 1; done # wait until zot listens
curl -s -o /dev/null -w 'no credentials: %{http_code}\n' "$URL"
curl -s -o /dev/null -w 'deployer: %{http_code}\n' \
-u "deployer:$DEPLOY_PASS" "$URL""message":"config file is valid"
no credentials: 401
deployer: 200Step 3: package release 1.4.2. Create the source files and set the two values that CI provides. Then save scripts/package.sh and run it.
mkdir -p "$LAB/orders-lab/src" "$LAB/orders-lab/scripts" && cd "$LAB/orders-lab"
printf 'def handler():\n return "orders 1.4.2"\n' > src/app.py
printf '{"name": "orders-service", "version": "1.4.2"}\n' > src/manifest.json
# CI fills these from the checkout: GIT_SHA=$GITHUB_SHA and
# SOURCE_DATE_EPOCH=$(git log -1 --pretty=%ct). Fixed here so you get our digests.
export GIT_SHA=3f9c2e1d0b8a7c6e5f4a3b2c1d0e9f8a7b6c5d4e
export SOURCE_DATE_EPOCH=1793613600 # 2026-11-02 10:00 UTC#!/usr/bin/env bash
# Usage: scripts/package.sh <version> -> dist/orders-service-<version>.tar.gz
set -euo pipefail
VERSION="$1"
: "${SOURCE_DATE_EPOCH:?set it to the commit time}"
ARCHIVE="dist/orders-service-${VERSION}.tar.gz"
mkdir -p dist
tar --sort=name --format=gnu --owner=0 --group=0 --numeric-owner \
--mode=a=rX,u+w --mtime="@${SOURCE_DATE_EPOCH}" \
-cf - -C src . | gzip -n > "${ARCHIVE}"
sha256sum "${ARCHIVE}" | tee "${ARCHIVE}.sha256" | cut -d' ' -f1cd "$LAB/orders-lab" && bash scripts/package.sh 1.4.2cfce8eee9c245fbbc8d846847f24c5745fc6eee1ff79db18e051d7829cb88caeStep 4: publish as the CI account. publish.sh pushes the archive under two tags at once: the version that people use, and sha-<commit>, which ties it to the commit that built it. Annotations store the version, commit and build time in the manifest, and the script prints the manifest digest. oras login keeps the CI credentials in ci-auth.json.
#!/usr/bin/env bash
# Usage: scripts/publish.sh <version> <tag>[,<tag>...] prints the manifest digest
set -euo pipefail
VERSION="$1" TAGS="$2"
REPO="${REPO:-127.0.0.1:5050/orders/orders-service}"
AUTH="${AUTH_FILE:?path to the CI credentials file}"
CREATED="$(date -u -d "@${SOURCE_DATE_EPOCH}" +%Y-%m-%dT%H:%M:%SZ)"
cd dist
oras push ${PLAIN_HTTP:+--plain-http} --registry-config "${AUTH}" \
--artifact-type application/vnd.example.orders.release.v1 \
--annotation "org.opencontainers.image.version=${VERSION}" \
--annotation "org.opencontainers.image.revision=${GIT_SHA}" \
--annotation "org.opencontainers.image.created=${CREATED}" \
--format go-template='{{.digest}}{{println}}' \
"${REPO}:${TAGS}" \
"orders-service-${VERSION}.tar.gz:application/vnd.example.orders.tar+gzip"cd "$LAB/orders-lab"
export PLAIN_HTTP=1 AUTH_FILE="$LAB/orders-lab/ci-auth.json" # lab registry: no TLS
printf '%s' "$CI_PASS" | oras login --plain-http --registry-config "$AUTH_FILE" \
-u ci-publisher --password-stdin 127.0.0.1:5050
MANIFEST=$(bash scripts/publish.sh 1.4.2 "1.4.2,sha-${GIT_SHA:0:7}")
printf 'MANIFEST=%s\nARCHIVE_SHA256=%s\n' "$MANIFEST" \
"$(cut -d' ' -f1 dist/orders-service-1.4.2.tar.gz.sha256)" | tee release-1.4.2.env
oras repo tags --plain-http --registry-config "$AUTH_FILE" \
127.0.0.1:5050/orders/orders-serviceLogin Succeeded
MANIFEST=sha256:6d22c44ed97ac71851c080c0a2d15bad35cf85f26733a0275d4c5e80a7592eb1
ARCHIVE_SHA256=cfce8eee9c245fbbc8d846847f24c5745fc6eee1ff79db18e051d7829cb88cae
1.4.2
sha-3f9c2e1The two lines in release-1.4.2.env are the release record. CI keeps them somewhere the registry cannot change, such as the job summary or the release notes, and later steps check against this record rather than against whatever the registry returns.
03. Steps 5 to 7: pull, overwrite, retention
Step 5: pull as the deploy step. In a separate directory and with the read-only account, resolve the tag, pull the artifact by the recorded digest and check the archive. Then try to push with the same account.
mkdir -p "$LAB/deploy" && cd "$LAB/deploy"
source ../orders-lab/release-1.4.2.env # the record CI kept: MANIFEST, ARCHIVE_SHA256
REPO=127.0.0.1:5050/orders/orders-service
printf '%s' "$DEPLOY_PASS" | oras login --plain-http --registry-config auth.json \
-u deployer --password-stdin 127.0.0.1:5050 > /dev/null
oras resolve --plain-http --registry-config auth.json "$REPO:1.4.2"
oras pull --plain-http --registry-config auth.json -o pkg "$REPO@$MANIFEST" > /dev/null
echo "$ARCHIVE_SHA256 pkg/orders-service-1.4.2.tar.gz" | sha256sum --check
tar -xzOf pkg/orders-service-1.4.2.tar.gz ./app.py | tail -1
echo junk > junk.txt # now try to push with the read-only account
oras push --plain-http --registry-config auth.json "$REPO:1.4.2" junk.txt 2>&1 \
| tail -1 | cut -d: -f1-3sha256:6d22c44ed97ac71851c080c0a2d15bad35cf85f26733a0275d4c5e80a7592eb1
pkg/orders-service-1.4.2.tar.gz: OK
return "orders 1.4.2"
Error response from registry: denied: requested access to the resource is deniedoras resolve shows that 1.4.2 still points at the recorded digest. Pulling by digest (@sha256:...) means that even if someone moved the tag later, the deploy would fetch exactly the tested bytes. The push fails because deployer has only read, so a leaked deploy credential cannot plant an artifact.
Step 6: try to publish 1.4.2 again. First change the code but keep the version, as a rushed hotfix would. Then rebuild the original code, as a CI rerun would, and push again.
cd "$LAB/orders-lab"
# 1. new bytes, same version
printf 'def handler():\n return "orders 1.4.2 hotfix"\n' > src/app.py
bash scripts/package.sh 1.4.2 > /dev/null
bash scripts/publish.sh 1.4.2 1.4.2 2>&1 | tail -1 | cut -d: -f1-3
# 2. a CI rerun: identical bytes, same version
printf 'def handler():\n return "orders 1.4.2"\n' > src/app.py
bash scripts/package.sh 1.4.2 > /dev/null
bash scripts/publish.sh 1.4.2 1.4.2 2>&1 | tail -1 | cut -d: -f1-3
oras resolve --plain-http --registry-config "$AUTH_FILE" \
127.0.0.1:5050/orders/orders-service:1.4.2Error response from registry: denied: requested access to the resource is denied
Error response from registry: denied: requested access to the resource is denied
sha256:6d22c44ed97ac71851c080c0a2d15bad35cf85f26733a0275d4c5e80a7592eb1Both pushes are refused and the tag still points at the original digest. The reason: the CI account lacks zot's update action, so it cannot overwrite any existing tag, even with identical bytes, while release-admin still could. The OCI Distribution Specification does not require registries to refuse such a push; each registry decides. Amazon ECR has a repository setting (--image-tag-mutability IMMUTABLE) that rejects it with ImageTagAlreadyExistsException; GHCR, as of late 2026, has none (feature request), so there the publish job must check first. Either way, a rerun that finds its version published should stop, and a changed build ships as a new version.
Step 7: watch the retention rule. Push three builds of main, each tagged only with its commit, and wait for zot's background retention run.
cd "$LAB/orders-lab"
for n in 1 2 3; do # three builds of main, tagged only with their commit
printf 'def handler():\n return "orders main build %s"\n' "$n" > src/app.py
export GIT_SHA="${n}a${n}b${n}c${n}d0000000000000000000000000000000000"
export SOURCE_DATE_EPOCH=$((1793613600 + n * 3600))
bash scripts/package.sh "main-${GIT_SHA:0:7}" > /dev/null
bash scripts/publish.sh "main-${GIT_SHA:0:7}" "sha-${GIT_SHA:0:7}" > /dev/null
done
REPO=127.0.0.1:5050/orders/orders-service
tags() { oras repo tags --plain-http --registry-config "$AUTH_FILE" "$REPO"; }
for i in $(seq 24); do # retention runs in the background: wait up to 2 minutes
[ "$(tags | wc -l)" -le 3 ] && break
sleep 5
done
tags
echo "--- removed by retention ---"
grep '"decision":"delete"' "$LAB/registry/zot.log" | grep -o '"tag":"[^"]*"' | sort -u1.4.2
sha-2a2b2c2
sha-3a3b3c3
--- removed by retention ---
"tag":"sha-1a1b1c1"
"tag":"sha-3f9c2e1"The release tag survives through the version pattern, and only the two newest sha- tags stay. sha-3f9c2e1 went although it belongs to the release: retention removes tags, and the 1.4.2 manifest stays because 1.4.2 still points at it. A rule based only on age or count would have deleted the release too. To protect a deployed build that is not a release, give it a tag a keep rule matches, as the registries lesson explains. Stop the registry when you are done.
kill "$(cat "$LAB/registry/zot.pid")" && echo "registry stopped"registry stoppedSteps 5 and 6 check against your own build record, not against the registry. A .sha256 file stored next to an archive catches a broken download, but whoever can replace the archive on a compromised server can replace that file too; proving who built an artifact needs a signature.
04. The same publish from GitHub Actions to GHCR
The workflow below runs the same two scripts on GitHub's runners and publishes to GHCR. It is a configuration to apply in your own repository, with scripts/ and src/ at its root; it was not run for this lesson, so no output is shown.
Three details carry the security. permissions: {} removes every default permission, and the job asks only for contents: read to check out and packages: write to push. The password is the job's GITHUB_TOKEN, which GitHub issues per job, limits to this repository and expires when the job ends. Each action is pinned to a full commit SHA, with its version as a comment, because a tag such as v7 can be moved to other code.
A Git tag v1.4.2 publishes 1.4.2 and sha-<commit>; a push to main publishes only sha-<commit>. Because GHCR accepts a push to an existing tag, the script first checks the tag the run adds (NEW): the version on a release, sha-<commit> on main, so a rerun of a finished job also stops. A release of a commit that main already published moves sha-<commit> to the release manifest, built from the same source. The script goes on only when oras resolve reports not found; any other error, such as a rejected token or an outage, stops the job, because a check that could not run must not count as "the tag is free". Two racing runs could both pass that check, so restrict who may push release tags. SOURCE_DATE_EPOCH is set on its own line: inside export X="$(…)", a failing git log would not stop the script there. persist-credentials: false keeps the Git token out of .git/config, where later steps could read it. GHCR names must be lowercase, hence ${GITHUB_REPOSITORY_OWNER,,}. The digest goes to the job summary as the release record. For retention, a scheduled job can delete old sha- versions through the package versions REST API.
name: publish
on:
push:
branches: [main]
tags: ['v*.*.*']
permissions: {} # nothing by default; the job asks for what it needs
jobs:
publish:
runs-on: ubuntu-24.04
permissions:
contents: read # check out the code
packages: write # push packages owned by this repository
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false # later steps get no Git token on disk
- uses: oras-project/setup-oras@ac735097e22fe643015b0d19645f7c9a69942128 # v2.0.4
with:
version: 1.3.6
- name: Package and publish
env:
REGISTRY_TOKEN: ${{ secrets.GITHUB_TOKEN }}
AUTH_FILE: ${{ runner.temp }}/oras-auth.json
run: |
GIT_SHA="$GITHUB_SHA"
SOURCE_DATE_EPOCH="$(git log -1 --pretty=%ct)" # alone, so a failure stops
REPO="ghcr.io/${GITHUB_REPOSITORY_OWNER,,}/orders-service"
export GIT_SHA SOURCE_DATE_EPOCH REPO
SHORT="${GITHUB_SHA:0:7}"
# NEW: the tag this run adds, which must not exist yet
if [[ "$GITHUB_REF" == refs/tags/v* ]]; then
VERSION="${GITHUB_REF_NAME#v}"; TAGS="$VERSION,sha-$SHORT"; NEW="$VERSION"
else
VERSION="main-$SHORT"; TAGS="sha-$SHORT"; NEW="sha-$SHORT"
fi
echo "$REGISTRY_TOKEN" | oras login ghcr.io --registry-config "$AUTH_FILE" \
-u "$GITHUB_ACTOR" --password-stdin
# Go on only if the registry says "not found"; stop on any other error
if CHECK="$(oras resolve --registry-config "$AUTH_FILE" \
"$REPO:$NEW" 2>&1)"; then
echo "::error::$REPO:$NEW already exists; it is not published again"
exit 1
elif [[ "$CHECK" != *": not found" ]]; then
echo "::error::cannot check $REPO:$NEW: $CHECK"
exit 1
fi
bash scripts/package.sh "$VERSION"
DIGEST="$(bash scripts/publish.sh "$VERSION" "$TAGS")"
echo "Published \`$REPO@$DIGEST\` as \`$TAGS\`" >> "$GITHUB_STEP_SUMMARY"05. Check your understanding
06. Apply it
Take the lab to your own project:
- Apply the workflow, push a
v0.1.0tag, and find the digest in the job summary. If it stops withdenied, see the box under the workflow. - Pull that version by digest elsewhere with a read-only token and check the archive.
- Write down what stops a version being overwritten in your registry: account rights, an immutable-tag setting or a workflow check.
- Draft a retention rule that keeps releases and what production runs, and try it in dry-run mode.
07. Review prompt
Without looking back, explain why each release gets a version tag and a commit tag, and why the deploy pulls by digest. Name the zot action the CI account lacked and what that did and did not protect. Then say what retention removed in step 7 and why the release manifest survived.
References
- zot documentation: User authentication and authorization
- zot documentation: Retention policies
- ORAS documentation: Pushing and pulling
- GitHub Docs: Publishing and installing a package with GitHub Actions
- Amazon ECR: Preventing image tags from being overwritten
- Reproducible Builds: Archive metadata
- GNU tar manual page
- Semantic Versioning 2.0.0
Your progress
Reading state
Sign in to save that you finished reading this lesson.
Prove this skill
6 questions · about 5 minutes · scored automatically. Marking a lesson as read never demonstrates a skill.