Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
184 changes: 184 additions & 0 deletions .github/actions/docker-build/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
# Copyright Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

name: Docker Build (Hermetic)
description: Build must-gather image hermetically using Hermeto (offline/reproducible build)
inputs:
imageName:
description: The full image name including registry (e.g., quay.io/rhdh-community/rhdh-must-gather)
required: true
imageTags:
description: The tags to apply to the image
required: true
imageLabels:
description: The labels for the Docker image
required: false
platform:
description: "Target given CPU platform architecture (default: linux/amd64)"
required: false
default: linux/amd64
containerfilePath:
description: Path to the Konflux/hermetic Containerfile
required: false
default: '.rhdh/docker/Containerfile'
Comment thread
Fortune-Ndlovu marked this conversation as resolved.
Resolved
buildArgs:
description: Build arguments to pass to buildah (newline-separated KEY=VALUE pairs)
required: false
default: ''
skipArtifactUpload:
description: Skip uploading the built image as a GitHub artifact
required: false
default: 'false'

runs:
using: composite
steps:
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@dc802804100637a589fabce1cb79ff13a1411302 # v6.2.0
with:
images: ${{ inputs.imageName }}
tags: |
${{ inputs.imageTags }}
labels: |
${{ inputs.imageLabels }}

- name: Ensure podman is available
shell: bash
run: |
if ! command -v podman &>/dev/null; then
echo "podman not found, installing..."
sudo apt-get -y update && sudo apt-get -y install podman
fi
podman --version

- name: Set up hermetic build variables
id: hermeto-vars
shell: bash
run: |
# Same pin as local hermetic builds: HERMETO_IMAGE in the Makefile.
HERMETO_IMAGE=$(sed -n 's/^HERMETO_IMAGE ?= //p' Makefile | head -1)
if [[ -z "${HERMETO_IMAGE}" ]]; then
echo "HERMETO_IMAGE is not set in the Makefile"
exit 1
fi
echo "Using ${HERMETO_IMAGE}"
echo "HERMETO_IMAGE=${HERMETO_IMAGE}" >> "$GITHUB_ENV"
echo "LOCAL_CACHE_DIR=/tmp/hermeto-cache/rhdh-must-gather" >> "$GITHUB_ENV"
# Invalidate the dependency cache when the Hermeto image tag changes.
echo "hermeto_key=${HERMETO_IMAGE##*:}" >> "$GITHUB_OUTPUT"

- name: Restore hermeto dependency cache
id: cache-deps
uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
with:
path: /tmp/hermeto-cache/rhdh-must-gather
key: hermeto-deps-${{ inputs.platform }}-${{ steps.hermeto-vars.outputs.hermeto_key }}-${{ hashFiles('go.mod', 'go.sum', 'rpms.lock.yaml') }}

Comment thread
Fortune-Ndlovu marked this conversation as resolved.
Resolved
- name: Fetch dependencies with hermeto
if: steps.cache-deps.outputs.cache-hit != 'true'
shell: bash
run: |
set -ex

mkdir -p "$LOCAL_CACHE_DIR"

echo "::group::Fetching dependencies with hermeto"
podman run --rm -v "$PWD:/source:z" -v "$LOCAL_CACHE_DIR:/cachi2:z" -w /source "$HERMETO_IMAGE" \
Comment thread
Fortune-Ndlovu marked this conversation as resolved.
Resolved
--log-level DEBUG \
fetch-deps \
--source . \
--output /cachi2/output \
'[{"type": "rpm", "path": "."}, {"type": "gomod", "path": "."}]'
echo "::endgroup::"

if [ ! -d "$LOCAL_CACHE_DIR/output" ]; then
echo "No output directory found after fetch-deps"
exit 1
fi

echo "::group::Generating environment file"
podman run --rm -v "$PWD:/source:z" -v "$LOCAL_CACHE_DIR:/cachi2:z" -w /source "$HERMETO_IMAGE" \
--log-level DEBUG \
generate-env --format env \
--output /cachi2/cachi2.env /cachi2/output
echo "::endgroup::"

