Skip to content

Commit 8399533

Browse files
authored
Publish gitopssets-controller helm chart with github-actions (#51)
* Add and copy license into chart * Publish chart to ghcr * Strip leading v from chart version * Include version when building * Log version earlier during startup * Review feedback - bump helm to latests - bump golang to 1.20 - move version.go to root
1 parent a81090a commit 8399533

File tree

6 files changed

+89
-15
lines changed

6 files changed

+89
-15
lines changed

.github/workflows/ci.yaml

+47-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ on:
1212
pull_request_target:
1313
types:
1414
- closed
15+
workflow_dispatch:
1516

1617
env:
1718
REGISTRY: ghcr.io
@@ -36,7 +37,7 @@ jobs:
3637
- name: Setup
3738
uses: actions/setup-go@v3
3839
with:
39-
go-version: 1.19.x
40+
go-version: 1.20.x
4041
cache: true
4142

4243
- name: Test
@@ -54,6 +55,10 @@ jobs:
5455
fetch-depth: 0 # for git describe
5556
ref: ${{ github.event.pull_request.head.sha || github.sha }}
5657

58+
- name: Get version
59+
id: get_version
60+
run: echo "::set-output name=VERSION::$(make version)"
61+
5762
- name: Log in to the Container registry
5863
uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9
5964
with:
@@ -72,7 +77,7 @@ jobs:
7277
GITHUB_BUILD_USERNAME: ${{ secrets.BUILD_BOT_USER }}
7378
GITHUB_BUILD_TOKEN: ${{ secrets.BUILD_BOT_PERSONAL_ACCESS_TOKEN }}
7479
run: git config --global url."https://${GITHUB_BUILD_USERNAME}:${GITHUB_BUILD_TOKEN}@github.com".insteadOf "https://github.com"
75-
80+
7681
- name: go mod vendor
7782
run: go mod vendor
7883

@@ -82,4 +87,43 @@ jobs:
8287
context: .
8388
push: true
8489
tags: ${{ steps.meta.outputs.tags }}
85-
labels: ${{ steps.meta.outputs.labels }}
90+
labels: ${{ steps.meta.outputs.labels }}
91+
build-args: VERSION=${{ steps.get_version.outputs.VERSION }}
92+
93+
build-push-helm-chart:
94+
runs-on: ubuntu-latest
95+
needs: [build, test]
96+
# only run on tag
97+
if: startsWith(github.ref, 'refs/tags/v')
98+
permissions:
99+
contents: read # for actions/checkout to fetch code
100+
steps:
101+
- name: Checkout
102+
uses: actions/checkout@v3
103+
with:
104+
fetch-depth: 0 # for git describe
105+
ref: ${{ github.event.pull_request.head.sha || github.sha }}
106+
107+
- name: Configure git for private modules
108+
env:
109+
GITHUB_BUILD_USERNAME: ${{ secrets.BUILD_BOT_USER }}
110+
GITHUB_BUILD_TOKEN: ${{ secrets.BUILD_BOT_PERSONAL_ACCESS_TOKEN }}
111+
run: git config --global url."https://${GITHUB_BUILD_USERNAME}:${GITHUB_BUILD_TOKEN}@github.com".insteadOf "https://github.com"
112+
113+
- name: Install Helm
114+
run: |
115+
wget --no-verbose https://get.helm.sh/helm-v3.12.1-linux-amd64.tar.gz
116+
tar -zxvf helm-v3.12.1-linux-amd64.tar.gz
117+
mv linux-amd64/helm /usr/local/bin/helm
118+
helm version
119+
120+
- name: Login to GitHub Container Registry
121+
uses: docker/login-action@v2
122+
with:
123+
registry: ghcr.io
124+
username: ${{ github.actor }}
125+
password: ${{ secrets.BUILD_BOT_USER_WRITE_PACKAGES_PAT }}
126+
127+
- name: Build and publish chart
128+
run: |
129+
make publish-helm-chart

Dockerfile

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# Build the manager binary
2-
FROM golang:1.19 as builder
2+
FROM golang:1.20 as builder
33
ARG TARGETOS
44
ARG TARGETARCH
5+
ARG VERSION
56

67
WORKDIR /workspace
78
# Copy the Go Modules manifests
@@ -11,6 +12,7 @@ COPY vendor vendor
1112

1213
# Copy the go source
1314
COPY main.go main.go
15+
COPY version.go version.go
1416
COPY api/ api/
1517
COPY controllers/ controllers/
1618
COPY pkg/ pkg/
@@ -20,7 +22,7 @@ COPY pkg/ pkg/
2022
# was called. For example, if we call make docker-build in a local env which has the Apple Silicon M1 SO
2123
# the docker BUILDPLATFORM arg will be linux/arm64 when for Apple x86 it will be linux/amd64. Therefore,
2224
# by leaving it empty we can ensure that the container and binary shipped on it will have the same platform.
23-
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o manager main.go
25+
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o manager -ldflags "-X main.Version=${VERSION}" main.go version.go
2426

2527
# Use distroless as minimal base image to package the manager binary
2628
# Refer to https://github.com/GoogleContainerTools/distroless for more details

LICENSE

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
GitOpsSets is under the same license and commercial agreement as Weave GitOps Enterprise, and can only be used in conjunction.
2+

Makefile

+24-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11

2+
VERSION ?= $(shell git describe --tags --always)
3+
# Strip off leading `v`: v0.12.0 -> 0.12.0
4+
# Seems to be idiomatic for chart versions: https://helm.sh/docs/topics/charts/#the-chart-file
5+
CHART_VERSION := $(shell echo $(VERSION) | sed 's/^v//')
6+
27
# Image URL to use all building/pushing image targets
3-
IMG ?= ghcr.io/weaveworks/gitopssets-controller:latest
8+
IMG ?= ghcr.io/weaveworks/gitopssets-controller:${VERSION}
49
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
510
ENVTEST_K8S_VERSION = 1.25.0
611
GEN_API_REF_DOCS_VERSION ?= e327d0730470cbd61b06300f81c5fcf91c23c113
7-
12+
CHART_REGISTRY ?= ghcr.io/weaveworks/charts
813

914
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
1015
ifeq (,$(shell go env GOBIN))
@@ -69,7 +74,7 @@ e2e-tests: manifests generate fmt vet envtest ## Run tests.
6974

7075
.PHONY: build
7176
build: manifests generate fmt vet ## Build manager binary.
72-
go build -o bin/manager main.go
77+
go build -o bin/manager -ldflags "-X main.Version=${VERSION}" main.go version.go
7378

7479
.PHONY: run
7580
run: manifests generate fmt vet ## Run a controller from your host.
@@ -80,12 +85,15 @@ vendor: ## Update vendor directory.
8085
go mod tidy
8186
go mod vendor
8287

88+
version:
89+
@echo $(VERSION)
90+
8391
# If you wish built the manager image targeting other platforms you can use the --platform flag.
8492
# (i.e. docker build --platform linux/arm64 ). However, you must enable docker buildKit for it.
8593
# More info: https://docs.docker.com/develop/develop-images/build_enhancements/
8694
.PHONY: docker-build
8795
docker-build: test vendor ## Build docker image with the manager.
88-
docker build -t ${IMG} .
96+
docker build -t ${IMG} --build-arg VERSION=${VERSION} .
8997

9098
.PHONY: docker-push
9199
docker-push: ## Push docker image with the manager.
@@ -182,6 +190,18 @@ helmify:
182190
helm: manifests kustomize helmify
183191
$(KUSTOMIZE) build config/default | $(HELMIFY) -crd-dir ../weave-gitops-enterprise/charts/gitopssets-controller
184192

193+
.PHONY: helm-chart
194+
helm-chart: manifests kustomize helmify
195+
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
196+
$(KUSTOMIZE) build config/default | $(HELMIFY) -crd-dir charts/gitopssets-controller
197+
echo "fullnameOverride: gitopssets" >> charts/gitopssets-controller/values.yaml
198+
cp LICENSE charts/gitopssets-controller/LICENSE
199+
helm lint charts/gitopssets-controller
200+
helm package charts/gitopssets-controller --app-version $(VERSION) --version $(CHART_VERSION) --destination /tmp/helm-repo
201+
202+
publish-helm-chart: helm-chart
203+
helm push /tmp/helm-repo/gitopssets-controller-${CHART_VERSION}.tgz oci://${CHART_REGISTRY}
204+
185205
.PHONY: download-crds
186206
download-crds:
187207
@curl https://raw.githubusercontent.com/fluxcd/source-controller/main/config/crd/bases/source.toolkit.fluxcd.io_gitrepositories.yaml > tests/e2e/testdata/crds/source.toolkit.fluxcd.io_gitrepositories.yaml

main.go

+9-6
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,15 @@ func main() {
6363

6464
flag.Parse()
6565

66+
watchNamespace := ""
67+
if !watchAllNamespaces {
68+
watchNamespace = os.Getenv("RUNTIME_NAMESPACE")
69+
}
70+
71+
ctrl.SetLogger(logger.NewLogger(logOptions))
72+
73+
setupLog.Info("configuring manager", "version", Version)
74+
6675
err := setup.ValidateEnabledGenerators(enabledGenerators)
6776
if err != nil {
6877
setupLog.Error(err, "invalid enabled generators")
@@ -76,12 +85,6 @@ func main() {
7685
os.Exit(1)
7786
}
7887

79-
watchNamespace := ""
80-
if !watchAllNamespaces {
81-
watchNamespace = os.Getenv("RUNTIME_NAMESPACE")
82-
}
83-
84-
ctrl.SetLogger(logger.NewLogger(logOptions))
8588
restConfig := runtimeclient.GetConfigOrDie(clientOptions)
8689
mgr, err := ctrl.NewManager(restConfig, ctrl.Options{
8790
Scheme: scheme,

version.go

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package main
2+
3+
var Version = "dev"

0 commit comments

Comments
 (0)