Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@
# Ignore build and test binaries.
bin/
testbin/

# Hermetic build artifacts
hermeto-cache/
Dockerfile.hermeto
189 changes: 189 additions & 0 deletions .github/actions/docker-build/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
# 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 operator image hermetically using Hermeto (offline/reproducible build)
inputs:
imageName:
description: The full image name including registry (e.g., quay.io/rhdh-community/operator)
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 Dockerfile to use
required: false
default: 'Dockerfile'
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@c299e40c65443455700f0fdfc63efafe5b349051 # v5.10.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
shell: bash
run: |
# renovate: datasource=docker depName=quay.io/konflux-ci/hermeto
echo "HERMETO_IMAGE=quay.io/konflux-ci/hermeto:0.60.1" >> "$GITHUB_ENV"
echo "LOCAL_CACHE_DIR=./hermeto-cache/operator" >> "$GITHUB_ENV"

- name: Restore hermeto dependency cache
id: cache-deps
uses: actions/cache@5a3ec84eff668545956fd18022155c47e93e2684 # v4.2.3
with:
path: ./hermeto-cache/operator
key: hermeto-deps-${{ inputs.platform }}-${{ hashFiles('go.mod', 'go.sum', 'rpms.lock.yaml') }}

- 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" \
--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")"

- name: Transform Dockerfile for hermetic build
shell: bash
id: transform-containerfile
env:
CONTAINERFILE_PATH: ${{ inputs.containerfilePath }}
TRANSFORMED_CONTAINERFILE: ${{ inputs.containerfilePath }}.hermeto
run: |
set -x

cp "$CONTAINERFILE_PATH" "$TRANSFORMED_CONTAINERFILE"

# Insert RPM repo replacement before every dnf/microdnf install
sed -i '/RUN *\(dnf\|microdnf\) install/i RUN rm -r /etc/yum.repos.d/* && cp /cachi2/output/deps/rpm/$(uname -m)/repos.d/hermeto.repo /etc/yum.repos.d/' \
"$TRANSFORMED_CONTAINERFILE"

# Prepend cachi2 env sourcing to every RUN command
sed -i 's/^\s*RUN /RUN . \/cachi2\/cachi2.env \&\& /' "$TRANSFORMED_CONTAINERFILE"

echo "transformed_containerfile=$TRANSFORMED_CONTAINERFILE" >> "$GITHUB_OUTPUT"

