Skip to content

Commit e878b2b

Browse files
Kasturi NarraKasturi Narra
authored andcommitted
Separate OKD build and push phases
1 parent dfa6c32 commit e878b2b

File tree

2 files changed

+177
-17
lines changed

2 files changed

+177
-17
lines changed

.github/actions/build-okd/action.yaml

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,23 +51,30 @@ runs:
5151
set -euo pipefail
5252
5353
cd ${GITHUB_WORKSPACE}/
54+
# The 'staging' mode builds images locally AND pushes them to staging registry
55+
# Staging registry is automatically derived as: $(dirname target-registry)/okd-staging
56+
# This allows testing before promoting to production
5457
TARGET_REGISTRY="${{ inputs.target-registry }}" ./src/okd/build_images.sh \
58+
staging \
5559
"${{ inputs.okd-version-tag }}" \
5660
"${{ inputs.ushift-gitref }}" \
5761
"${{ inputs.target-arch }}"
5862
59-
- name: Build MicroShift RPMs
63+
- name: Build MicroShift RPMs using staging OKD images
6064
shell: bash
6165
run: |
6266
# See https://github.com/microshift-io/microshift/blob/main/docs/build.md
6367
# for more information about the build process.
6468
65-
# Run the RPM build process.
69+
# Run the RPM build process using images from staging registry
70+
# Staging registry is derived as: $(dirname target-registry)/okd-staging
6671
cd ${GITHUB_WORKSPACE}/
72+
PRODUCTION_REGISTRY="${{ inputs.target-registry }}"
73+
STAGING_REGISTRY="$(dirname "${PRODUCTION_REGISTRY}")/okd-staging"
6774
make rpm \
6875
USHIFT_GITREF="${{ inputs.ushift-gitref }}" \
6976
OKD_VERSION_TAG="${{ inputs.okd-version-tag }}" \
70-
OKD_RELEASE_IMAGE="${{ inputs.target-registry }}/okd-release-${{ steps.detect-cpu-arch.outputs.go_arch }}" \
77+
OKD_RELEASE_IMAGE="${STAGING_REGISTRY}/okd-release-${{ steps.detect-cpu-arch.outputs.go_arch }}:${{ inputs.okd-version-tag }}" \
7178
RPM_OUTDIR=/mnt/rpms
7279
7380
- name: Build MicroShift bootc container image
@@ -97,6 +104,37 @@ runs:
97104
make run-healthy
98105
make stop
99106
107+
- name: Push OKD images to production registry
108+
if: success()
109+
shell: bash
110+
run: |
111+
set -euo pipefail
112+
113+
cd ${GITHUB_WORKSPACE}/
114+
# Only push to production if all tests passed
115+
# This ensures we don't publish broken OKD images to production
116+
TARGET_REGISTRY="${{ inputs.target-registry }}" ./src/okd/build_images.sh \
117+
production \
118+
"${{ inputs.okd-version-tag }}" \
119+
"${{ inputs.ushift-gitref }}" \
120+
"${{ inputs.target-arch }}"
121+
122+
- name: Cleanup staging registry
123+
if: always()
124+
shell: bash
125+
continue-on-error: true
126+
run: |
127+
set -euo pipefail
128+
129+
cd ${GITHUB_WORKSPACE}/
130+
# Cleanup staging registry using the script's cleanup mode
131+
# This runs on both success and failure to keep the staging registry clean
132+
TARGET_REGISTRY="${{ inputs.target-registry }}" ./src/okd/build_images.sh \
133+
cleanup \
134+
"${{ inputs.okd-version-tag }}" \
135+
"${{ inputs.ushift-gitref }}" \
136+
"${{ inputs.target-arch }}"
137+
100138
# Uncomment this to enable tmate-debug on failure
101139
# - name: Pause and open tmate debug session
102140
# if: failure()

src/okd/build_images.sh

Lines changed: 136 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,25 @@ set -euo pipefail
44
export LC_ALL=C.UTF-8
55
export LANG=C.UTF-8
66

