Skip to content

Commit 1df9aff

Browse files
ekarlsoekarlso
and
ekarlso
authored
Cleanup build and release issues (#43)
* Add release notes generator used on other CAPI projects * Fix release notes and workflow * Pin Go version in Dockerfile * Fix workflow go version(s) Co-authored-by: ekarlso <[email protected]>
1 parent 1cde851 commit 1df9aff

File tree

6 files changed

+615
-60
lines changed

6 files changed

+615
-60
lines changed

.github/workflows/pr.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ on:
66

77
env:
88
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
9+
GO_VERSION: "1.20"
910

1011
jobs:
1112
validate:
@@ -25,6 +26,8 @@ jobs:
2526
- uses: actions/setup-go@v4
2627
with:
2728
go-version-file: "./go.mod"
29+
go-version: "${{ env.GO_VERSION }}"
30+
cache: true
2831

2932
- name: ${{ matrix.target }}
3033
run: make ${{ matrix.target }}

.github/workflows/release.yaml

+25-22
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,19 @@ on:
77
env:
88
REGISTRY: ghcr.io
99
IMAGE_NAME: ${{ github.repository }}
10-
GO_VERSION: '1.20'
10+
GO_VERSION: "1.20"
11+
12+
permissions:
13+
packages: write
14+
contents: write
1115

1216
jobs:
1317
image:
1418
name: Build and push ${{ matrix.target }} image
1519
runs-on: ubuntu-latest
1620
strategy:
1721
matrix:
18-
target: [bootsrap, controlplane]
22+
target: [bootstrap, controlplane]
1923

2024
steps:
2125
- name: Checkout code
@@ -46,7 +50,7 @@ jobs:
4650
uses: docker/metadata-action@v4
4751
with:
4852
github-token: ${{ secrets.GITHUB_TOKEN }}
49-
images: ${{ env.REGISTRY }}/${{ matrix.target }}-${{ env.IMAGE_NAME }}
53+
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}-${{ matrix.target }}
5054
flavor: latest=false
5155
tags: type=ref,event=tag
5256

@@ -71,6 +75,7 @@ jobs:
7175
push: true
7276
build-args: |
7377
LDFLAGS=${{ env.DOCKER_BUILD_LDFLAGS }}
78+
package=./${{ matrix.target }}/main.go
7479
tags: ${{ steps.meta.outputs.tags }}
7580
labels: ${{ steps.meta.outputs.labels }}
7681
platforms: linux/amd64,linux/arm64
@@ -93,7 +98,13 @@ jobs:
9398
steps:
9499
- uses: actions/checkout@v3
95100

96-
- uses: ./.github/actions/setup-go
101+
- name: Set release
102+
run: echo "RELEASE_TAG=${GITHUB_REF:10}" >> $GITHUB_ENV
103+
104+
- uses: actions/setup-go@v4
105+
with:
106+
go-version: "${{ env.GO_VERSION }}"
107+
cache: true
97108

98109
- uses: actions/cache@v3
99110
with:
@@ -108,33 +119,25 @@ jobs:
108119
uses: docker/metadata-action@v4
109120
with:
110121
github-token: ${{ secrets.GITHUB_TOKEN }}
111-
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
122+
images: |
123+
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}-controlplane
124+
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}-bootstrap
112125
flavor: latest=false
113126
tags: type=ref,event=tag
114127

