147 lines
5.4 KiB
YAML
147 lines
5.4 KiB
YAML
name: Build container image
|
|
|
|
on:
|
|
pull_request:
|
|
branches:
|
|
- main
|
|
schedule:
|
|
- cron: '05 12 * * 2,5'
|
|
workflow_dispatch:
|
|
|
|
env:
|
|
IMAGE_DESC: "Custom Fedora bootc image"
|
|
IMAGE_NAME: "${{ github.event.repository.name }}"
|
|
DEFAULT_TAG: "stable"
|
|
|
|
concurrency:
|
|
group: ${{ github.workflow }}-${{ github.ref || github.run_id }}
|
|
cancel-in-progress: true
|
|
|
|
jobs:
|
|
build_push:
|
|
name: Build and push image
|
|
runs-on: ubuntu-24.04
|
|
|
|
permissions:
|
|
contents: read
|
|
packages: write
|
|
id-token: write
|
|
|
|
steps:
|
|
- name: Prepare environment
|
|
run: |
|
|
# Extrahiert die reine Domain/Host (inkl. Port, falls vorhanden) aus der Gitea-URL
|
|
REGISTRY_HOST=$(echo "${{ github.server_url }}" | awk -F/ '{print $3}')
|
|
OWNER_LOWER="${{ github.repository_owner }}"
|
|
NAME_LOWER="${{ env.IMAGE_NAME }}"
|
|
|
|
echo "REGISTRY_HOST=${REGISTRY_HOST}" >> ${GITHUB_ENV}
|
|
echo "IMAGE_REGISTRY=${REGISTRY_HOST}/${OWNER_LOWER,,}" >> ${GITHUB_ENV}
|
|
echo "IMAGE_NAME=${NAME_LOWER,,}" >> ${GITHUB_ENV}
|
|
|
|
- name: Determine image tag
|
|
run: |
|
|
BRANCH="${GITHUB_REF_NAME}"
|
|
if [[ "$BRANCH" == "main" ]]; then
|
|
echo "IMAGE_TAG=${DEFAULT_TAG}" >> ${GITHUB_ENV}
|
|
echo "IS_TEST_BUILD=false" >> ${GITHUB_ENV}
|
|
else
|
|
SANITIZED=$(echo "$BRANCH" | sed 's/[^a-zA-Z0-9._-]/-/g' | head -c 50)
|
|
echo "IMAGE_TAG=test-${SANITIZED}" >> ${GITHUB_ENV}
|
|
echo "IS_TEST_BUILD=true" >> ${GITHUB_ENV}
|
|
fi
|
|
|
|
- name: Checkout
|
|
uses: actions/checkout@v5
|
|
|
|
- name: Set up Docker Buildx
|
|
uses: docker/setup-buildx-action@v3
|
|
|
|
- name: Image Metadata
|
|
id: metadata
|
|
uses: docker/metadata-action@v5
|
|
with:
|
|
images: ${{ env.REGISTRY_HOST }}/${{ github.repository }}
|
|
tags: |
|
|
type=raw,value=${{ env.IMAGE_TAG }}
|
|
type=raw,value=${{ env.IMAGE_TAG }}.{{date 'YYYYMMDD'}},enable=${{ env.IS_TEST_BUILD == 'false' }}
|
|
type=raw,value={{date 'YYYYMMDD'}},enable=${{ env.IS_TEST_BUILD == 'false' }}
|
|
type=sha,enable=${{ github.event_name == 'pull_request' }}
|
|
type=ref,event=pr
|
|
labels: |
|
|
org.opencontainers.image.created={{date 'YYYY-MM-DDTHH:mm:ssZ'}}
|
|
org.opencontainers.image.description=${{ env.IMAGE_DESC }}
|
|
org.opencontainers.image.documentation=${{ github.server_url }}/${{ github.repository }}/raw/commit/${{ github.sha }}/README.md
|
|
org.opencontainers.image.source=${{ github.server_url }}/${{ github.repository }}/src/commit/${{ github.sha }}/Containerfile
|
|
org.opencontainers.image.title=${{ env.IMAGE_NAME }}
|
|
org.opencontainers.image.url=${{ github.server_url }}/${{ github.repository }}
|
|
org.opencontainers.image.vendor=${{ github.repository_owner }}
|
|
org.opencontainers.image.version=${{ env.IMAGE_TAG }}.{{date 'YYYYMMDD'}}
|
|
containers.bootc=1
|
|
ostree.bootable=1
|
|
|
|
- name: Install Skopeo
|
|
run: |
|
|
sudo apt-get update
|
|
sudo apt-get install -y skopeo
|
|
|
|
- name: Login to Gitea Container Registry
|
|
if: github.event_name != 'pull_request'
|
|
uses: docker/login-action@v3
|
|
with:
|
|
registry: ${{ env.REGISTRY_HOST }}
|
|
username: ${{ github.repository_owner }}
|
|
password: ${{ secrets.REGISTRY }}
|
|
|
|
- name: Build Image Locally
|
|
uses: docker/build-push-action@v6
|
|
id: build_image
|
|
with:
|
|
context: .
|
|
file: ./Containerfile
|
|
push: false # Docker baut nur, schiebt aber nichts hoch
|
|
load: true # Lädt das Image direkt in den lokalen Docker-Daemon des Runners
|
|
tags: ${{ steps.metadata.outputs.tags }}
|
|
labels: ${{ steps.metadata.outputs.labels }}
|
|
|
|
- name: Push Image via Skopeo (Convert to OCI)
|
|
if: github.event_name != 'pull_request'
|
|
run: |
|
|
# Loop über alle von metadata-action generierten Tags
|
|
for tag in ${{ steps.metadata.outputs.tags }}; do
|
|
echo "Pushing and converting $tag to clean OCI format..."
|
|
skopeo copy \
|
|
--format oci \
|
|
--dest-creds "${{ github.repository_owner }}:${{ secrets.REGISTRY }}" \
|
|
docker-daemon:$tag \
|
|
docker://$tag
|
|
done
|
|
|
|
- name: Install envsubst dependencies
|
|
if: github.event_name != 'pull_request'
|
|
run: |
|
|
sudo apt-get update && sudo apt-get install -y gettext
|
|
|
|
- name: Install Cosign
|
|
if: github.event_name != 'pull_request'
|
|
uses: sigstore/cosign-installer@v4.1.2
|
|
|
|
- name: Sign container image
|
|
if: github.event_name != 'pull_request' && env.IS_TEST_BUILD == 'false'
|
|
run: |
|
|
# Dynamische Ziel-URL zusammenbauen
|
|
IMAGE_REF="${{ env.IMAGE_REGISTRY }}/${{ env.IMAGE_NAME }}:${{ env.IMAGE_TAG }}"
|
|
|
|
echo "Fetching digest for $IMAGE_REF..."
|
|
DIGEST=$(skopeo inspect --creds "${{ github.repository_owner }}:${{ secrets.REGISTRY }}" docker://$IMAGE_REF --format '{{.Digest}}')
|
|
|
|
echo "Signing OCI reference..."
|
|
cosign sign -y \
|
|
--key env://COSIGN_PRIVATE_KEY \
|
|
--registry-referrers-mode=oci-1-1 \
|
|
"${{ env.IMAGE_REGISTRY }}/${{ env.IMAGE_NAME }}@${DIGEST}"
|
|
env:
|
|
COSIGN_PRIVATE_KEY: ${{ secrets.SIGNING_SECRET }}
|
|
COSIGN_PASSWORD: ""
|
|
COSIGN_EXPERIMENTAL: "1"
|