Build and push k3s image to GHCR

Signed-off-by: Derek Nola <derek.nola@suse.com>
This commit is contained in:
Derek Nola
2025-04-23 10:36:37 -07:00
parent 1797b5abc1
commit 07171fd7e8
4 changed files with 142 additions and 9 deletions

View File

@@ -11,6 +11,12 @@ on:
default: 'linux'
upload-image:
type: boolean
description: 'Build and upload k3s image (only works on arm64 or amd64)'
required: false
default: false
upload-build:
type: boolean
description: 'Upload contents of build/out, used to build the k3s image externally'
required: false
default: false
cache:
@@ -45,7 +51,7 @@ jobs:
if: inputs.arch == 'arm'
uses: docker/setup-qemu-action@v3
with:
platforms: linux/arm/v7
cache-image: false
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
@@ -70,7 +76,7 @@ jobs:
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build K3s Binary Native
if: inputs.arch != 'arm'
if: inputs.arch == 'arm64' || inputs.arch == 'amd64'
env:
DOCKER_BUILD_SUMMARY: false
uses: docker/build-push-action@v6
@@ -122,15 +128,20 @@ jobs:
fi
- name: Build K3s image
if: inputs.upload-image == true && inputs.os == 'linux'
if: inputs.upload-image == true && inputs.os == 'linux' && (inputs.arch == 'amd64' || inputs.arch == 'arm64')
run: ./scripts/package-image
- name: "Save K3s image"
if: inputs.upload-image == true && inputs.os == 'linux'
run: docker image save rancher/k3s -o ./dist/artifacts/k3s-image.tar
- name: "Save K3s build"
if: inputs.upload-build == true && inputs.os == 'linux'
run: |
mv ./build/out/data-linux.tar.zst ./dist/artifacts/data-linux${{ env.ARCH_EXT }}.tar.zst
- name: "Upload K3s Artifacts"
uses: actions/upload-artifact@v4
with:
name: k3s${{ env.ARCH_EXT }}
path: dist/artifacts/k3s*
path: dist/artifacts/

104
.github/workflows/release.yml vendored Normal file
View File

@@ -0,0 +1,104 @@
name: K3s Release
on:
release:
types: [published]
permissions:
contents: read
packages: read
jobs:
build-amd64:
name: Build Binary (amd64)
uses: ./.github/workflows/build-k3s.yaml
with:
cache: '' # No cache for release builds
upload-build: true
build-arm64:
name: Build Binary (arm64)
uses: ./.github/workflows/build-k3s.yaml
with:
arch: arm64
cache: '' # No cache for release builds
upload-build: true
build-arm:
name: Build Binary (arm)
uses: ./.github/workflows/build-k3s.yaml
with:
arch: arm
cache: '' # No cache for release builds
upload-build: true
push-release-image:
name: Build and Push Multi-Arch Image
runs-on: ubuntu-latest
permissions:
packages: write # Needed to push images to GHCR
needs: [build-amd64, build-arm64, build-arm]
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: COnfigure image tags
id: tag_config
run: |
TAG=${GITHUB_REF#refs/tags/}
# Base configuration - always transform the main tag
# Transforms v1.32.4-rc1+k3s1 → v1.32.4-rc1-k3s1
BASE_CONFIG="type=raw,value=${TAG//+/-}"
if [[ "${TAG,,}" == *"rc"* ]]; then
echo "RC release detected: $TAG"
echo "tag_spec=$BASE_CONFIG" >> $GITHUB_OUTPUT
else
echo "Stable release detected: $TAG"
echo "tag_spec=$BASE_CONFIG
type=semver,pattern=v{{major}}.{{minor}}" >> $GITHUB_OUTPUT
fi
- name: Extract Docker metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ghcr.io/${{ github.repository_owner }}/k3s
flavor: latest=false
tags: ${{ steps.tag_config.outputs.tag_spec }}
- name: "Download K3s build"
uses: actions/download-artifact@v4
with:
pattern: k3s*
path: ./dist/artifacts
merge-multiple: true
- name: Prepare build folder
run: |
mkdir -p ./build/out
cp ./dist/artifacts/data-* ./build/out
- name: Build and push K3s runtime image
uses: docker/build-push-action@v6
with:
context: .
file: ./package/Dockerfile
platforms: linux/amd64,linux/arm64,linux/arm/v7
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
build-args: |
DRONE_TAG=${{ github.ref_name }}