7-
TARGET_REGISTRY=${TARGET_REGISTRY:-ghcr.io/microshift-io/okd}
7+
# Production registry - must be provided via TARGET_REGISTRY environment variable
8+
# or defaults to the upstream registry if not specified
9+
PRODUCTION_REGISTRY="${TARGET_REGISTRY:-ghcr.io/microshift-io/okd}"
10+
# Automatically derive staging registry by appending '/okd-staging' subpath
11+
STAGING_REGISTRY="$(dirname "${PRODUCTION_REGISTRY}")/okd-staging"
812
PULL_SECRET=${PULL_SECRET:-~/.pull-secret.json}
913

1014
WORKDIR=$(mktemp -d /tmp/okd-build-images-XXXXXX)
1115
trap 'cd ; rm -rf "${WORKDIR}"' EXIT
1216

1317
usage() {
14-
echo "Usage: $(basename "$0") <okd-version> <ocp-branch> <target-arch>"
18+
echo "Usage: $(basename "$0") <mode> <okd-version> <ocp-branch> <target-arch>"
19+
echo " mode: Operation mode - 'staging', 'production', or 'cleanup'"
20+
echo " 'staging' - Build OKD images locally and push to staging registry"
21+
echo " (${STAGING_REGISTRY})"
22+
echo " 'production' - Push previously built images to production registry"
23+
echo " (${PRODUCTION_REGISTRY})"
24+
echo " 'cleanup' - Delete images from staging registry"
25+
echo " (${STAGING_REGISTRY})"
1526
echo " okd-version: The version of OKD to build (see https://amd64.origin.releases.ci.openshift.org/)"
1627
echo " ocp-branch: The branch of OCP to build (e.g. release-4.19)"
1728
echo " target-arch: The architecture of the target images (amd64 or arm64)"
@@ -320,17 +331,118 @@ create_new_okd_release() {
320331
# "ovn-kubernetes-microshift=${images_sha[ovn-kubernetes-microshift]}" \
321332
}
322333

334+
# Build OKD images locally and populate images_sha array
335+
build_okd_images() {
336+
echo "Building OKD images locally..."
337+
create_images
338+
339+
for key in "${!images[@]}" ; do
340+
# Skip haproxy-router for non-ARM64 architectures (see TODO at line 99)
341+
# haproxy28 package implementation for amd64 is not yet available
342+
if [ "${TARGET_ARCH}" != "arm64" ] && [ "${key}" = "haproxy-router" ] ; then
343+
continue
344+
fi
345+
images_sha["${key}"]="${images[$key]}"
346+
done
347+
348+
echo "Build completed successfully"
349+
}
350+
351+
# Push images and manifests to registry, then create OKD release
352+
push_okd_images() {
353+
echo "Pushing images to registry: ${TARGET_REGISTRY}"
354+
push_image_manifests
355+
create_new_okd_release
356+
echo "Push completed successfully"
357+
echo "OKD release image published to: ${OKD_RELEASE_IMAGE}"
358+
}
359+
360+
# Retag staging images to production names
361+
retag_staging_to_production() {
362+
echo "Re-tagging staging images to production names..."
363+
364+
for key in "${!images[@]}" ; do
365+
# Skip haproxy-router for non-ARM64 architectures (see TODO at line 99)
366+
# haproxy28 package implementation for amd64 is not yet available
367+
if [ "${TARGET_ARCH}" != "arm64" ] && [ "${key}" = "haproxy-router" ] ; then
368+
continue
369+
fi
370+
371+
staging_image="${images[$key]/${PRODUCTION_REGISTRY}/${STAGING_REGISTRY}}"
372+
production_image="${images[$key]}"
373+
374+
if ! podman image exists "${staging_image}" ; then
375+
echo "ERROR: Local staging image ${staging_image} not found."
376+
echo "Run staging build first: $0 staging ${OKD_VERSION} ${OCP_BRANCH} ${TARGET_ARCH}"
377+
exit 1
378+
fi
379+
380+
echo "Re-tagging ${staging_image} to ${production_image}"
381+
podman tag "${staging_image}" "${production_image}"
382+
images_sha["${key}"]="${production_image}"
383+
done
384+
}
385+
386+
# Staging mode: build images locally and push to staging registry
387+
push_staging() {
388+
check_podman_login
389+
check_release_image_exists
390+
build_okd_images
391+
push_okd_images
392+
echo ""
393+
echo "Images built and pushed to staging registry: ${STAGING_REGISTRY}"
394+
echo "OKD release image available at: ${OKD_RELEASE_IMAGE}"
395+
echo "After successful testing, push to production with:"
396+
echo " $0 production ${OKD_VERSION} ${OCP_BRANCH} ${TARGET_ARCH}"
397+
}
398+
399+
# Production mode: retag staging images and push to production registry
400+
push_production() {
401+
check_podman_login
402+
check_release_image_exists
403+
retag_staging_to_production
404+
push_okd_images
405+
}
406+
407+
# Cleanup mode: delete images from staging registry
408+
cleanup_staging() {
409+
check_podman_login
410+
echo "Cleaning up staging registry images for version ${OKD_VERSION}..."
411+
412+
for key in "${!images[@]}" ; do
413+
# Skip haproxy-router for non-ARM64 architectures (see TODO at line 99)
414+
if [ "${TARGET_ARCH}" != "arm64" ] && [ "${key}" = "haproxy-router" ] ; then
415+
continue
416+
fi
417+
418+
echo " Deleting: ${images[$key]}"
419+
skopeo delete "docker://${images[$key]}" || true
420+
done
421+
422+
# Delete the OKD release image
423+
echo " Deleting: ${OKD_RELEASE_IMAGE}"
424+
skopeo delete "docker://${OKD_RELEASE_IMAGE}" || true
425+
426+
echo "Staging registry cleanup completed"
427+
}
428+
323429
#
324430
# Main
325431
#
326-
if [[ $# -ne 3 ]]; then
432+
if [[ $# -ne 4 ]]; then
327433
usage
328434
fi
329435

330-
OKD_VERSION="$1"
331-
OCP_BRANCH="$2"
332-
TARGET_ARCH="$3"
333-
OKD_RELEASE_IMAGE="${TARGET_REGISTRY}/okd-release-${TARGET_ARCH}:${OKD_VERSION}"
436+
MODE="$1"
437+
OKD_VERSION="$2"
438+
OCP_BRANCH="$3"
439+
TARGET_ARCH="$4"
440+
441+
# Validate mode
442+
if [[ "${MODE}" != "staging" ]] && [[ "${MODE}" != "production" ]] && [[ "${MODE}" != "cleanup" ]]; then
443+
echo "ERROR: Invalid mode '${MODE}'. Must be 'staging', 'production', or 'cleanup'"
444+
usage
445+
fi
334446

335447
# Determine the alternate architecture
336448
case "${TARGET_ARCH}" in
@@ -346,6 +458,15 @@ case "${TARGET_ARCH}" in
346458
;;
347459
esac
348460

461+
# Set target registry based on mode
462+
if [[ "${MODE}" == "staging" ]] || [[ "${MODE}" == "cleanup" ]]; then
463+
TARGET_REGISTRY="${STAGING_REGISTRY}"
464+
elif [[ "${MODE}" == "production" ]]; then
465+
TARGET_REGISTRY="${PRODUCTION_REGISTRY}"
466+
fi
467+
468+
OKD_RELEASE_IMAGE="${TARGET_REGISTRY}/okd-release-${TARGET_ARCH}:${OKD_VERSION}"
469+
349470
# Populate associative arrays with image names and tags
350471
declare -A images
351472
declare -A images_sha
@@ -368,10 +489,11 @@ images=(
368489

369490
# Check the prerequisites
370491
check_prereqs
371-
check_podman_login
372-
check_release_image_exists
373-
# Create and push images
374-
create_images
375-
push_image_manifests
376-
# Create a new OKD release
377-
create_new_okd_release
492+
# Execute based on mode
493+
if [[ "${MODE}" == "staging" ]]; then
494+
push_staging
495+
elif [[ "${MODE}" == "production" ]]; then
496+
push_production
497+
elif [[ "${MODE}" == "cleanup" ]]; then
498+
cleanup_staging
499+
fi

0 commit comments

Comments
 (0)