Skip to content

Commit c1814d6

Browse files
committed
Use ko to build images in CI
1 parent daa4528 commit c1814d6

File tree

1 file changed

+36
-55
lines changed

1 file changed

+36
-55
lines changed

.github/workflows/verify-build.yaml

Lines changed: 36 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -30,73 +30,54 @@ jobs:
3030
- uses: actions/checkout@v3
3131
with:
3232
path: go/src/github.com/timebertt/kubernetes-controller-sharding
33-
- name: Setup Go
34-
uses: actions/setup-go@v4
33+
- uses: actions/setup-go@v4
3534
with:
3635
go-version-file: go/src/github.com/timebertt/kubernetes-controller-sharding/webhosting-operator/go.mod
3736
cache-dependency-path: go/src/github.com/timebertt/kubernetes-controller-sharding/webhosting-operator/go.sum
3837
- name: Verify
3938
run: make -C webhosting-operator verify
4039

41-
buildAndPush:
40+
images:
4241
runs-on: ubuntu-latest
43-
strategy:
44-
matrix:
45-
include:
46-
- name: webhosting-operator
47-
context: webhosting-operator
4842
steps:
4943
- uses: actions/checkout@v3
50-
- name: Cache Docker layers
51-
uses: actions/cache@v3
44+
- uses: actions/setup-go@v4
5245
with:
53-
path: /tmp/.buildx-cache
54-
key: ${{ runner.os }}-buildx-${{ github.sha }}
55-
restore-keys: |
56-
${{ runner.os }}-buildx-
57-
- name: Setup Docker buildx
58-
uses: docker/setup-buildx-action@v2
46+
go-version-file: webhosting-operator/go.mod
47+
cache-dependency-path: webhosting-operator/go.sum
48+
- uses: ko-build/[email protected]
5949
with:
60-
install: true
61-
- name: Login to GHCR
62-
if: github.event_name != 'pull_request'
63-
uses: docker/login-action@v2
64-
with:
65-
registry: ghcr.io
66-
username: ${{ github.repository_owner }}
67-
password: ${{ secrets.GITHUB_TOKEN }}
68-
69-
- uses: docker/metadata-action@v4
50+
version: v0.14.1
51+
- name: Prepare image metadata
7052
id: meta
71-
with:
72-
images: |
73-
ghcr.io/${{ github.repository }}/${{ matrix.name }}
74-
tags: |
75-
type=ref,event=branch
76-
type=ref,event=tag
77-
type=ref,event=pr
78-
type=sha
79-
flavor: |
80-
latest=${{ endsWith(github.ref, github.event.repository.default_branch) }}
53+
# generate comma-separated tags and labels for image build (similar to docker/metadata-action)
54+
run: |
55+
tags=(
56+
${{ github.ref_name }}
57+
sha-$( echo ${{ github.sha }} | head -c7 )
58+
)
8159
82-
- name: Build and push
83-
uses: docker/build-push-action@v4
84-
with:
85-
context: ${{ matrix.context }}
86-
target: ${{ matrix.name }}
87-
platforms: linux/amd64
88-
push: ${{ github.event_name != 'pull_request' }}
89-
tags: ${{ steps.meta.outputs.tags }}
90-
labels: ${{ steps.meta.outputs.labels }}
91-
cache-from: type=local,src=/tmp/.buildx-cache
92-
cache-to: type=local,dest=/tmp/.buildx-cache-new,mode=max
93-
secrets: |
94-
GIT_AUTH_TOKEN=${{ secrets.GITHUB_TOKEN }}
60+
if [[ ${{ github.ref_name }} == master ]] ; then
61+
tags+=( latest )
62+
fi
63+
64+
echo "tags=$(IFS=, ; echo "${tags[*]}")" >> $GITHUB_OUTPUT
9565
96-
- name: Move cache
97-
# Temp fix
98-
# https://github.com/docker/build-push-action/issues/252
99-
# https://github.com/moby/buildkit/issues/1896
66+
labels=(
67+
org.opencontainers.image.created=$( date -u +%Y-%m-%dT%H:%M:%SZ )
68+
org.opencontainers.image.licenses=Apache-2.0
69+
org.opencontainers.image.revision=${{ github.sha }}
70+
org.opencontainers.image.source=https://github.com/${{ github.repository }}
71+
org.opencontainers.image.url=https://github.com/${{ github.repository }}
72+
org.opencontainers.image.version=${{ github.ref_name }}
73+
)
74+
75+
echo "labels=$(IFS=, ; echo "${labels[*]}")" >> $GITHUB_OUTPUT
76+
- name: ko build
10077
run: |
101-
rm -rf /tmp/.buildx-cache
102-
mv /tmp/.buildx-cache-new /tmp/.buildx-cache
78+
set -ex
79+
80+
cd ./webhosting-operator
81+
ko build --push=${{ github.event_name != 'pull_request' }} --sbom none --base-import-paths --platform linux/amd64,linux/arm64 \
82+
--tags "${{ steps.meta.outputs.tags }}" --image-label "${{ steps.meta.outputs.labels }}" \
83+
./cmd/webhosting-operator

0 commit comments

Comments
 (0)