chore: prepare repo for multi-arch builds #67
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
name: build-centos-image | |
on: | |
pull_request: | |
branches: | |
- main | |
schedule: | |
- cron: '05 10 * * *' # 10:05am UTC everyday | |
push: | |
branches: | |
- main | |
paths-ignore: | |
- '**/README.md' | |
workflow_dispatch: | |
env: | |
IMAGE_NAME: "rs-main-test" | |
IMAGE_DESC: "CentOS Stream-based images" | |
IMAGE_REGISTRY: "ghcr.io/${{ github.repository_owner }}" | |
DEFAULT_TAG: "latest" | |
CENTOS_VERSION: "stream10" | |
LOGO_URL: "https://avatars.githubusercontent.com/u/120078124?s=200&v=4" | |
README_URL: "https://raw.githubusercontent.com/${{ github.repository }}/main/README.md" | |
PLATFORMS: "amd64" | |
concurrency: | |
group: ${{ github.workflow }}-${{ github.ref || github.run_id }} | |
cancel-in-progress: true | |
jobs: | |
generate_matrix: | |
runs-on: ubuntu-latest | |
outputs: | |
matrix: ${{ steps.set-matrix.outputs.matrix }} | |
steps: | |
- name: Set matrix | |
id: set-matrix | |
run: | | |
# turn the comma separated string into a list | |
platforms=(${{ env.PLATFORMS }}) | |
MATRIX="{\"include\":[]}" | |
for platform in "${platforms[@]}"; do | |
MATRIX=$(echo $MATRIX | jq ".include += [{\"platform\": \"$platform\"}]") | |
done | |
echo "matrix=$(echo $MATRIX | jq -c '.')" >> $GITHUB_OUTPUT | |
build_push: | |
name: Build and push image | |
runs-on: ubuntu-24.04 | |
needs: generate_matrix | |
strategy: | |
fail-fast: false | |
matrix: ${{fromJson(needs.generate_matrix.outputs.matrix)}} | |
permissions: | |
contents: read | |
packages: write | |
id-token: write | |
env: | |
DEFAULT_TAG: local | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
- name: Setup Just | |
uses: extractions/setup-just@v2 | |
- name: Check Just Syntax | |
shell: bash | |
run: | | |
just check | |
- name: Maximize build space | |
uses: ublue-os/remove-unwanted-software@v7 | |
with: | |
remove-codeql: true | |
- name: Build Image | |
id: build-image | |
shell: bash | |
run: | | |
just=$(which just) | |
sudo $just build "${IMAGE_NAME}" "${DEFAULT_TAG}" | |
# Reprocess raw-img using rechunker which will delete it | |
- name: Run Rechunker | |
id: rechunk | |
uses: hhd-dev/[email protected] | |
with: | |
rechunk: 'ghcr.io/hhd-dev/rechunk:v1.0.1' | |
ref: "localhost/${{ env.IMAGE_NAME }}:${{ env.DEFAULT_TAG }}" | |
prev-ref: "${{ env.IMAGE_REGISTRY }}/${{ env.IMAGE_NAME }}:${{ env.DEFAULT_TAG }}" | |
skip_compression: true | |
version: ${{ env.CENTOS_VERSION }} | |
labels: | | |
org.opencontainers.image.title=${{ env.IMAGE_NAME }} | |
org.opencontainers.image.description=${{ env.IMAGE_DESC }} | |
io.artifacthub.package.readme-url=${{ env.README_URL }} | |
io.artifacthub.package.logo-url=${{ env.LOGO_URL }} | |
- name: Load in podman and tag | |
id: load | |
run: | | |
IMAGE=$(podman pull ${{ steps.rechunk.outputs.ref }}) | |
sudo rm -rf ${{ steps.rechunk.outputs.output }} | |
podman image tag $IMAGE ${{ env.IMAGE_REGISTRY }}/${{ env.IMAGE_NAME }}:${{ env.DEFAULT_TAG }} | |
podman image ls | |
podman inspect ${{ env.IMAGE_REGISTRY }}/${{ env.IMAGE_NAME }}:${{ env.DEFAULT_TAG }} | |
IMAGE_DIGEST=$(podman inspect --format '{{.Digest}}' ${{ env.IMAGE_REGISTRY }}/${{ env.IMAGE_NAME }}:${{ env.DEFAULT_TAG }}) | |
echo "image=$IMAGE" >> $GITHUB_OUTPUT | |
echo "digest=$IMAGE_DIGEST" >> $GITHUB_OUTPUT | |
# Workaround bug where capital letters in your GitHub username make it impossible to push to GHCR. | |
# https://github.com/macbre/push-to-ghcr/issues/12 | |
- name: Lowercase Registry | |
id: registry_case | |
uses: ASzc/change-string-case-action@v6 | |
with: | |
string: ${{ env.IMAGE_REGISTRY }} | |
- name: Login to GitHub Container Registry | |
uses: docker/login-action@v3 | |
with: | |
registry: ghcr.io | |
username: ${{ github.actor }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
# Push the image to GHCR (Image Registry) | |
- name: Push To GHCR | |
# if: github.event_name != 'pull_request' | |
id: push | |
env: | |
IMAGE_REGISTRY: ${{ steps.registry_case.outputs.lowercase }} | |
IMAGE_NAME: ${{ env.IMAGE_NAME }} | |
IMAGE_DIGEST: ${{ steps.load.outputs.digest }} | |
run: | | |
for i in {1..3}; do | |
echo "Running podman push --remove-signatures --digestfile=/tmp/digestfile $IMAGE_REGISTRY/$IMAGE_NAME@${IMAGE_DIGEST}" | |
podman push --remove-signatures --digestfile=/tmp/digestfile $IMAGE_REGISTRY/$IMAGE_NAME@${IMAGE_DIGEST} && break || sleep $((5 * i)); | |
done | |
REMOTE_IMAGE_DIGEST=$(cat /tmp/digestfile) | |
echo "remote_image_digest=$REMOTE_IMAGE_DIGEST" >> $GITHUB_OUTPUT | |
- name: Install Cosign | |
uses: sigstore/[email protected] | |
if: github.event_name != 'pull_request' | |
- name: Sign container image | |
if: github.event_name != 'pull_request' | |
run: | | |
IMAGE_FULL="${{ steps.registry_case.outputs.lowercase }}/${IMAGE_NAME}" | |
cosign sign -y --key env://COSIGN_PRIVATE_KEY ${IMAGE_FULL}@${{ steps.push.outputs.remote_image_digest }} | |
env: | |
TAGS: ${{ steps.push.outputs.digest }} | |
COSIGN_EXPERIMENTAL: false | |
COSIGN_PRIVATE_KEY: ${{ secrets.SIGNING_SECRET }} | |
- name: Create job outputs | |
env: | |
IMAGE_NAME: ${{ env.IMAGE_NAME }} | |
PLATFORM: ${{ matrix.platform }} | |
DIGEST: ${{ steps.push.outputs.remote_image_digest }} | |
run: | | |
mkdir -p /tmp/outputs/digests | |
echo "${DIGEST}" > /tmp/outputs/digests/${IMAGE_NAME}-${PLATFORM}.txt | |
- name: Upload Output Artifacts | |
uses: actions/upload-artifact@v4 | |
with: | |
name: ${{ env.IMAGE_NAME }}-${{ matrix.platform }} | |
retention-days: 1 | |
if-no-files-found: error | |
path: | | |
/tmp/outputs/digests/*.txt | |
manifest: | |
runs-on: ubuntu-latest | |
needs: | |
- build_push | |
permissions: | |
contents: read | |
packages: write | |
id-token: write | |
steps: | |
- name: Generate tags | |
id: generate-tags | |
shell: bash | |
run: | | |
# Generate a timestamp for creating an image version history | |
TIMESTAMP="$(date +%Y%m%d)" | |
COMMIT_TAGS=() | |
BUILD_TAGS=() | |
# Have tags for tracking builds during pull request | |
SHA_SHORT="${GITHUB_SHA::7}" | |
COMMIT_TAGS+=("pr-${{ github.event.number }}") | |
COMMIT_TAGS+=("${SHA_SHORT}") | |
# Append matching timestamp tags to keep a version history | |
for TAG in "${BUILD_TAGS[@]}"; do | |
BUILD_TAGS+=("${TAG}-${TIMESTAMP}") | |
done | |
BUILD_TAGS+=("${TIMESTAMP}") | |
BUILD_TAGS+=("${DEFAULT_TAG}") | |
BUILD_TAGS+=("${CENTOS_VERSION}") | |
BUILD_TAGS+=("${CENTOS_VERSION}.${TIMESTAMP}") | |
if [[ "${{ github.event_name }}" == "pull_request" ]]; then | |
echo "Generated the following commit tags: " | |
for TAG in "${COMMIT_TAGS[@]}"; do | |
echo "${TAG}" | |
done | |
alias_tags=("${COMMIT_TAGS[@]}") | |
else | |
alias_tags=("${BUILD_TAGS[@]}") | |
fi | |
echo "Generated the following build tags: " | |
for TAG in "${BUILD_TAGS[@]}"; do | |
echo "${TAG}" | |
done | |
echo "alias_tags=${alias_tags[*]}" >> $GITHUB_OUTPUT | |
- name: Fetch Build Outputs | |
uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4 | |
with: | |
pattern: ${{ env.IMAGE_NAME }}-*.txt | |
merge-multiple: true | |
path: /tmp/artifacts | |
- name: Load Outputs | |
id: load-outputs | |
run: | | |
DIGESTS_JSON=$(jq -n '{}') | |
for file in /tmp/artifacts/*.txt; do | |
# Extract the platform from the file name | |
PLATFORM=$(basename $digest_file | rev | cut -d'-' -f1 | rev | cut -d'.' -f1) | |
DIGEST=$(cat $digest_file) | |
# Add the platform and digest to the JSON object | |
DIGESTS_JSON=$(echo "$DIGESTS_JSON" | jq --arg key "$PLATFORM" --arg value "$DIGEST" '. + {($key): $value}') | |
done | |
echo "DIGESTS_JSON=$(echo $DIGESTS_JSON | jq -c '.')" >> $GITHUB_OUTPUT | |
- name: Create Manifest | |
id: create-manifest | |
run: | | |
podman manifest create ${{ env.IMAGE_REGISTRY }}/${{ env.IMAGE_NAME }} | |
echo "MANIFEST=${{ env.IMAGE_REGISTRY }}/${{ env.IMAGE_NAME }}" >> $GITHUB_OUTPUT | |
- name: Populate Manifest | |
env: | |
MANIFEST: ${{ steps.create-manifest.outputs.MANIFEST }} | |
DIGESTS_JSON: ${{ steps.load-outputs.outputs.DIGESTS_JSON }} | |
run: | | |
DIGESTS=$(echo "$DIGESTS_JSON" | jq -c '.') | |
PLATFORMS=(${{ env.PLATFORMS }}) | |
for platform in ${PLATFORMS[@]}; do | |
digest=$(echo $DIGESTS | jq -r ".$platform") | |
podman manifest add $MANIFEST ${{ env.IMAGE_REGISTRY }}/${{ env.IMAGE_NAME }}:$digest --arch $platform | |
done | |
- name: Login to GHCR | |
uses: docker/login-action@v3 | |
with: | |
registry: ghcr.io | |
username: ${{ github.actor }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- name: Tag Manifest | |
id: tag-manifest | |
run: | | |
MANIFEST_TAGS=() | |
for tag in ${{ steps.generate-tags.outputs.alias_tags }}; do | |
podman manifest tag ${{ steps.create-manifest.outputs.MANIFEST }} ${{ env.IMAGE_REGISTRY }}/${{ env.IMAGE_NAME }}:$tag | |
MANIFEST_TAGS+=(${{ env.IMAGE_REGISTRY }}/${{ env.IMAGE_NAME }}:$tag) | |
done | |
MANIFEST_TAGS_JSON=$(printf '%s\n' "${MANIFEST_TAGS[@]}" | jq -R . | jq -cs .) | |
echo "MANIFEST_TAGS_JSON=${MANIFEST_TAGS_JSON}" >> $GITHUB_OUTPUT | |
- name: Push Manifest | |
env: | |
MANIFEST: ${{ steps.create-manifest.outputs.MANIFEST }} | |
MANIFEST_TAGS_JSON: ${{ steps.tag-manifest.outputs.MANIFEST_TAGS_JSON }} | |
run: | | |
for tag in $(echo $MANIFEST_TAGS_JSON | jq -r '.[]'); do | |
podman manifest push --all=false $MANIFEST $tag | |
done |