-
-
Notifications
You must be signed in to change notification settings - Fork 89
188 lines (164 loc) · 7.97 KB
/
build.yml
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
name: Docker
on:
push:
tags: ["*"]
branches:
- "main"
- "master"
schedule:
- cron: '0 5 * * 0'
pull_request:
branches: ["**"]
env:
# Hostname of your registry
REGISTRY: docker.io
# Image repository, without hostname and tag
IMAGE_NAME: alpine/k8s
SHA: ${{ github.event.pull_request.head.sha || github.event.after }}
jobs:
get-kubectl-versions:
runs-on: ubuntu-latest
outputs:
versions: ${{ steps.set-versions.outputs.versions }}
steps:
- name: Get versions
id: set-versions
run: |
# jq 1.6
DEBIAN_FRONTEND=noninteractive
#sudo apt-get update && sudo apt-get -q -y install jq
curl -sL https://github.com/stedolan/jq/releases/download/jq-1.6/jq-linux64 -o jq
chmod +x jq
# Get the list of all releases tags, excludes alpha, beta, rc tags
releases=$(curl -s https://api.github.com/repos/kubernetes/kubernetes/releases | jq -r '.[].tag_name | select(test("alpha|beta|rc") | not)')
# Loop through the releases and extract the minor version number
for release in $releases; do
minor_version=$(echo $release | awk -F'.' '{print $1"."$2}')
# Check if the minor version is already in the array of minor versions
if [[ ! " ${minor_versions[@]} " =~ " ${minor_version} " ]]; then
minor_versions+=($minor_version)
fi
done
# Sort the unique minor versions in reverse order
sorted_minor_versions=($(echo "${minor_versions[@]}" | tr ' ' '\n' | sort -rV))
# Loop through the first 4 unique minor versions and get the latest version for each
for i in $(seq 0 3); do
minor_version="${sorted_minor_versions[$i]}"
latest_version=$(echo "$releases" | grep "^$minor_version\." | sort -rV | head -1 | sed 's/v//')
latest_versions+=($latest_version)
done
echo ${latest_versions[@]}
# Convert to JSON array for matrix
TARGETS=$(echo ${latest_versions[@]})
echo "versions=$(./jq -cn --arg environments "$TARGETS" '{target: $environments}')" >> $GITHUB_OUTPUT
echo $GITHUB_OUTPUT
build:
runs-on: ubuntu-latest
needs: get-kubectl-versions
permissions:
pull-requests: write
strategy:
matrix:
versions: ${{ fromJson(needs.get-kubectl-versions.outputs.versions) }}
steps:
- name: Setup Docker buildx
uses: docker/setup-buildx-action@v3
# Step to fetch the latest curl version
- name: Get latest curl version
id: curl-version
run: |
# export CURL_OPTIONS="-sL -H \"Authorization: token ${{ secrets.API_KEY }}\""
curl -H "Cache-Control: no-cache" -sL "https://raw.githubusercontent.com/alpine-docker/multi-arch-docker-images/stable/functions.sh" -o functions.sh
#curl -H "Cache-Control: no-cache" -sL "https://raw.githubusercontent.com/alpine-docker/multi-arch-docker-images/refs/heads/master/functions.sh" -o functions.sh
source functions.sh
# helm latest, hold the release candidates
helm=$(curl -s https://api.github.com/repos/helm/helm/releases | jq -r '.[].tag_name | select([startswith("v"), (contains("-rc") | not)] | all)' \
| sort -rV | head -n 1 |sed 's/v//')
echo "helm version is $helm"
echo "HELM_VERSION=$helm" >> $GITHUB_ENV
# kustomize latest
kustomize_release=$(curl -s https://api.github.com/repos/kubernetes-sigs/kustomize/releases | jq -r '.[].tag_name | select(contains("kustomize"))' \
| sort -rV | head -n 1)
kustomize_version=$(basename ${kustomize_release})
echo "kustomize version is $kustomize_version"
echo "KUSTOMIZE_VERSION=$kustomize_version" >> $GITHUB_ENV
# kubeseal latest
kubeseal_version=$(curl -s https://api.github.com/repos/bitnami-labs/sealed-secrets/releases | jq -r '.[].tag_name | select(startswith("v"))' \
| sort -rV | head -n 1 |sed 's/v//')
echo "kubeseal version is $kubeseal_version"
echo "KUBESEAL_VERSION=$kubeseal_version" >> $GITHUB_ENV
# krew latest
krew_version=$(curl -s https://api.github.com/repos/kubernetes-sigs/krew/releases | jq -r '.[].tag_name | select(startswith("v"))' \
| sort -rV | head -n 1 |sed 's/v//')
echo "krew version is $krew_version"
echo "KREW_VERSION=$krew_version" >> $GITHUB_ENV
# vals latest
vals_version=$(curl -s https://api.github.com/repos/helmfile/vals/releases | jq -r '.[].tag_name | select(startswith("v"))' \
| sort -rV | head -n 1 |sed 's/v//')
echo "vals version is $vals_version"
echo "VALS_VERSION=$vals_version" >> $GITHUB_ENV
# kubeconform latest
kubeconform_version=$(curl -s https://api.github.com/repos/yannh/kubeconform/releases | jq -r '.[].tag_name | select(startswith("v"))' \
| sort -rV | head -n 1 |sed 's/v//')
echo "kubeconform version is $kubeconform_version"
echo "KUBECONFORM_VERSION=$kubeconform_version" >> $GITHUB_ENV
# Authenticate to the container registry
- name: Authenticate to registry ${{ env.REGISTRY }}
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
# Extract metadata (tags, labels) for Docker
- name: Extract Docker metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
labels: |
org.opencontainers.image.revision=${{ env.SHA }}
tags: |
type=edge,branch=$repo.default_branch
type=semver,pattern=v{{version}}
type=sha,prefix=,suffix=,format=short
# Build and push Docker image with Buildx
# (don't push on PR, load instead)
- name: Build and push Docker image
id: build-and-push
uses: docker/build-push-action@v6
with:
platforms: linux/amd64,linux/arm64
sbom: ${{ github.event_name != 'pull_request' }}
provenance: ${{ github.event_name != 'pull_request' }}
push: ${{ github.event_name != 'pull_request' }}
load: ${{ github.event_name == 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
build-args: |
HELM_VERSION=${{ env.HELM_VERSION }}
KUBECTL_VERSION=${{ matrix.versions }}
KUSTOMIZE_VERSION=${{ env.KUSTOMIZE_VERSION }}
KUBESEAL_VERSION=${{ env.KUBESEAL_VERSION }}
KREW_VERSION=${{ env.KREW_VERSION }}
VALS_VERSION=${{ env.VALS_VERSION }}
KUBECONFORM_VERSION=${{ env.KUBECONFORM_VERSION }}
# - name: Checkout code
# uses: actions/checkout@v2
# - name: check the platform in multi-arch images
# run: |
# echo ${{ steps.meta.outputs.tags }}
# bash ./test.sh ${{ steps.meta.outputs.tags }}
#- name: set tags
# run: |
# # install crane
# curl -LO https://github.com/google/go-containerregistry/releases/download/v0.20.2/go-containerregistry_Linux_x86_64.tar.gz
# tar zxvf go-containerregistry_Linux_x86_64.tar.gz
# chmod +x crane
# export VERSION=($(docker run -i --rm ${{ steps.meta.outputs.tags }} curl --version|awk '$1=$1' |awk -F "[ -]" 'NR==1{print $2}'))
# echo $VERSION
# ./crane auth login -u ${{ secrets.DOCKERHUB_USERNAME }} -p ${{ secrets.DOCKERHUB_TOKEN }} index.docker.io
# ./crane copy ${{ steps.meta.outputs.tags }} ${{ env.IMAGE_NAME }}:latest
# ./crane copy ${{ steps.meta.outputs.tags }} ${{ env.IMAGE_NAME }}:${VERSION}
# rm -f /home/runner/.docker/config.json