Skip to content

Commit 4926abf

Browse files
authored
Automatically set image versions when releasing (#303)
* override versions * Fix path to vars in ldflags * Add default tags to Makefile + fix image name in kustomize step * Set up the docker build to inject the container versions to use * Fix build workflow to verify fetched image tags and improve error handling * Just use the docker build workflow to test docker builds, rather than a separate step in test
1 parent 26ad88e commit 4926abf

File tree

7 files changed

+72
-20
lines changed

7 files changed

+72
-20
lines changed

.github/workflows/build.yml

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ on:
66
- main
77
tags:
88
- '**'
9+
pull_request:
910
workflow_dispatch:
1011
schedule:
1112
- cron: '0 0 * * 0'
@@ -17,4 +18,41 @@ permissions:
1718

1819
jobs:
1920
package:
20-
uses: Chia-Network/actions/.github/workflows/docker-build.yaml@main
21+
runs-on: ubuntu-latest
22+
timeout-minutes: 360
23+
steps:
24+
- uses: actions/checkout@v5
25+
26+
- name: Set Env
27+
uses: Chia-Network/actions/setjobenv@main
28+
env:
29+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
30+
31+
- name: Set dependency versions
32+
run: |
33+
set -e
34+
CHIA_IMAGE_TAG=$(curl -fsSL https://latest.cmm.io/chia)
35+
EXPORTER_IMAGE_TAG=$(curl -fsSL https://latest.cmm.io/chia-exporter)
36+
HEALTHCHECK_IMAGE_TAG=$(curl -fsSL https://latest.cmm.io/chia-healthcheck)
37+
38+
# Verify all variables are set
39+
if [ -z "$CHIA_IMAGE_TAG" ] || [ -z "$EXPORTER_IMAGE_TAG" ] || [ -z "$HEALTHCHECK_IMAGE_TAG" ]; then
40+
echo "Error: Failed to fetch one or more version tags"
41+
echo "CHIA_IMAGE_TAG=$CHIA_IMAGE_TAG"
42+
echo "EXPORTER_IMAGE_TAG=$EXPORTER_IMAGE_TAG"
43+
echo "HEALTHCHECK_IMAGE_TAG=$HEALTHCHECK_IMAGE_TAG"
44+
exit 1
45+
fi
46+
47+
echo "CHIA_IMAGE_TAG=$CHIA_IMAGE_TAG" >> $GITHUB_ENV
48+
echo "EXPORTER_IMAGE_TAG=$EXPORTER_IMAGE_TAG" >> $GITHUB_ENV
49+
echo "HEALTHCHECK_IMAGE_TAG=$HEALTHCHECK_IMAGE_TAG" >> $GITHUB_ENV
50+
51+
- name: Build Container
52+
uses: Chia-Network/actions/docker/build@main
53+
with:
54+
push: ${{ github.event_name != 'pull_request' }}
55+
build-args: |
56+
"CHIA_IMAGE_TAG=${{ env.CHIA_IMAGE_TAG }}"
57+
"EXPORTER_IMAGE_TAG=${{ env.EXPORTER_IMAGE_TAG }}"
58+
"HEALTHCHECK_IMAGE_TAG=${{ env.HEALTHCHECK_IMAGE_TAG }}"

.github/workflows/release.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ jobs:
2020
env:
2121
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2222

23+
- name: Set dependency versions
24+
run: |
25+
echo "CHIA_OPERATOR_VERSION=$RELEASE_TAG" >> $GITHUB_ENV
26+
2327
- name: Install kubectl
2428
run: |
2529
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"

.github/workflows/test.yml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,6 @@ jobs:
2929
run: |
3030
make test
3131
32-
docker-build:
33-
uses: Chia-Network/actions/.github/workflows/docker-build.yaml@main
34-
with:
35-
push: false
36-
3732
test-controller-gen-ran:
3833
runs-on: "ubuntu-latest"
3934
timeout-minutes: 10

Dockerfile

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
# Build the manager binary
22
FROM golang:1 AS builder
3-
ARG TARGETOS
4-
ARG TARGETARCH
3+
4+
ARG CHIA_IMAGE_TAG=latest
5+
ARG EXPORTER_IMAGE_TAG=latest
6+
ARG HEALTHCHECK_IMAGE_TAG=latest
7+
8+
ENV CHIA_IMAGE_TAG=${CHIA_IMAGE_TAG}
9+
ENV EXPORTER_IMAGE_TAG=${EXPORTER_IMAGE_TAG}
10+
ENV HEALTHCHECK_IMAGE_TAG=${HEALTHCHECK_IMAGE_TAG}
511

612
WORKDIR /workspace
713
# Copy the Go Modules manifests
@@ -12,22 +18,19 @@ COPY go.sum go.sum
1218
RUN go mod download
1319

1420
# Copy the go source
21+
COPY Makefile Makefile
1522
COPY cmd/main.go cmd/main.go
1623
COPY api/ api/
1724
COPY internal/ internal/
25+
COPY hack/ hack/
1826

19-
# Build
20-
# the GOARCH has not a default value to allow the binary be built according to the host where the command
21-
# was called. For example, if we call make docker-build in a local env which has the Apple Silicon M1 SO
22-
# the docker BUILDPLATFORM arg will be linux/arm64 when for Apple x86 it will be linux/amd64. Therefore,
23-
# by leaving it empty we can ensure that the container and binary shipped on it will have the same platform.
24-
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o manager cmd/main.go
27+
RUN make build
2528

2629
# Use distroless as minimal base image to package the manager binary
2730
# Refer to https://github.com/GoogleContainerTools/distroless for more details
2831
FROM gcr.io/distroless/static:nonroot
2932
WORKDIR /
30-
COPY --from=builder /workspace/manager .
33+
COPY --from=builder /workspace/bin/manager .
3134
USER 65532:65532
3235

3336
ENTRYPOINT ["/manager"]

Makefile

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,17 @@ IMG ?= controller:latest
33
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
44
ENVTEST_K8S_VERSION = 1.30.0
55

6+
# Optional version overrides for ldflags and yaml manifests
7+
CHIA_IMAGE_TAG ?= latest
8+
EXPORTER_IMAGE_TAG ?= latest
9+
HEALTHCHECK_IMAGE_TAG ?= latest
10+
CHIA_OPERATOR_VERSION ?= latest
11+
12+
LD_FLAGS := \
13+
-X 'github.com/chia-network/chia-operator/internal/controller/common/consts.DefaultChiaImageTag=$(CHIA_IMAGE_TAG)' \
14+
-X 'github.com/chia-network/chia-operator/internal/controller/common/consts.DefaultChiaExporterImageTag=$(EXPORTER_IMAGE_TAG)' \
15+
-X 'github.com/chia-network/chia-operator/internal/controller/common/consts.DefaultChiaHealthcheckImageTag=$(HEALTHCHECK_IMAGE_TAG)'
16+
617
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
718
ifeq (,$(shell go env GOBIN))
819
GOBIN=$(shell go env GOPATH)/bin
@@ -80,15 +91,16 @@ lint-fix: golangci-lint ## Run golangci-lint linter and perform fixes
8091

8192
.PHONY: build
8293
build: manifests generate fmt vet ## Build manager binary.
83-
go build -o bin/manager cmd/main.go
94+
CGO_ENABLED=0 go build -ldflags="$(LD_FLAGS)" -o bin/manager cmd/main.go
8495

8596
.PHONY: run
8697
run: manifests generate fmt vet ## Run a controller from your host.
87-
go run ./cmd/main.go
98+
go run -ldflags="$(LD_FLAGS)" ./cmd/main.go
8899

89100
.PHONY: release
90101
release: manifests kustomize ## Build CRD and Operator manifests with kustomize.
91102
mkdir -p release/
103+
cd config/manager && $(KUSTOMIZE) edit set image ghcr.io/chia-network/chia-operator=ghcr.io/chia-network/chia-operator:$(CHIA_OPERATOR_VERSION)
92104
$(KUSTOMIZE) build config/crd > release/crd.yaml
93105
$(KUSTOMIZE) build config/default > release/manager.yaml
94106
$(KUSTOMIZE) build config/prometheus > release/monitor.yaml

config/manager/kustomization.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ resources:
33
apiVersion: kustomize.config.k8s.io/v1beta1
44
kind: Kustomization
55
images:
6-
- name: controller
7-
newName: controller
6+
- name: ghcr.io/chia-network/chia-operator
7+
newName: ghcr.io/chia-network/chia-operator
88
newTag: latest

internal/controller/common/consts/consts.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const (
3737
)
3838

3939
// API default image constants
40-
const (
40+
var (
4141
// DefaultChiaImageName contains the default image name for the chia-docker image
4242
DefaultChiaImageName = "ghcr.io/chia-network/chia"
4343

0 commit comments

Comments
 (0)