echo "::group::Injecting files"
podman run --rm -v "$PWD:/source:z" -v "$LOCAL_CACHE_DIR:/cachi2:z" -w /source "$HERMETO_IMAGE" \
--log-level DEBUG \
inject-files /cachi2/output
echo "::endgroup::"

- name: Generate env and inject files (cache hit)
if: steps.cache-deps.outputs.cache-hit == 'true'
shell: bash
run: |
set -ex
echo "::group::Generating environment file from cached deps"
podman run --rm -v "$PWD:/source:z" -v "$LOCAL_CACHE_DIR:/cachi2:z" -w /source "$HERMETO_IMAGE" \
--log-level DEBUG \
generate-env --format env \
--output /cachi2/cachi2.env /cachi2/output
echo "::endgroup::"

echo "::group::Injecting files from cached deps"
podman run --rm -v "$PWD:/source:z" -v "$LOCAL_CACHE_DIR:/cachi2:z" -w /source "$HERMETO_IMAGE" \
--log-level DEBUG \
inject-files /cachi2/output
echo "::endgroup::"

- name: Fix cache ownership for non-root buildah
shell: bash
run: |
set -ex
echo "LOCAL_CACHE_DIR_REALPATH=$(realpath "$LOCAL_CACHE_DIR")" >> "$GITHUB_ENV"
sudo chown -R runner "$(realpath "$LOCAL_CACHE_DIR")"
sudo chmod -R a+rwX "$(realpath "$LOCAL_CACHE_DIR")"

