Build kubectl image #187
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 kubectl image | |
on: | |
schedule: | |
# This will run the job daily at 00:13 UTC | |
- cron: "13 0 * * *" | |
workflow_dispatch: | |
env: | |
REGISTRY: ghcr.io | |
REGCTL_VERSION: v0.5.7 | |
SEMVER_VERSION: 3.4.0 | |
defaults: | |
run: | |
shell: bash | |
jobs: | |
prepare-matrix: | |
name: Prepare matrix | |
runs-on: ubuntu-latest | |
outputs: | |
matrix: ${{ steps.prepare-matrix.outputs.matrix }} | |
build: ${{ steps.prepare-matrix.outputs.build }} | |
repository: ${{ steps.prepare-repository-name.outputs.repository }} | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
- name: Setup regctl | |
uses: regclient/actions/regctl-installer@main | |
with: | |
release: ${{ env.REGCTL_VERSION }} | |
install-dir: ${{ runner.temp }}/bin | |
- name: Setup semver | |
uses: sap/cs-actions/setup-semver@main | |
with: | |
version: ${{ env.SEMVER_VERSION }} | |
install-directory: ${{ runner.temp }}/bin | |
- name: Prepare repository name | |
id: prepare-repository-name | |
run: | | |
repository=$REGISTRY/${{ github.repository }} | |
echo "repository=${repository,,}" >> $GITHUB_OUTPUT | |
- name: Log in to the registry | |
run: | | |
regctl registry login $REGISTRY --user ${{ github.actor }} --pass-stdin <<< ${{ github.token }} | |
- name: Fetch k8s versions | |
id: fetch-k8s-versions | |
run: | | |
page=1 | |
while true; do | |
release_info=$(curl \ | |
--silent \ | |
--header "Accept: application/vnd.github+json" \ | |
--header "Authorization: Bearer ${{ github.token }}" \ | |
--header "X-GitHub-Api-Version: 2022-11-28" \ | |
"https://api.github.com/repos/kubernetes/kubernetes/tags?page=${page}&per_page=100" | |
) | |
tag_names=$(echo "${release_info}" | grep "name" | awk '{ print substr($2, 2, length($2)-3) }') | |
[[ "${tag_names}" == "" ]] && break | |
tag_names=$(echo "$tag_names" | grep -vE 'alpha|beta|rc') | |
echo "${tag_names}" | awk '/^v1\.(2[6-9]|[3-9][0-9])\..*/||/^v[2-9]\..*/' | sort -V >> k8s_released_versions.txt | |
sed -i '/^$/d' k8s_released_versions.txt | |
((page=page+1)) | |
done | |
shell: bash {0} | |
- name: Fetch built versions | |
id: fetch-built-versions | |
run: | | |
regctl tag list ${{ steps.prepare-repository-name.outputs.repository }} | sort -V > gh_com_tags.txt | |
- name: Print unbuilt versions and prepare matrix | |
id: prepare-matrix | |
run: | | |
unbuilt_versions=$(comm -23 <(sort k8s_released_versions.txt) <(sort gh_com_tags.txt) | tr '\n' ',' | sed 's/,$//') | |
if [[ "${unbuilt_versions}" == "" ]] | |
then | |
echo "All the released k8s versions were built already" | |
exit 0 | |
else | |
echo "Unbuilt versions: $unbuilt_versions" | |
echo "matrix={\"k8s-version\":[$(echo $unbuilt_versions | sed 's/\([^,]*\)/"\1"/g')]}" >> $GITHUB_OUTPUT | |
echo "build=true" >> $GITHUB_OUTPUT | |
fi | |
build-image: | |
name: Build image | |
if: needs.prepare-matrix.outputs.build == 'true' | |
needs: prepare-matrix | |
runs-on: ubuntu-latest | |
strategy: | |
matrix: ${{fromJson(needs.prepare-matrix.outputs.matrix)}} | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
- name: Setup Docker Buildx | |
uses: docker/setup-buildx-action@v3 | |
- name: Log in to the Container registry | |
uses: docker/login-action@v3 | |
with: | |
registry: ${{ env.REGISTRY }} | |
username: ${{ github.actor }} | |
password: ${{ github.token }} | |
- name: Build and push docker image | |
uses: docker/build-push-action@v5 | |
with: | |
platforms: linux/amd64,linux/arm64 | |
context: . | |
file: ./Dockerfile | |
tags: ${{ needs.prepare-matrix.outputs.repository }}:${{ matrix['k8s-version'] }} | |
cache-from: | | |
type=gha,scope=sha-${{ github.sha }} | |
type=gha,scope=${{ github.ref_name }} | |
type=gha,scope=${{ github.base_ref || 'main' }} | |
type=gha,scope=main | |
cache-to: | | |
type=gha,scope=sha-${{ github.sha }},mode=max | |
type=gha,scope=${{ github.ref_name }},mode=max | |
build-args: "KUBECTL_VERSION=${{ matrix['k8s-version'] }}" | |
push: true |