-
Notifications
You must be signed in to change notification settings - Fork 0
165 lines (128 loc) · 6.58 KB
/
workflow.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
name: build and push container image
on:
workflow_dispatch:
push:
branches:
- main
pull_request:
branches:
- main
env:
CONTAINER_REGISTRY: ghcr.io
CONTAINER_IMAGE_NAME: ${{ github.repository }}
jobs:
hadolint:
name: lint Dockerfile(s)
runs-on: ubuntu-24.04
timeout-minutes: 2
permissions:
contents: read
steps:
- name: checkout
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
- name: lint Dockerfile(s)
uses: hadolint/hadolint-action@54c9adbab1582c2ef04b2016b760714a4bfde3cf # v3.1.0
with:
failure-threshold: style
ignore: DL3018,DL4006
docker-buildx:
name: build (and push) container image
needs: hadolint
runs-on: ubuntu-24.04
timeout-minutes: 5
permissions:
contents: write
packages: write
steps:
- name: checkout
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
- name: fetch tags
run: git fetch --tags
- name: setup docker buildx
uses: docker/setup-buildx-action@6524bf65af31da8d45b59e8c27de4bd072b392f5 # v3
- name: build the container image
uses: docker/build-push-action@b32b51a8eda65d6793cd0494a773d4f6bcef32dc # v6
with:
context: .
platforms: amd64
push: false
cache-to: type=local,dest=/tmp/.buildx-cache
cache-from: type=registry,ref=${{ env.CONTAINER_REGISTRY }}/${{ env.CONTAINER_IMAGE_NAME }}:cache,ignore-error=true
- name: extract kubernetes/dns versions from Dockerfile
if: github.event_name != 'pull_request' && github.ref == format('refs/heads/{0}', github.event.repository.default_branch)
run: |
set -eux
KUBERNETES_DNS_VERSION="$(grep -Eom1 'KUBERNETES_DNS_VERSION=[0-9]+\.[0-9]+\.[0-9]+\S*' Dockerfile | cut -d= -f2)"
echo "KUBERNETES_DNS_VERSION=${KUBERNETES_DNS_VERSION:?}" | tee -a "${GITHUB_ENV:?}"
- name: create a new git tag
if: github.event_name != 'pull_request' && github.ref == format('refs/heads/{0}', github.event.repository.default_branch)
run: |
set -eux
MAYBE_LATEST_TAG="$(git tag --list "${KUBERNETES_DNS_VERSION:?}+build.[0-9]*" --sort=version:refname | tail -n1)"
# no latest tag existing (`-z` for String length is zero)
if test -z "${MAYBE_LATEST_TAG:-}"; then
NEW_BUILD=0
NEXT_TAG="${KUBERNETES_DNS_VERSION:?}+build.${NEW_BUILD:?}"
CURRENT_HASH="$(git rev-parse --verify HEAD)"
git tag "${NEXT_TAG:?}" "${CURRENT_HASH:?}"
git push origin "${NEXT_TAG:?}"
# make the build number available as environment variable in the following workflow steps
echo "BUILD_NUMBER=${NEW_BUILD:?}" | tee -a "${GITHUB_ENV:?}"
exit 0
fi
LATEST_TAG=${MAYBE_LATEST_TAG:?}
LATEST_BUILD=$(echo "${LATEST_TAG:?}" | rev | cut --delimiter="." --fields=1 | rev)
CURRENT_HASH="$(git rev-parse --verify HEAD)"
# no new tag is needed (current commit == commit of latest tag)
LATEST_TAG_GIT_HASH="$(git rev-list -n 1 ${LATEST_TAG})"
if test "${LATEST_TAG_GIT_HASH:?}" = "${CURRENT_HASH:?}"; then
# make the build number available as environment variable in the following workflow steps
echo "BUILD_NUMBER=${LATEST_BUILD:?}" | tee -a "${GITHUB_ENV:?}"
exit 0
fi
# new tag is needed
NEW_BUILD=$((LATEST_BUILD + 1))
NEXT_TAG="${KUBERNETES_DNS_VERSION:?}+build.${NEW_BUILD:?}"
# create and push the new tag
git tag "${NEXT_TAG:?}" "${CURRENT_HASH:?}"
git push origin "${NEXT_TAG:?}"
# make the build number available as environment variable in the following workflow steps
echo "BUILD_NUMBER=${NEW_BUILD:?}" | tee -a "${GITHUB_ENV:?}"
# prepare container image tags from iptables and kubernetes/dns versions and container image labels from git metadata (repository url, commit hash, ...)
- name: prepare container image labels and container image tags
if: github.event_name != 'pull_request' && github.ref == format('refs/heads/{0}', github.event.repository.default_branch)
id: meta
uses: docker/metadata-action@369eb591f429131d6889c46b94e711f089e6ca96 # v5
with:
images: ${{ env.CONTAINER_REGISTRY }}/${{ env.CONTAINER_IMAGE_NAME }}
tags: | # https://github.com/docker/metadata-action#typeraw
type=raw,value=${{ env.KUBERNETES_DNS_VERSION }}-build.${{ env.BUILD_NUMBER }}
type=raw,value=${{ env.KUBERNETES_DNS_VERSION }}
flavor: |
latest=true
- name: login to the container registry
if: github.event_name != 'pull_request' && github.ref == format('refs/heads/{0}', github.event.repository.default_branch)
uses: docker/login-action@9780b0c442fbb1117ed29e0efdff1e18412f7567 # v3
with:
registry: ${{ env.CONTAINER_REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: build and push the container image
if: github.event_name != 'pull_request' && github.ref == format('refs/heads/{0}', github.event.repository.default_branch)
uses: docker/build-push-action@b32b51a8eda65d6793cd0494a773d4f6bcef32dc # v6
with:
context: .
labels: |
org.opencontainers.image.title=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.title'] }}
org.opencontainers.image.description=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.description'] }}
org.opencontainers.image.url=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.url'] }}
org.opencontainers.image.source=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.source'] }}
org.opencontainers.image.version=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.version'] }}
org.opencontainers.image.revision=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.revision'] }}
org.opencontainers.image.licenses=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.licenses'] }}
provenance: false
platforms: linux/amd64
push: true
tags: ${{ steps.meta.outputs.tags }}
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=registry,ref=${{ env.CONTAINER_REGISTRY }}/${{ env.CONTAINER_IMAGE_NAME }}:cache,mode=max