- name: Build Docker Image
id: build
uses: redhat-actions/buildah-build@719e3c40d8af9790c23eca13f7daa339f2867034 # v3.1.0
with:
containerfiles: ${{ inputs.containerfilePath }}
context: .
platform: ${{ inputs.platform }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
build-args: ${{ inputs.buildArgs }}
extra-args: |
--network=none
--volume ${{ env.LOCAL_CACHE_DIR_REALPATH }}:/cachi2:z

- name: Save image as artifact
if: ${{ inputs.skipArtifactUpload != 'true' }}
shell: bash
env:
TAGS_LIST: ${{ steps.meta.outputs.tags }}
run: |
mkdir -p ./podman-artifacts
echo "Saving images with tags:"
echo "$TAGS_LIST"
readarray -t tags <<< "$TAGS_LIST"
podman save "${tags[@]}" -o ./podman-artifacts/image.tar
echo "$TAGS_LIST" > ./podman-artifacts/tags.txt

- name: Upload image artifact
if: ${{ inputs.skipArtifactUpload != 'true' }}
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: podman-image-${{ github.event.number || github.ref_name }}
path: ./podman-artifacts/
retention-days: 1
if-no-files-found: error
9 changes: 9 additions & 0 deletions .github/renovate.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@
}
],
"customManagers": [
{
"customType": "regex",
"managerFilePatterns": ["/^Makefile$/"],
"matchStrings": [
"# renovate: datasource=docker depName=(?<depName>\\S+)\\nHERMETO_IMAGE \\?= \\S+:(?<currentValue>\\S+)"
],
"datasourceTemplate": "docker",
"versioningTemplate": "docker"
},
{
"customType": "regex",
"managerFilePatterns": ["/^\\.github/workflows/.*\\.ya?ml$/"],
Expand Down
104 changes: 56 additions & 48 deletions .github/workflows/container-build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ jobs:
image-publish:
name: Publish Container Image
runs-on: ubuntu-latest
timeout-minutes: 60
permissions:
contents: read
packages: write
id-token: write

steps:
- name: Checkout repository
uses: actions/checkout@v7
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7

- name: Log into registry ${{ vars.REGISTRY }}
uses: redhat-actions/podman-login@50c2d9a331bb67c8fdab99b86455fad05e2e3252 # v2
Expand All @@ -53,78 +54,84 @@ jobs:
EXPIRATION_TIMESTAMP=$(date -u -d "+14 days" +"%s")
echo "EXPIRATION_TIMESTAMP=${EXPIRATION_TIMESTAMP}" >> "$GITHUB_OUTPUT"

IMAGE_TAG=""
EXTRA_TAGS=""
VERSION=""

if [[ "${{ github.ref }}" == refs/tags/* ]]; then
# Git tag: push the tag version and derive major/minor tags
TAG_VERSION=${GITHUB_REF#refs/tags/}
echo "IMAGE_TAG=${TAG_VERSION}" >> "$GITHUB_OUTPUT"
echo "VERSION=${TAG_VERSION}" >> "$GITHUB_OUTPUT"
IMAGE_TAG="${TAG_VERSION}"
VERSION="${TAG_VERSION}"

# Parse semantic version to extract major and major.minor
if [[ ${TAG_VERSION} =~ ^([0-9]+)\.([0-9]+)\.([0-9]+) ]]; then
MAJOR="${BASH_REMATCH[1]}"
MINOR="${BASH_REMATCH[2]}"
echo "EXTRA_TAGS=${MAJOR} ${MAJOR}.${MINOR}" >> "$GITHUB_OUTPUT"
else
echo "EXTRA_TAGS=" >> "$GITHUB_OUTPUT"
EXTRA_TAGS="${MAJOR} ${MAJOR}.${MINOR}"
fi
elif [[ "${{ github.ref }}" == "refs/heads/main" ]]; then
# Main branch: push latest and latest-${commit}
echo "IMAGE_TAG=latest" >> "$GITHUB_OUTPUT"
echo "EXTRA_TAGS=latest-${SHORT_SHA}" >> "$GITHUB_OUTPUT"
IMAGE_TAG="latest"
Comment thread
Fortune-Ndlovu marked this conversation as resolved.
Resolved
EXTRA_TAGS="latest-${SHORT_SHA}"
VERSION="0.0.0-$(git describe --no-match --always --abbrev=9 --dirty --broken 2>/dev/null || echo unknown)"
echo "VERSION=${VERSION}" >> "$GITHUB_OUTPUT"
elif [[ "${{ github.ref }}" == refs/heads/release-* ]]; then
# Release branch: extract version and push latest-1.x and latest-1.x-${commit}
Comment thread
nickboldt marked this conversation as resolved.
Resolved
BRANCH_NAME=${GITHUB_REF#refs/heads/}
RELEASE_VERSION=${BRANCH_NAME#release-}
echo "IMAGE_TAG=latest-${RELEASE_VERSION}" >> "$GITHUB_OUTPUT"
echo "EXTRA_TAGS=latest-${RELEASE_VERSION}-${SHORT_SHA}" >> "$GITHUB_OUTPUT"
IMAGE_TAG="latest-${RELEASE_VERSION}"
EXTRA_TAGS="latest-${RELEASE_VERSION}-${SHORT_SHA}"
VERSION="0.0.0-$(git describe --no-match --always --abbrev=9 --dirty --broken 2>/dev/null || echo unknown)"
echo "VERSION=${VERSION}" >> "$GITHUB_OUTPUT"
fi

# Build and push using Makefile
- name: Build and push container image
echo "IMAGE_TAG=${IMAGE_TAG}" >> "$GITHUB_OUTPUT"
echo "EXTRA_TAGS=${EXTRA_TAGS}" >> "$GITHUB_OUTPUT"
echo "VERSION=${VERSION}" >> "$GITHUB_OUTPUT"

ALL_TAGS="${IMAGE_TAG}"
for t in ${EXTRA_TAGS}; do
ALL_TAGS="${ALL_TAGS}"$'\n'"${t}"
done
{
echo "ALL_TAGS<<EOF"
echo "${ALL_TAGS}"
echo "EOF"
} >> "$GITHUB_OUTPUT"

- name: Build container image (hermetic)
uses: ./.github/actions/docker-build
with:
imageName: ${{ vars.REGISTRY }}/${{ env.IMAGE_NAME }}
imageTags: |
${{ steps.prep.outputs.ALL_TAGS }}
buildArgs: |
RHDH_MUST_GATHER_VERSION=${{ steps.prep.outputs.VERSION }}
skipArtifactUpload: 'true'

- name: Push container image
id: build-push
env:
REGISTRY: ${{ vars.REGISTRY }}
IMAGE: ${{ env.IMAGE_NAME }}
MAIN_TAG: ${{ steps.prep.outputs.IMAGE_TAG }}
EXTRA_TAGS: ${{ steps.prep.outputs.EXTRA_TAGS }}
run: |
# Get the VERSION from prep step
VERSION="${{ steps.prep.outputs.VERSION }}"

# Build and push main tag
make image-push \
CONTAINER_TOOL=podman \
REGISTRY=${{ vars.REGISTRY }} \
IMAGE_NAME=${{ env.IMAGE_NAME}} \
IMAGE_TAG=${{ steps.prep.outputs.IMAGE_TAG }} \
BUILD_ARGS="--build-arg RHDH_MUST_GATHER_VERSION=${VERSION}"

# Get image digest
DIGEST=$(podman inspect --format='{{index .RepoDigests 0}}' ${{ vars.REGISTRY }}/${{ env.IMAGE_NAME}}:${{ steps.prep.outputs.IMAGE_TAG }} | cut -d'@' -f2)
MAIN_REF="${REGISTRY}/${IMAGE}:${MAIN_TAG}"
podman push --digestfile=/tmp/digest "${MAIN_REF}"
DIGEST=$(cat /tmp/digest)
echo "digest=${DIGEST}" >> "$GITHUB_OUTPUT"

# Save main tag for signing
echo "${{ vars.REGISTRY }}/${{ env.IMAGE_NAME}}:${{ steps.prep.outputs.IMAGE_TAG }}" > /tmp/image-tags.txt
echo "${MAIN_REF}" > /tmp/image-tags.txt

for TAG in ${EXTRA_TAGS}; do
REF="${REGISTRY}/${IMAGE}:${TAG}"
echo "Pushing extra tag: ${TAG}"
podman push "${REF}"

# Push extra tags if specified
- name: Push extra tags
if: steps.prep.outputs.EXTRA_TAGS != ''
run: |
# Process each extra tag (space-separated)
for TAG in ${{ steps.prep.outputs.EXTRA_TAGS }}; do
echo "Processing tag: ${TAG}"

# All extra tags reuse the main image to preserve digest
echo "Tagging and pushing: ${TAG}"
podman tag ${{ vars.REGISTRY }}/${{ env.IMAGE_NAME}}:${{ steps.prep.outputs.IMAGE_TAG }} ${{ vars.REGISTRY }}/${{ env.IMAGE_NAME}}:${TAG}
podman push ${{ vars.REGISTRY }}/${{ env.IMAGE_NAME}}:${TAG}

# Track commit-SHA tags for expiration
if [[ ${TAG} == latest-* ]]; then
echo "${TAG}" >> /tmp/expiry-tags.txt
fi

# Add tag for signing
echo "${{ vars.REGISTRY }}/${{ env.IMAGE_NAME}}:${TAG}" >> /tmp/image-tags.txt

echo "${REF}" >> /tmp/image-tags.txt
done

# Set expiration on commit-SHA tags via Quay.io API
Expand Down Expand Up @@ -184,6 +191,7 @@ jobs:
echo "" >> $GITHUB_STEP_SUMMARY
echo "- **Version**: ${{ steps.prep.outputs.VERSION }}" >> $GITHUB_STEP_SUMMARY
echo "- **Registry**: ${{ vars.REGISTRY }}" >> $GITHUB_STEP_SUMMARY
echo "- **Build**: Hermetic (\`--network=none\`)" >> $GITHUB_STEP_SUMMARY
echo "- **Main Tag**: \`${{ steps.prep.outputs.IMAGE_TAG }}\`" >> $GITHUB_STEP_SUMMARY
if [ -n "${{ steps.prep.outputs.EXTRA_TAGS }}" ]; then
echo "- **Extra Tags**:" >> $GITHUB_STEP_SUMMARY
Expand Down
Loading
Loading