- name: Build Docker Image
id: build
uses: redhat-actions/buildah-build@7a95fa7ee0f02d552a32753e7414641a04307056 # v2.13
with:
containerfiles: ${{ steps.transform-containerfile.outputs.transformed_containerfile }}
context: .
platform: ${{ inputs.platform }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
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 ./operator-podman-artifacts
echo "Saving images with tags:"
echo "$TAGS_LIST"
readarray -t tags <<< "$TAGS_LIST"
podman save "${tags[@]}" -o ./operator-podman-artifacts/image.tar
echo "$TAGS_LIST" > ./operator-podman-artifacts/tags.txt

- name: Upload image artifact
if: ${{ inputs.skipArtifactUpload != 'true' }}
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
with:
name: podman-image-${{ github.event.number || github.ref_name }}-${{ env.SHORT_SHA }}
path: ./operator-podman-artifacts/
retention-days: 1
if-no-files-found: error
21 changes: 11 additions & 10 deletions .github/workflows/next-container-build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,15 @@ jobs:
username: ${{ vars.QUAY_USERNAME }}
password: ${{ secrets.QUAY_TOKEN }}

- name: Build and push per-arch operator and catalog images
- name: Build operator image (hermetic)
uses: ./.github/actions/docker-build
with:
imageName: ${{ env.REGISTRY }}/${{ vars.REGISTRY_ORG }}/${{ vars.OPERATOR_IMAGE_NAME || 'operator' }}
imageTags: type=raw,value=${{ env.BASE_VERSION }}
platform: ${{ env.PLATFORM }}
skipArtifactUpload: 'true'

- name: Build catalog image and push per-arch images
run: |
set -euo pipefail
sudo apt-get -y update; sudo apt-get -y install podman
Expand All @@ -200,22 +208,15 @@ jobs:
export VERSION="${BASE_VERSION}"
export REGISTRY_WITH_ORG="${REGISTRY}/${REGISTRY_ORG}"
export IMAGE_TAG_BASE="${REGISTRY_WITH_ORG}/${OPERATOR_IMAGE_NAME}"
# Point catalog-build's bundle reference at the already-published, stable bundle
# tag from the dedicated `bundle` job instead of rebuilding/pushing it again here.
export BUNDLE_IMGS="${REGISTRY_WITH_ORG}/${OPERATOR_IMAGE_NAME}-bundle:${VERSION}"

: "${PLATFORM:?PLATFORM must be set}"
: "${SHORT_SHA:?SHORT_SHA must be set}"
: "${LATEST_NEXT:?LATEST_NEXT must be set}"
: "${OPERATOR_IMAGE_NAME:?OPERATOR_IMAGE_NAME must be set}"

# Build the operator image, then the catalog image. catalog-build's only other
# prerequisite (besides the bundle-push we're skipping) is `opm`, which downloads
# the opm CLI if needed -- that still runs normally. `-o bundle-push` tells make to
# treat that phony prerequisite as already satisfied, so `opm index add` runs
# directly against BUNDLE_IMGS above (a safe concurrent *read* of an already-stable
# tag) instead of each matrix leg re-pushing its own bundle to a shared tag first.
CONTAINER_TOOL="${CONTAINER_TOOL}" VERSION="${VERSION}" PLATFORM="${PLATFORM}" BUNDLE_IMGS="${BUNDLE_IMGS}" make -o bundle-push image-build catalog-build
# Build the catalog image only (operator was built hermetically above)
CONTAINER_TOOL="${CONTAINER_TOOL}" VERSION="${VERSION}" PLATFORM="${PLATFORM}" BUNDLE_IMGS="${BUNDLE_IMGS}" make -o bundle-push catalog-build

# Push per-arch tagged images
for image in "${OPERATOR_IMAGE_NAME}" "${OPERATOR_IMAGE_NAME}-catalog"; do
Expand Down
21 changes: 13 additions & 8 deletions .github/workflows/pr-container-build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,20 @@ jobs:
username: ${{ vars.QUAY_USERNAME }}
password: ${{ secrets.QUAY_TOKEN }}

- name: Build and push images
- name: Build operator image (hermetic)
uses: ./.github/actions/docker-build
with:
imageName: ${{ env.REGISTRY }}/${{ vars.REGISTRY_ORG }}/${{ vars.OPERATOR_IMAGE_NAME }}
imageTags: type=raw,value=${{ env.BASE_VERSION }}-pr-${{ steps.pr.outputs.number }}-${{ env.SHORT_SHA }}
platform: linux/amd64
skipArtifactUpload: 'true'

- name: Build bundle and catalog images, then push all
# We explicitly do NOT pass GH_TOKEN or RHDH_BOT_TOKEN here.
# This makes running 'make' safe, even if the Makefile is malicious,
# because there are no secrets in the env to steal.
env:
REGISTRY_ORG: ${{ vars.REGISTRY_ORG }}
OPERATOR_IMAGE_NAME: ${{ vars.OPERATOR_IMAGE_NAME }}
CONTAINER_TOOL: podman
# Construct version safely
VERSION: ${{ env.BASE_VERSION }}-pr-${{ steps.pr.outputs.number }}-${{ env.SHORT_SHA }}
run: |
sudo apt-get -y update; sudo apt-get -y install skopeo podman
Expand All @@ -106,11 +111,11 @@ jobs:

set -ex

# Run ONLY release-build (Lint removed)
# We use 'make' here for convenience, but it is sandboxed from secrets.
make release-build
# Build bundle and catalog (operator was built hermetically above)
make bundle bundle-build
BUNDLE_IMGS="${REGISTRY_WITH_ORG}/${OPERATOR_IMAGE_NAME}-bundle:${VERSION}" make -o bundle-push catalog-build

# Push logic
# Push all images
for image in ${OPERATOR_IMAGE_NAME} ${OPERATOR_IMAGE_NAME}-bundle ${OPERATOR_IMAGE_NAME}-catalog; do
podman push -q ${REGISTRY_WITH_ORG}/${image}:${VERSION} docker://${REGISTRY_WITH_ORG}/${image}:${VERSION}
skopeo --insecure-policy copy --all docker://${REGISTRY_WITH_ORG}/${image}:${VERSION} docker://${REGISTRY_WITH_ORG}/${image}:${VERSION}
Expand Down
20 changes: 11 additions & 9 deletions .github/workflows/pr-dockerbuild-validation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
validate-dockerfile:
name: Validate Dockerfile
runs-on: ubuntu-latest
timeout-minutes: 20
timeout-minutes: 30
permissions:
contents: read

Expand All @@ -31,6 +31,7 @@ jobs:
with:
files: |
.github/workflows/pr-dockerbuild-validation.yaml
.github/actions/docker-build/**
Makefile
**/*.go
bundle/**
Expand All @@ -42,19 +43,20 @@ jobs:
**/Containerfile
**/*.Dockerfile
**/.dockerignore
rpms.in.yaml
rpms.lock.yaml
scripts/**
files_ignore: |
**/*.md
**/*.adoc
.rhdh/**
tests/**

- name: Setup Go
- name: Build operator image (hermetic)
if: steps.changed-files.outputs.any_changed == 'true'
uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7
uses: ./.github/actions/docker-build
with:
Comment thread
Fortune-Ndlovu marked this conversation as resolved.
Resolved
go-version-file: 'go.mod'

- name: Test build Dockerfile (no push)
if: steps.changed-files.outputs.any_changed == 'true'
run: |
make image-build IMG=localhost/operator:validate
imageName: localhost/operator
imageTags: type=raw,value=validate
platform: linux/amd64
skipArtifactUpload: 'true'
4 changes: 2 additions & 2 deletions .github/workflows/update-rpm-lockfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ on:
- 'release-[0-9]+.[0-9]+'
paths:
- 'rpms.in.yaml'
- '.rhdh/docker/Dockerfile'
- 'Dockerfile'
- '.github/workflows/update-rpm-lockfile.yaml'

permissions:
contents: write

env:
DOCKERFILE_PATH: .rhdh/docker/Dockerfile
DOCKERFILE_PATH: Dockerfile

jobs:
update-lockfile:
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/validate-image-digests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ on:
- 'hack/validate-image-digests.sh'
- 'Dockerfile'
- '**/*bundle.Dockerfile'
- '.rhdh/docker/Dockerfile'

jobs:
validate-image-digests:
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ bin/*
bundle_tmp*
testbin/*
Dockerfile.cross
Dockerfile.hermeto
hermeto-cache/
__debug_bin*
tmp/*

Expand Down
Loading
Loading