Skip to content

Commit

Permalink
Inject version information into images
Browse files Browse the repository at this point in the history
  • Loading branch information
timebertt committed Nov 3, 2023
1 parent 3d893c2 commit 6f3e4db
Showing 1 changed file with 55 additions and 6 deletions.
61 changes: 55 additions & 6 deletions .github/workflows/controller-sharding.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,35 @@ jobs:
id: meta
# generate comma-separated tags and labels for image build (similar to docker/metadata-action)
run: |
build_date="$(date -u +%Y-%m-%dT%H:%M:%SZ)"
# revision is the commit sha
revision=${{ github.sha }}
# version is the commit sha, or the semantic version git tag
version=$revision
# short_ref is the branch name, or the pr-<number> string, or the semantic version git tag
short_ref=${{ github.ref_name }}
if ${{ github.event_name == 'pull_request' }} ; then
# for PR events, ref_name is '<pr-number>/merge' but tags may not contain /
short_ref=pr-${{ github.event.pull_request.number }}
fi
major_version="0"
minor_version="0+"
if [[ ${{ github.ref }} = refs/tags/* && $short_ref =~ ^v([0-9]+)\.([0-9]+)\.([0-9]+)([-].*)?([+].*)?$ ]] ; then
version=$short_ref
short_ref=$short_ref

major_version=${BASH_REMATCH[1]}
minor_version=${BASH_REMATCH[2]}
if [[ -n "${BASH_REMATCH[4]}" ]]; then
minor_version+="+"
fi
fi

tags=(
$short_ref
sha-$( echo ${{ github.sha }} | head -c7 )
sha-$( echo $revision | head -c7 )
)

if ${{ github.ref_name == 'master' }} ; then
Expand All @@ -72,19 +92,48 @@ jobs:
echo "tags=$(IFS=, ; echo "${tags[*]}")" >> $GITHUB_OUTPUT

labels=(
org.opencontainers.image.created=$( date -u +%Y-%m-%dT%H:%M:%SZ )
org.opencontainers.image.created=$build_date
org.opencontainers.image.licenses=Apache-2.0
org.opencontainers.image.revision=${{ github.sha }}
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=$short_ref
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
ko build --push=${{ github.event_name != 'pull_request' }} --sbom none --base-import-paths --platform linux/amd64,linux/arm64 \
--tags "${{ steps.meta.outputs.tags }}" --image-label "${{ steps.meta.outputs.labels }}" \
# 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[@]}"

0 comments on commit 6f3e4db

Please sign in to comment.