generated from SAP/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 0
133 lines (114 loc) · 4.33 KB
/
docker-build.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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-9][6-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