Skip to content

Inject version information into images #8

Inject version information into images

Inject version information into images #8

name: controller-sharding
on:
release:
types:
- published
push:
branches:
- '*'
tags:
- v*
paths-ignore:
- "webhosting-operator/**"
- "**.md"
pull_request:
paths-ignore:
- "webhosting-operator/**"
- "**.md"
jobs:
verify:
runs-on: ubuntu-latest
# update-codegen requires the workspace to be placed in GOPATH.
# Set up env var, working directory, and check out at the correct path in GOPATH.
env:
GOPATH: /home/runner/work/kubernetes-controller-sharding/kubernetes-controller-sharding/go
defaults:
run:
working-directory: go/src/github.com/timebertt/kubernetes-controller-sharding
steps:
- uses: actions/checkout@v3
with:
path: go/src/github.com/timebertt/kubernetes-controller-sharding
- uses: actions/setup-go@v4
with:
go-version-file: go/src/github.com/timebertt/kubernetes-controller-sharding/go.mod
cache-dependency-path: go/src/github.com/timebertt/kubernetes-controller-sharding/go.sum
- name: Verify
run: make verify
images:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version-file: go.mod
- uses: ko-build/[email protected]
with:
version: v0.15.0
- name: Prepare image metadata
id: meta
# generate comma-separated tags and labels for image build (similar to docker/metadata-action)
run: |
set -ex
build_date="$(date -u +%Y-%m-%dT%H:%M:%SZ)"
# revision is the commit sha
revision=${{ github.sha }}
# short_ref is the branch name, or the semver git tag
short_ref=${{ github.ref_name }}
# version is the semantic version
version=v0.1.0-dev # used if no semver tag has been pushed yet
major_version="0"
minor_version="1"
if [[ ${{ github.ref }} = refs/tags/* && $short_ref =~ ^v([0-9]+)\.([0-9]+)\.([0-9]+)([-].*)?([+].*)?$ ]] ; then
# if triggered for a semver tag, extract its information
version=$short_ref
major_version=${BASH_REMATCH[1]}
minor_version=${BASH_REMATCH[2]}
elif [[ "$(git describe --tags --match "v*.*.*" --abbrev=0 2>/dev/null)" =~ ^v([0-9]+)\.([0-9]+)\.([0-9]+)$ ]] ; then
# otherwise, find the previous semver tag, extract its information, bump minor/patch, and append -dev
major_version=${BASH_REMATCH[1]}
minor_version=${BASH_REMATCH[2]}
patch_version=${BASH_REMATCH[3]}
if [[ $short_ref = release-* ]] ; then
(( patch_version++ ))
else
(( minor_version++ ))
fi
version=v$major_version.$minor_version.$patch_version-dev
fi
tags=(
$short_ref
sha-$( echo $revision | head -c7 )
)
if [[ $short_ref = master ]] ; then
tags+=( latest )
fi
echo "tags=$(IFS=, ; echo "${tags[*]}")" >> $GITHUB_OUTPUT
labels=(
org.opencontainers.image.created=$build_date
org.opencontainers.image.licenses=Apache-2.0
org.opencontainers.image.revision=$revision
org.opencontainers.image.source=https://github.com/${{ github.repository }}
org.opencontainers.image.url=https://github.com/${{ github.repository }}
org.opencontainers.image.version=$version
)
echo "labels=$(IFS=, ; echo "${labels[*]}")" >> $GITHUB_OUTPUT
# calculate ldflags for injecting version information into binaries
tree_state="$([ -z "$(git status --porcelain 2>/dev/null)" ] && echo clean || echo dirty)"
echo "LDFLAGS<<EOF
-X k8s.io/component-base/version.gitMajor=$major_version
-X k8s.io/component-base/version.gitMinor=$minor_version
-X k8s.io/component-base/version.gitVersion=$version
-X k8s.io/component-base/version.gitTreeState=$tree_state
-X k8s.io/component-base/version.gitCommit=$revision
-X k8s.io/component-base/version.buildDate=$build_date
-X k8s.io/component-base/version/verflag.programName=kubernetes-controller-sharding
EOF" >> $GITHUB_ENV
- name: ko build
run: |
set -ex
# prepare .ko.yaml to inject build settings into all images
entrypoints=(
./cmd/sharder
)
echo builds: > .ko.yaml
for entrypoint in "${entrypoints[@]}" ; do
cat >> .ko.yaml <<EOF
- main: $entrypoint
ldflags:
- |
{{.Env.LDFLAGS}}
EOF
done
ko build --push=${{ github.event_name != 'pull_request' }} --sbom none --base-import-paths \
--tags "${{ steps.meta.outputs.tags }}" --image-label "${{ steps.meta.outputs.labels }}" \
--platform linux/amd64,linux/arm64 \
"${entrypoints[@]}"