Skip to content

Commit cf76e2c

Browse files
authored
Update binary and image build process (#490)
## Description Binary and container image builds are only run on a merge into main. We should be testing this on all PRs. Also, the binary and image builds on main take almost 20mins to run. This PR is to speed up binary and image builds and to have them run on all PRs. Fixes: # ## How Has This Been Tested? ## How are existing users impacted? What migration steps/scripts do we need? ## Checklist: I have: - [ ] updated the documentation and/or roadmap (if required) - [ ] added unit or e2e tests - [ ] provided instructions on how to upgrade
2 parents 0d152e0 + cfb1867 commit cf76e2c

10 files changed

+150
-274
lines changed

.github/workflows/ci.yaml

Lines changed: 11 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ on:
77

88
env:
99
CGO_ENABLED: 0
10-
GO_VERSION: '1.24'
10+
GO_VERSION: '1.25'
1111
REGISTRY: ghcr.io
1212
# github.repository as <account>/<repo>
1313
IMAGE_NAME: ${{ github.repository }}
@@ -51,49 +51,19 @@ jobs:
5151
with:
5252
check_filenames: true
5353
check_hidden: true
54-
skip: "./.git,./go.mod,./go.sum"
54+
skip: "./.git,./go.mod,./go.sum,.goreleaser.yaml"
5555
ignore_words_list: ro,NotIn
5656

57-
build-and-push-image:
58-
name: Build and push manager image
57+
build:
58+
name: Build binaries and container images
5959
runs-on: ubuntu-latest
60-
permissions:
61-
contents: read
62-
packages: write
63-
6460
steps:
65-
- name: Checkout
66-
uses: actions/checkout@v5
67-
68-
- name: Login to ghcr.io
69-
if: github.event_name != 'pull_request'
70-
uses: docker/login-action@v3
71-
with:
72-
registry: ${{ env.REGISTRY }}
73-
username: ${{ github.actor }}
74-
password: ${{ secrets.GITHUB_TOKEN }}
75-
76-
- name: Extract metadata (tags, labels) for Docker
77-
id: meta
78-
uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7
79-
with:
80-
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
81-
tags: |
82-
type=sha
83-
type=raw,value=latest
84-
85-
- name: Set up QEMU
86-
uses: docker/setup-qemu-action@v3
61+
- uses: actions/checkout@v5
8762

88-
- name: Set up Docker Buildx
89-
uses: docker/setup-buildx-action@v3
63+
- uses: actions/setup-go@v6
64+
with:
65+
go-version: "${{ env.GO_VERSION }}"
66+
cache: true
9067

91-
- name: Build and push Docker image
92-
id: build-and-push
93-
uses: docker/build-push-action@v6
94-
with:
95-
context: .
96-
push: ${{ github.event_name != 'pull_request' }}
97-
tags: ${{ steps.meta.outputs.tags }}
98-
labels: ${{ steps.meta.outputs.labels }}
99-
platforms: ${{ github.event_name != 'pull_request' && 'linux/amd64,linux/arm64' || 'linux/amd64' }}
68+
- name: Build
69+
run: make build-image

.github/workflows/release.yaml

Lines changed: 18 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
1-
# workflow to release assets as part of the release
2-
name: Upload Release Asset
1+
name: Release Workflow
32
on:
43
push:
54
tags:
65
- "v*"
76
env:
7+
CGO_ENABLED: 0
8+
GO_VERSION: '1.25'
89
REGISTRY: ghcr.io
10+
# github.repository as <account>/<repo>
911
IMAGE_NAME: ${{ github.repository }}
10-
GO_VERSION: '1.24'
12+
13+
permissions:
14+
contents: write
1115

1216
jobs:
13-
manager-image:
14-
name: Build and push manager image
17+
build-image:
18+
name: Build and push image
1519
runs-on: ubuntu-latest
1620
steps:
1721
- name: Checkout code
@@ -22,92 +26,27 @@ jobs:
2226
go-version: "${{ env.GO_VERSION }}"
2327
cache: true
2428

25-
- name: Set up QEMU
26-
uses: docker/setup-qemu-action@v3
27-
28-
- name: Set up Docker Buildx
29-
uses: docker/setup-buildx-action@v3
30-
31-
- name: Cache Docker layers
32-
uses: actions/cache@v4
33-
with:
34-
path: /tmp/.buildx-cache
35-
key: ${{ runner.os }}-buildx-manager-${{ github.sha }}
36-
restore-keys: |
37-
${{ runner.os }}-buildx-manager-
38-
${{ runner.os }}-buildx-
39-
40-
- name: Docker manager metadata
41-
id: meta
42-
uses: docker/metadata-action@v5
43-
with:
44-
github-token: ${{ secrets.GITHUB_TOKEN }}
45-
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
46-
flavor: latest=false
47-
tags: type=ref,event=tag
48-
49-
- name: Log in to the Container registry
50-
uses: docker/login-action@v3
51-
with:
52-
registry: ${{ env.REGISTRY }}
53-
username: ${{ github.actor }}
54-
password: ${{ secrets.GITHUB_TOKEN }}
55-
56-
- name: Build and push manager image
57-
uses: docker/build-push-action@v6
58-
with:
59-
context: .
60-
push: true
61-
build-args: |
62-
LDFLAGS=${{ env.DOCKER_BUILD_LDFLAGS }}
63-
tags: ${{ steps.meta.outputs.tags }}
64-
labels: ${{ steps.meta.outputs.labels }}
65-
platforms: linux/amd64,linux/arm64
66-
cache-from: type=local,src=/tmp/.buildx-cache
67-
cache-to: type=local,dest=/tmp/.buildx-cache-new,mode=max
68-
69-
- name: Move cache
70-
# Temp fix
71-
# https://github.com/docker/build-push-action/issues/252
72-
# https://github.com/moby/buildkit/issues/1896
73-
run: |
74-
rm -rf /tmp/.buildx-cache
75-
mv /tmp/.buildx-cache-new /tmp/.buildx-cache
29+
- name: Build and push image
30+
run: make image-build-push
31+
env:
32+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
7633

7734
release:
7835
name: Release
7936
runs-on: ubuntu-latest
8037
needs:
81-
- manager-image
38+
- build-image
8239
steps:
83-
- uses: actions/checkout@v5
40+
- name: Checkout code
41+
uses: actions/checkout@v5
8442

8543
- uses: actions/setup-go@v6
8644
with:
8745
go-version: "${{ env.GO_VERSION }}"
8846
cache: true
8947

90-
- uses: actions/cache@v4
91-
with:
92-
path: bin
93-
key: ${{ runner.os }}-tools-bin-release-${{ hashFiles('Makefile') }}
94-
restore-keys: |
95-
${{ runner.os }}-tools-bin-release-
96-
${{ runner.os }}-tools-bin-
97-
98-
- name: Docker manager metadata
99-
id: meta
100-
uses: docker/metadata-action@v5
101-
with:
102-
github-token: ${{ secrets.GITHUB_TOKEN }}
103-
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
104-
flavor: latest=false
105-
tags: type=ref,event=tag
106-
107-
- name: manifest
48+
- name: Create manifests
10849
run: make release
109-
env:
110-
TAG: ${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.version'] }}
11150

11251
- name: Generate Release Notes
11352
run: |
@@ -120,7 +59,7 @@ jobs:
12059
OWNER: ${{ github.repository_owner }}
12160
REPO: ${{ github.event.repository.name }}
12261

123-
- name: Create Release
62+
- name: Create GitHub Release
12463
id: create_release
12564
uses: softprops/action-gh-release@v2
12665
env:

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,5 @@ dist
4343

4444
# development / test / lints
4545
bin/
46+
# Added by goreleaser init:
47+
dist/

.goreleaser.yaml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
version: 2
2+
3+
env:
4+
- REGISTRY={{ if index .Env "REGISTRY" }}{{ .Env.REGISTRY }}{{ else }}ghcr.io{{ end }}
5+
- IMAGE_NAME={{ if index .Env "IMAGE_NAME" }}{{ .Env.IMAGE_NAME }}{{ else }}tinkerbell/cluster-api-provider-tinkerbell{{ end }}
6+
- BINARY=capt
7+
8+
before:
9+
hooks:
10+
- go mod tidy
11+
12+
builds:
13+
- id: build
14+
binary: "{{ .Env.BINARY }}"
15+
env:
16+
- CGO_ENABLED=0
17+
goos:
18+
- linux
19+
ldflags:
20+
- -s -w
21+
goarch:
22+
- amd64
23+
- arm64
24+
25+
checksum:
26+
name_template: 'checksums.txt'
27+
28+
archives:
29+
- formats:
30+
- binary
31+
32+
release:
33+
disable: true
34+
35+
dockers_v2:
36+
- images:
37+
- "{{ .Env.REGISTRY }}/{{ .Env.IMAGE_NAME }}"
38+
tags:
39+
- "v{{ .Version }}"
40+
- "{{ if not .IsNightly }}latest{{ end }}"
41+
labels:
42+
"org.opencontainers.image.created": "{{.Date}}"
43+
"org.opencontainers.image.name": "{{.ProjectName}}"
44+
"org.opencontainers.image.revision": "{{.FullCommit}}"
45+
"org.opencontainers.image.version": "v{{.Version}}"
46+
"org.opencontainers.image.source": "{{.GitURL}}"

Dockerfile

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,8 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
# Build the manager binary
16-
ARG GOVER=1.24
17-
FROM golang:${GOVER} AS builder
18-
19-
WORKDIR /workspace
20-
21-
# Run this with docker build --build_arg $(go env GOPROXY) to override the goproxy
22-
ARG goproxy=https://proxy.golang.org
23-
ENV GOPROXY=$goproxy
24-
25-
# Copy the Go Modules manifests
26-
COPY go.mod go.mod
27-
COPY go.sum go.sum
28-
# cache deps before building and copying source so that we don't need to re-download as much
29-
# and so that source changes don't invalidate our downloaded layer
30-
RUN go mod download
31-
32-
# Copy the sources
33-
COPY ./ ./
34-
35-
# Build
36-
ARG ARCH
37-
ARG LDFLAGS
38-
RUN CGO_ENABLED=0 GOOS=linux GOARCH=${ARCH} go build -a -ldflags="${LDFLAGS}" -o manager .
39-
40-
# Use distroless as minimal base image to package the manager binary
41-
# Refer to https://github.com/GoogleContainerTools/distroless for more details
4215
FROM gcr.io/distroless/static:nonroot
43-
WORKDIR /
44-
COPY --from=builder /workspace/manager .
16+
ARG TARGETPLATFORM
17+
COPY ${TARGETPLATFORM}/capt /capt
4518
USER nonroot:nonroot
46-
ENTRYPOINT ["/manager"]
19+
ENTRYPOINT ["/capt"]

0 commit comments

Comments
 (0)