Skip to content

Commit cb15ff4

Browse files
authored
[2.14 main] Removing dapper from the build process (#1150)
* Removing dapper from the build process * Move binary build into Dockerfile * Eliminate redundant build * Add validate-ci run * Fix up for the make validate target * Now using buildx, caching, and introducing a stage for unit tests and building the integration test binary * Remove --no-cache from unit test step * Cleanup ci.yaml to rely on make package target * Move golangci-lint into Dockerfile * Use uname rather than go-specific command in version script * Use uname for integration test and eliminate separate validate script * Add early stage for getting the webhook binary * Add test-binary Makefile target, move that logic from integration-test-ci * Make ARCH and PLATFORM configurable in Makefile * Organize Dockerfile to pair build and scratch stages. Move TARGETOS and TARGETARCH to the webhook-build stage * Make ARCH/PLATFORM configurable, but default to sensible host-based values * Create separate image Makefile target to quickly produce dev image * Remove unneeded scripts/validate * Rename test-binary to integration-test-binary and cleanup duplicated arch setting from the script * Rename "package" makefile target to the more appropriate "ci" * Update release.yaml to use the new Makefile/Dockerfile * Update publish-head.yaml to use the new Makefile/Dockerfile
1 parent c66eae1 commit cb15ff4

File tree

15 files changed

+187
-197
lines changed

15 files changed

+187
-197
lines changed

.github/workflows/ci.yaml

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,6 @@ jobs:
3535
# https://github.com/actions/checkout/releases/tag/v4.1.1
3636
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
3737

38-
- name: Setup Go
39-
# https://github.com/actions/setup-go/releases/tag/v5.0.0
40-
uses: actions/setup-go@44694675825211faa026b3c33043df3e48a5fa00 # v6.0.0
41-
with:
42-
go-version-file: 'go.mod'
43-
- name: Install golangci-lint
44-
uses: golangci/golangci-lint-action@55c2c1448f86e01eaae002a5a3a9624417608d84 # v6.5.2
45-
with:
46-
version: v1.64.8
47-
4838
# TODO: Pull this next one out once there's a helm-release for rancher 2.9
4939
- name: Checkout rancher/rancher and build the chart
5040
run: |
@@ -56,14 +46,14 @@ jobs:
5646
tar cfz "${{ runner.temp }}/rancher.tgz" -C build/chart/rancher .
5747
popd
5848
49+
- name: Build, Test, and Package
50+
run: make ci
51+
5952
- name: install K3d
6053
run: ./.github/workflows/scripts/install-k3d.sh
6154
env:
6255
K3D_VERSION: latest
6356

64-
- name: ci
65-
run: make ci
66-
6757
- name: setup cluster
6858
run: ./.github/workflows/scripts/setup-cluster.sh
6959
env:

.github/workflows/publish-head.yaml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,14 @@ jobs:
2525
uses: actions/setup-go@44694675825211faa026b3c33043df3e48a5fa00 # v6.0.0
2626
with:
2727
go-version-file: 'go.mod'
28+
- name: Set Version/Commit
29+
run: |
30+
source ./scripts/version
31+
echo "VERSION=${VERSION}" >> $GITHUB_ENV
32+
echo "COMMIT=${COMMIT}" >> $GITHUB_ENV
2833
- name: Build and package
2934
run: |
30-
./scripts/build
35+
make build ARCH=${{ matrix.arch }}
3136
mkdir -p dist/artifacts
3237
cp bin/webhook dist/artifacts/webhook-linux-${{ matrix.arch }}
3338
env:
@@ -80,6 +85,11 @@ jobs:
8085
registry: ${{ env.REGISTRY }}
8186
username: ${{ env.DOCKER_USERNAME }}
8287
password: ${{ env.DOCKER_PASSWORD }}
88+
- name: Set Version/Commit
89+
run: |
90+
source ./scripts/version
91+
echo "VERSION=${VERSION}" >> $GITHUB_ENV
92+
echo "COMMIT=${COMMIT}" >> $GITHUB_ENV
8393
- name: Build and push the webhook image
8494
id: build
8595
uses: docker/build-push-action@263435318d21b8e681c14492fe198d362a7d2c83 # v6.18.0
@@ -88,6 +98,9 @@ jobs:
8898
file: ./package/Dockerfile
8999
platforms: "linux/${{ matrix.arch }}"
90100
outputs: type=image,name=${{ env.REPO }}/rancher-webhook,push-by-digest=true,name-canonical=true,push=true
101+
build-args: |
102+
VERSION=${{ env.VERSION }}
103+
COMMIT=${{ env.COMMIT }}
91104
- name: Export digest
92105
run: |
93106
mkdir -p /tmp/digests

.github/workflows/release.yaml

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,17 @@ jobs:
2626

2727
- name : Checkout repository
2828
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
29-
# https://github.com/actions/checkout/releases/tag/v4.1.1
3029

31-
- name: Setup Go
32-
uses: actions/setup-go@44694675825211faa026b3c33043df3e48a5fa00 # v6.0.0
33-
# https://github.com/actions/setup-go/releases/tag/v5.0.0
34-
with:
35-
go-version-file: 'go.mod'
30+
- name: Build Binary
31+
run: make build ARCH=${{ matrix.arch }}
3632

37-
- name: Build and package
38-
run: |
39-
./scripts/build
40-
mkdir -p dist/artifacts
41-
cp bin/webhook dist/artifacts/webhook-linux-${{ matrix.arch }}
42-
env:
43-
ARCH: "${{ matrix.arch}}"
44-
GOARCH: "${{ matrix.arch}}"
33+
- name: Package Helm Chart
34+
run: make package-helm
4535

46-
- name: Generate checksum files
36+
- name: Prepare Artifacts
4737
run: |
48-
ls -lR dist
38+
mkdir -p dist/artifacts
39+
cp bin/webhook dist/artifacts/webhook-linux-${{ matrix.arch }}
4940
cd dist/artifacts
5041
sha256sum webhook-linux-${{ matrix.arch }} > sha256sum-${{ matrix.arch }}.txt
5142
@@ -122,12 +113,6 @@ jobs:
122113
name: webhook-artifacts-${{ matrix.arch }}
123114
path: dist/artifacts
124115

125-
- name: Move binary to bin/
126-
run: |
127-
mkdir -p bin/
128-
cp -v dist/artifacts/webhook-linux-${{ matrix.arch }} bin/webhook
129-
chmod +x bin/webhook
130-
131116
- name: "Read vault secrets"
132117
uses: rancher-eio/read-vault-secrets@main
133118
with:
@@ -151,6 +136,12 @@ jobs:
151136
username: ${{ env.DOCKER_USERNAME }}
152137
password: ${{ env.DOCKER_PASSWORD }}
153138

139+
- name: Set Version/Commit
140+
run: |
141+
source ./scripts/version
142+
echo "VERSION=${VERSION}" >> $GITHUB_ENV
143+
echo "COMMIT=${COMMIT}" >> $GITHUB_ENV
144+
154145
- name: Build and push the webhook image
155146
id: build
156147
# https://github.com/docker/build-push-action/releases/tag/v6.3.0
@@ -159,6 +150,9 @@ jobs:
159150
context: .
160151
file: ./package/Dockerfile
161152
platforms: "linux/${{ matrix.arch }}"
153+
build-args: |
154+
VERSION=${{ env.VERSION }}
155+
COMMIT=${{ env.COMMIT }}
162156
outputs: type=image,name=${{ env.REPO }}/rancher-webhook,push-by-digest=true,name-canonical=true,push=true
163157

164158
- name: Export digest

.github/workflows/scripts/integration-test-ci

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ set -eu
44
cd $(dirname $0)/../../..
55
DIST_DIR="$PWD"/dist
66

7+
# Build the integration test binary
8+
make integration-test-binary ARCH=${ARCH}
9+
710
source ./scripts/version
811
# Source tags file to get the last built tags
912
source ./dist/tags

Dockerfile.dapper

Lines changed: 0 additions & 29 deletions
This file was deleted.

Makefile

Lines changed: 77 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,82 @@
1-
TARGETS := $(shell ls scripts)
1+
# Set default ARCH, but allow it to be overridden
2+
UNAME_ARCH := $(shell uname -m)
3+
ifeq ($(UNAME_ARCH),x86_64)
4+
ARCH ?= amd64
5+
else ifeq ($(UNAME_ARCH),aarch64)
6+
ARCH ?= arm64
7+
else
8+
ARCH ?= $(UNAME_ARCH)
9+
endif
210

3-
.dapper:
4-
@echo Downloading dapper
5-
@curl -sL https://releases.rancher.com/dapper/latest/dapper-$$(uname -s)-$$(uname -m) > .dapper.tmp
6-
@@chmod +x .dapper.tmp
7-
@./.dapper.tmp -v
8-
@mv .dapper.tmp .dapper
11+
# Export ARCH so it's available to subshells (like the scripts)
12+
export ARCH
13+
PLATFORM ?= linux/$(ARCH)
914

10-
$(TARGETS): .dapper
11-
./.dapper $@
15+
.PHONY: all build integration-test-binary test validate ci package-helm image clean
1216

13-
clean:
14-
rm -rf build bin dist
17+
all: ci
18+
19+
build:
20+
@echo "--- Building Webhook Binary ---"
21+
@bash -c 'source scripts/version && \
22+
mkdir -p bin && \
23+
docker buildx build \
24+
--file package/Dockerfile \
25+
--target binary \
26+
--build-arg VERSION=$${VERSION} \
27+
--build-arg COMMIT=$${COMMIT} \
28+
--platform=$(PLATFORM) \
29+
--output=type=local,dest=./bin \
30+
. '
31+
32+
integration-test-binary:
33+
@echo "--- Building Integration Test Binary ---"
34+
@bash -c 'source scripts/version && \
35+
mkdir -p bin && \
36+
docker buildx build \
37+
--file package/Dockerfile \
38+
--target integration-test-binary \
39+
--platform=$(PLATFORM) \
40+
--output=type=local,dest=./bin \
41+
. '
42+
43+
test:
44+
@echo "--- Running Unit Tests ---"
45+
@docker buildx build \
46+
--file package/Dockerfile \
47+
--target test \
48+
--progress=plain \
49+
.
1550

16-
.DEFAULT_GOAL := default
51+
validate:
52+
@echo "--- Validating ---"
53+
@docker buildx build \
54+
--file package/Dockerfile \
55+
--target validate \
56+
--progress=plain \
57+
.
1758

18-
.PHONY: $(TARGETS)
59+
package-helm:
60+
./scripts/package-helm
61+
62+
image: build
63+
@echo "--- Building Development Image ---"
64+
@bash -c 'source scripts/version && \
65+
docker buildx build \
66+
--file package/Dockerfile \
67+
--build-arg VERSION=$${VERSION} \
68+
--build-arg COMMIT=$${COMMIT} \
69+
--platform=$(PLATFORM) \
70+
-t rancher/webhook:$${TAG} \
71+
--load \
72+
. && \
73+
mkdir -p dist && \
74+
chmod a+rwx dist && \
75+
docker save -o dist/rancher-webhook-image.tar rancher/webhook:$${TAG} && \
76+
echo IMAGE_TAG=$${TAG} > dist/image_tag'
77+
78+
# ci target is for CI, ensuring tests and validation run first
79+
ci: test validate image package-helm
80+
81+
clean:
82+
rm -rf bin dist

package/Dockerfile

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,74 @@
1+
# ===============
2+
# Build Stage
3+
# ===============
4+
FROM --platform=$BUILDPLATFORM registry.suse.com/bci/golang:1.24 AS build
5+
6+
# Set up build cache
7+
ENV GOMODCACHE=/root/.cache/go/modcache
8+
ENV GOCACHE=/root/.cache/go/cache
9+
ENV CGO_ENABLED=0
10+
11+
WORKDIR /src
12+
13+
# Define build arguments
14+
ARG VERSION
15+
ARG COMMIT
16+
ARG LINKFLAGS="-extldflags -static -s"
17+
ARG LDFLAGS="-X main.Version=${VERSION} -X main.GitCommit=${COMMIT} ${LINKFLAGS}"
18+
19+
# Download dependencies
20+
COPY go.mod go.sum ./
21+
RUN --mount=type=cache,target=/root/.cache,id=rancher go mod download
22+
23+
# ===============
24+
# Webhook Binary Build Stage
25+
# ===============
26+
FROM --platform=$BUILDPLATFORM build AS webhook-build
27+
ARG TARGETOS
28+
ARG TARGETARCH
29+
# Copy source and build the webhook binary
30+
COPY pkg/ pkg/
31+
COPY main.go ./
32+
RUN --mount=type=cache,target=/root/.cache,id=rancher GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -ldflags "${LDFLAGS}" -o /dist/webhook .
33+
34+
# ===============
35+
# Binary Stage (for extracting the main binary)
36+
# ===============
37+
FROM scratch AS binary
38+
COPY --from=webhook-build /dist/webhook /webhook
39+
40+
# ===============
41+
# Test Stage
42+
# ===============
43+
FROM build AS test
44+
COPY . .
45+
RUN --mount=type=cache,target=/root/.cache,id=rancher go test --coverpkg=./pkg/... -coverprofile=coverage.out --count=1 ./pkg/...
46+
RUN cat coverage.out | awk 'BEGIN {cov=0; stat=0;} $3!="" { cov+=($3==1?$2:0); stat+=$2; } END {printf("Total coverage: %.2f%% of statements\n", (cov/stat)*100);}'
47+
48+
# ===============
49+
# Validate Stage
50+
# ===============
51+
FROM build AS validate
52+
RUN curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b /usr/local/bin v1.64.8
53+
COPY . .
54+
RUN --mount=type=cache,target=/root/.cache,id=rancher golangci-lint run
55+
56+
# ===============
57+
# Integration Test Build Stage
58+
# ===============
59+
FROM build AS integration-test-build
60+
COPY . .
61+
RUN --mount=type=cache,target=/root/.cache,id=rancher go test ./tests/integration/... -c -o /dist/rancher-webhook-integration.test
62+
63+
# ===============
64+
# Test Binary Stage to extract the integration test binary
65+
# ===============
66+
FROM scratch AS integration-test-binary
67+
COPY --from=integration-test-build /dist/rancher-webhook-integration.test /rancher-webhook-integration.test
68+
69+
# ===============
70+
# Final Stage
71+
# ===============
172
FROM registry.suse.com/bci/bci-micro:15.7
273

374
ARG user=webhook
@@ -7,7 +78,8 @@ RUN echo "$user:x:1000:1000::/home/$user:/bin/bash" >> /etc/passwd && \
778
mkdir /home/$user && \
879
chown -R $user:$user /home/$user
980

10-
COPY bin/webhook /usr/bin/
81+
# Copy the compiled binary from the webhook-build stage
82+
COPY --from=webhook-build /dist/webhook /usr/bin/
1183

1284
USER $user
1385

scripts/build

Lines changed: 0 additions & 18 deletions
This file was deleted.

scripts/ci

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)