115128
- name: manifest
116-
run: make release-${{ matrix.target }}
117-
env:
118-
TAG: ${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.version'] }}
129+
run: make release
119130

120-
- name: Generate Release Notes
121-
run: |
122-
release_notes=$(gh api repos/{owner}/{repo}/releases/generate-notes -F tag_name=${{ github.ref }} --jq .body)
123-
echo 'RELEASE_NOTES<<EOF' >> $GITHUB_ENV
124-
echo "${release_notes}" >> $GITHUB_ENV
125-
echo 'EOF' >> $GITHUB_ENV
126-
env:
127-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
128-
OWNER: ${{ github.repository_owner }}
129-
REPO: ${{ github.event.repository.name }}
131+
- name: manifest
132+
run: make release-notes
130133

131134
- name: Create Release
132135
id: create_release
133136
uses: softprops/action-gh-release@v1
134137
env:
135138
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
136139
with:
137-
files: out/release/*
138-
body: ${{ env.RELEASE_NOTES }}
139-
draft: false
140-
prerelease: false
140+
files: out/*
141+
body_path: _releasenotes/${{ env.RELEASE_TAG }}.md
142+
draft: true
143+
prerelease: false

.gitignore

+2-5
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@
1111
# Output of the go coverage tool, specifically when used with LiteIDE
1212
*.out
1313

14-
# Dependency directories (remove the comment below to include it)
15-
# vendor/
16-
17-
1814
bin/
19-
out/
15+
out/
16+
_releasenotes

Dockerfile

+24-24
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
1-
# Build the manager binary
2-
# Run this with docker build --build-arg builder_image=<golang:x.y.z>
3-
ARG builder_image
1+
# Copyright 2019 The Kubernetes Authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
414

5-
# Build architecture
6-
ARG ARCH
15+
# Build the manager binary
16+
FROM --platform=${BUILDPLATFORM} docker.io/library/golang:1.20.7 as build
17+
ARG TARGETOS TARGETARCH
18+
ARG package
719

8-
# Ignore Hadolint rule "Always tag the version of an image explicitly."
9-
# It's an invalid finding since the image is explicitly set in the Makefile.
10-
# https://github.com/hadolint/hadolint/wiki/DL3006
11-
# hadolint ignore=DL3006
12-
FROM ${builder_image} as builder
1320
WORKDIR /workspace
1421

1522
# Run this with docker build --build-arg goproxy=$(go env GOPROXY) to override the goproxy
@@ -34,22 +41,15 @@ RUN --mount=type=cache,target=/root/.cache/go-build \
3441
--mount=type=cache,target=/go/pkg/mod \
3542
go build ./bootstrap/main.go
3643

37-
# Build
38-
ARG package=.
39-
ARG ARCH
40-
ARG ldflags
41-
42-
# Do not force rebuild of up-to-date packages (do not use -a) and use the compiler cache folder
43-
RUN --mount=type=cache,target=/root/.cache/go-build \
44-
--mount=type=cache,target=/go/pkg/mod \
45-
CGO_ENABLED=0 GOOS=linux GOARCH=${ARCH} \
46-
go build -trimpath -ldflags "${ldflags} -extldflags '-static'" \
44+
RUN --mount=type=cache,target=/root/.cache \
45+
--mount=type=cache,target=/go/pkg \
46+
GOOS=${TARGETOS} GOARCH=${TARGETARCH} CGO_ENABLED=0 \
47+
go build -ldflags "${LDFLAGS} -extldflags '-static'" \
4748
-o manager ${package}
4849

49-
# Production image
50-
FROM gcr.io/distroless/static:nonroot-${ARCH}
50+
FROM --platform=${BUILDPLATFORM} gcr.io/distroless/static:nonroot
5151
WORKDIR /
52-
COPY --from=builder /workspace/manager .
52+
COPY --from=build /workspace/manager .
5353
# Use uid of nonroot user (65532) because kubernetes expects numeric user when applying pod security policies
5454
USER 65532
55-
ENTRYPOINT ["/manager"]
55+
ENTRYPOINT ["/manager"]

Makefile

+42-9
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ SHELL:=/usr/bin/env bash
2020

2121
.DEFAULT_GOAL:=help
2222

23-
GO_VERSION ?= 1.20.6
23+
GO_VERSION ?= 1.20.7
2424
GO_CONTAINER_IMAGE ?= docker.io/library/golang:$(GO_VERSION)
2525

2626
ARCH ?= $(shell go env GOARCH)
@@ -90,6 +90,32 @@ KUSTOMIZE_VER := v4.0.4
9090
KUSTOMIZE_BIN := kustomize
9191
KUSTOMIZE := $(TOOLS_BIN_DIR)/$(KUSTOMIZE_BIN)-$(KUSTOMIZE_VER)
9292

93+
## --------------------------------------
94+
## Release
95+
## --------------------------------------
96+
97+
##@ release:
98+
99+
## latest git tag for the commit, e.g., v0.3.10
100+
RELEASE_TAG ?= $(shell git describe --abbrev=0 2>/dev/null)
101+
ifneq (,$(findstring -,$(RELEASE_TAG)))
102+
PRE_RELEASE=true
103+
endif
104+
# the previous release tag, e.g., v0.3.9, excluding pre-release tags
105+
PREVIOUS_TAG ?= $(shell git tag -l | grep -E "^v[0-9]+\.[0-9]+\.[0-9]+$$" | sort -V | grep -B1 $(RELEASE_TAG) | head -n 1 2>/dev/null)
106+
## set by Prow, ref name of the base branch, e.g., main
107+
RELEASE_ALIAS_TAG := $(PULL_BASE_REF)
108+
RELEASE_DIR := out
109+
RELEASE_NOTES_DIR := _releasenotes
110+
111+
.PHONY: $(RELEASE_DIR)
112+
$(RELEASE_DIR):
113+
mkdir -p $(RELEASE_DIR)/
114+
115+
.PHONY: $(RELEASE_NOTES_DIR)
116+
$(RELEASE_NOTES_DIR):
117+
mkdir -p $(RELEASE_NOTES_DIR)/
118+
93119

94120
all-bootstrap: manager-bootstrap
95121

@@ -122,18 +148,17 @@ deploy-bootstrap: manifests-bootstrap
122148
manifests-bootstrap: $(KUSTOMIZE) $(CONTROLLER_GEN)
123149
$(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=bootstrap/config/crd/bases output:rbac:dir=bootstrap/config/rbac
124150

125-
release-bootstrap: manifests-bootstrap ## Release bootstrap
126-
mkdir -p out
151+
release-bootstrap:$(RELEASE_DIR) manifests-bootstrap ## Release bootstrap
127152
cd bootstrap/config/manager && $(KUSTOMIZE) edit set image controller=${BOOTSTRAP_IMG}
128-
$(KUSTOMIZE) build bootstrap/config/default > out/bootstrap-components.yaml
153+
$(KUSTOMIZE) build bootstrap/config/default > $(RELEASE_DIR)/bootstrap-components.yaml
129154

130155
# Generate code
131156
generate-bootstrap: $(CONTROLLER_GEN)
132157
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="$(shell pwd)/bootstrap/..."
133158

134159
# Build the docker image
135160
docker-build-bootstrap: manager-bootstrap ## Build bootstrap
136-
DOCKER_BUILDKIT=1 docker build --build-arg builder_image=$(GO_CONTAINER_IMAGE) --build-arg goproxy=$(GOPROXY) --build-arg ARCH=$(ARCH) --build-arg package=./bootstrap/main.go --build-arg ldflags="$(LDFLAGS)" . -t ${BOOTSTRAP_IMG}
161+
DOCKER_BUILDKIT=1 docker build --build-arg builder_image=$(GO_CONTAINER_IMAGE) --build-arg goproxy=$(GOPROXY) --build-arg TARGETARCH=$(ARCH) --build-arg package=./bootstrap/main.go --build-arg ldflags="$(LDFLAGS)" . -t ${BOOTSTRAP_IMG}
137162

138163
# Push the docker image
139164
docker-push-bootstrap: ## Push bootstrap
@@ -170,21 +195,29 @@ deploy-controlplane: manifests-controlplane
170195
manifests-controlplane: $(KUSTOMIZE) $(CONTROLLER_GEN)
171196
$(CONTROLLER_GEN) rbac:roleName=manager-role webhook crd paths="./..." output:crd:artifacts:config=controlplane/config/crd/bases output:rbac:dir=controlplane/config/rbac
172197

173-
release-controlplane: manifests-controlplane ## Release control-plane
174-
mkdir -p out
198+
release-controlplane: $(RELEASE_DIR) manifests-controlplane ## Release control-plane
175199
cd controlplane/config/manager && $(KUSTOMIZE) edit set image controller=${CONTROLPLANE_IMG}
176-
$(KUSTOMIZE) build controlplane/config/default > out/control-plane-components.yaml
200+
$(KUSTOMIZE) build controlplane/config/default > $(RELEASE_DIR)/control-plane-components.yaml
177201

178202
generate-controlplane: $(CONTROLLER_GEN)
179203
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="$(shell pwd)/controlplane/..."
180204

181205
docker-build-controlplane: manager-controlplane ## Build control-plane
182-
DOCKER_BUILDKIT=1 docker build --build-arg builder_image=$(GO_CONTAINER_IMAGE) --build-arg goproxy=$(GOPROXY) --build-arg ARCH=$(ARCH) --build-arg package=./controlplane/main.go --build-arg ldflags="$(LDFLAGS)" . -t ${CONTROLPLANE_IMG}
206+
DOCKER_BUILDKIT=1 docker build --build-arg builder_image=$(GO_CONTAINER_IMAGE) --build-arg goproxy=$(GOPROXY) --build-arg TARGETARCH=$(ARCH) --build-arg package=./controlplane/main.go --build-arg ldflags="$(LDFLAGS)" . -t ${CONTROLPLANE_IMG}
183207

184208
docker-push-controlplane: ## Push control-plane
185209
docker push ${CONTROLPLANE_IMG}
186210

187211
release: release-bootstrap release-controlplane
212+
213+
.PHONY: release-notes
214+
release-notes: $(RELEASE_NOTES_DIR) $(RELEASE_NOTES)
215+
if [ -n "${PRE_RELEASE}" ]; then \
216+
echo ":rotating_light: This is a RELEASE CANDIDATE. Use it only for testing purposes. If you find any bugs, file an [issue](https://github.com/kubernetes-sigs/cluster-api/issues/new)." > $(RELEASE_NOTES_DIR)/$(RELEASE_TAG).md; \
217+
else \
218+
go run ./hack/tools/release/notes.go --from=$(PREVIOUS_TAG) > $(RELEASE_NOTES_DIR)/$(RELEASE_TAG).md; \
219+
fi
220+
188221
## --------------------------------------
189222
## Help
190223
## --------------------------------------

0 commit comments

Comments
 (0)