From 8910d1badbb2c898f7018fb5307c0b415501fef1 Mon Sep 17 00:00:00 2001 From: Bastian Krol Date: Wed, 26 Jun 2024 14:55:27 +0200 Subject: [PATCH] ci: publish helm chart to GitHub pages via GH action --- .github/workflows/ci.yaml | 33 ++++++++++--- helm-chart/bin/publish.sh | 48 +++++++++++++++++++ .../dash0-operator/templates/_helpers.tpl | 6 ++- .../__snapshot__/deployment_test.yaml.snap | 4 +- .../pre-delete-hook_test.yaml.snap | 2 +- .../tests/operator/deployment_test.yaml | 21 ++++++++ .../tests/operator/pre-delete-hook_test.yaml | 11 ++++- helm-chart/dash0-operator/values.yaml | 6 +-- 8 files changed, 117 insertions(+), 14 deletions(-) create mode 100755 helm-chart/bin/publish.sh diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 49ffc578..8900043e 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -63,12 +63,13 @@ jobs: run: | make test - # Builds all container images. For pushes to PRs/branches, we simply verify that the image build still works, the - # resulting image will not be pushed to any target registry. For pushes to the main branch, the images are tagged with - # "main", but not with a version x.y.z. Finally, for pushes to a tag (or when a tag is created), the images are tagged - # with the version indicated by the tag respectively, and also with latest. That is: Creating a GitHub release (or - # creating a git tag via other means) will trigger building images tagged with x.y.z meant for production use. - build-images: + # Builds and potentially pushes all container images. For pushes to PRs/branches, we simply verify that the image + # build still works, the resulting image will not be pushed to any target registry. For pushes to the main branch, the + # images are tagged with "main-dev", but not with a version x.y.z. Finally, for pushes to a tag (or when a tag is + # created), the images are tagged with the version indicated by the tag respectively, and also with latest. That is: + # Creating a GitHub release (or creating a git tag via other means) will trigger building images tagged with x.y.z + # meant for production use. + build-and-push-images: name: Build Images runs-on: ubuntu-latest needs: @@ -134,3 +135,23 @@ jobs: imageTitle: Dash0 Kubernetes Operator Controller imageDescription: the controller for the Dash0 Kubernetes operator context: . + + publish-helm-chart: + name: Publish Helm Chart + runs-on: ubuntu-latest + if: ${{ contains(github.ref, 'refs/tags/') }} + needs: + - build-and-push-images + concurrency: + group: ci-concurrency-group-${{ github.ref }} + cancel-in-progress: true + + steps: + - uses: actions/checkout@v4 + + - name: publish helm chart + run: | + git config user.name "github-actions[bot]" + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" + echo "publishing helm chart version ${{ github.ref_name }}" + helm-chart/bin/publish.sh ${{ github.ref_name }} diff --git a/helm-chart/bin/publish.sh b/helm-chart/bin/publish.sh new file mode 100755 index 00000000..0416eee5 --- /dev/null +++ b/helm-chart/bin/publish.sh @@ -0,0 +1,48 @@ +#!/usr/bin/env bash + +# SPDX-FileCopyrightText: Copyright 2024 Dash0 Inc. +# SPDX-License-Identifier: Apache-2.0 + +set -euo pipefail + +cd "$(dirname ${BASH_SOURCE})"/.. + +# Abort when there are local changes in the repository. This is mostly relevant for testing the script locally. +if ! git diff --quiet --exit-code; then + echo "error: The repository has local changes, aborting." + git --no-pager diff + exit 1 +fi + +version=${1:-} + +if [[ -z $version ]]; then + echo "Mandatory parameter version is missing." + echo "Usage: $0 " + exit 1 +fi + +echo "packaging Helm chart as version $version" +helm package \ + dash0-operator \ + --version $version \ + --app-version $version \ + --dependency-update \ + --destination .. +echo "packaging Helm version $version has been packaged" + +cd .. +echo "switching to gh-pages branch" +git fetch origin gh-pages:gh-pages +git switch gh-pages + +# clean up potential left-overs from the --dependency-update flag +rm -rf helm-chart + +echo "creating Helm chart index" +helm repo index . + +echo "git add, commit & push" +git add "dash0-operator-$version.tgz" index.yaml +git commit -m"feat(chart): publish version $version" +git push --no-verify --set-upstream origin gh-pages diff --git a/helm-chart/dash0-operator/templates/_helpers.tpl b/helm-chart/dash0-operator/templates/_helpers.tpl index f73c0876..eaed9add 100644 --- a/helm-chart/dash0-operator/templates/_helpers.tpl +++ b/helm-chart/dash0-operator/templates/_helpers.tpl @@ -53,7 +53,11 @@ helm.sh/chart: {{ include "dash0-operator.chartNameWithVersion" . }} {{/* the init container image */}} {{- define "dash0-operator.initContainerImage" -}} -{{- printf "%s:%s" .Values.operator.initContainerImage.repository .Values.operator.initContainerImage.tag }} +{{- printf "%s:%s" .Values.operator.initContainerImage.repository (include "dash0-operator.initContainerImageTag" .) }} +{{- end }} + +{{- define "dash0-operator.initContainerImageTag" -}} +{{- default .Chart.AppVersion .Values.operator.initContainerImage.tag }} {{- end }} {{- define "dash0-operator.restrictiveContainerSecurityContext" -}} diff --git a/helm-chart/dash0-operator/tests/operator/__snapshot__/deployment_test.yaml.snap b/helm-chart/dash0-operator/tests/operator/__snapshot__/deployment_test.yaml.snap index 6c652f93..7e12e3fc 100644 --- a/helm-chart/dash0-operator/tests/operator/__snapshot__/deployment_test.yaml.snap +++ b/helm-chart/dash0-operator/tests/operator/__snapshot__/deployment_test.yaml.snap @@ -39,10 +39,10 @@ deployment should match snapshot (default values): - name: DASH0_OTEL_COLLECTOR_BASE_URL value: http://RELEASE-NAME-opentelemetry-collector.NAMESPACE.svc.cluster.local:4318 - name: DASH0_OPERATOR_IMAGE - value: dash0-operator-controller:1.0.0 + value: ghcr.io/dash0hq/operator-controller:1.0.0 - name: DASH0_INIT_CONTAINER_IMAGE value: ghcr.io/dash0hq/instrumentation:1.0.0 - image: dash0-operator-controller:1.0.0 + image: ghcr.io/dash0hq/operator-controller:1.0.0 livenessProbe: httpGet: path: /healthz diff --git a/helm-chart/dash0-operator/tests/operator/__snapshot__/pre-delete-hook_test.yaml.snap b/helm-chart/dash0-operator/tests/operator/__snapshot__/pre-delete-hook_test.yaml.snap index 5d432b62..0e47476f 100644 --- a/helm-chart/dash0-operator/tests/operator/__snapshot__/pre-delete-hook_test.yaml.snap +++ b/helm-chart/dash0-operator/tests/operator/__snapshot__/pre-delete-hook_test.yaml.snap @@ -32,7 +32,7 @@ pre-delete hook job should match snapshot: - command: - /manager - --uninstrument-all - image: dash0-operator-controller:1.0.0 + image: ghcr.io/dash0hq/operator-controller:1.0.0 imagePullPolicy: null name: pre-delete-job resources: diff --git a/helm-chart/dash0-operator/tests/operator/deployment_test.yaml b/helm-chart/dash0-operator/tests/operator/deployment_test.yaml index 077f8f9e..43b6d156 100644 --- a/helm-chart/dash0-operator/tests/operator/deployment_test.yaml +++ b/helm-chart/dash0-operator/tests/operator/deployment_test.yaml @@ -6,6 +6,27 @@ tests: asserts: - matchSnapshot: {} + - it: image tags should default to appVersion + chart: + version: 4.5.6 + appVersion: 99.100.101 + asserts: + - equal: + path: spec.template.spec.containers[0].image + value: ghcr.io/dash0hq/operator-controller:99.100.101 + - equal: + path: spec.template.spec.containers[0].env[1].name + value: DASH0_OPERATOR_IMAGE + - equal: + path: spec.template.spec.containers[0].env[1].value + value: ghcr.io/dash0hq/operator-controller:99.100.101 + - equal: + path: spec.template.spec.containers[0].env[2].name + value: DASH0_INIT_CONTAINER_IMAGE + - equal: + path: spec.template.spec.containers[0].env[2].value + value: ghcr.io/dash0hq/instrumentation:99.100.101 + - it: should render deployment with custom settings set: operator: diff --git a/helm-chart/dash0-operator/tests/operator/pre-delete-hook_test.yaml b/helm-chart/dash0-operator/tests/operator/pre-delete-hook_test.yaml index 00624478..81fced0f 100644 --- a/helm-chart/dash0-operator/tests/operator/pre-delete-hook_test.yaml +++ b/helm-chart/dash0-operator/tests/operator/pre-delete-hook_test.yaml @@ -4,4 +4,13 @@ templates: tests: - it: pre-delete hook job should match snapshot asserts: - - matchSnapshot: {} \ No newline at end of file + - matchSnapshot: {} + + - it: image tag should default to appVersion + chart: + version: 4.5.6 + appVersion: 99.100.101 + asserts: + - equal: + path: spec.template.spec.containers[0].image + value: ghcr.io/dash0hq/operator-controller:99.100.101 diff --git a/helm-chart/dash0-operator/values.yaml b/helm-chart/dash0-operator/values.yaml index f3de9257..76849d9f 100644 --- a/helm-chart/dash0-operator/values.yaml +++ b/helm-chart/dash0-operator/values.yaml @@ -74,7 +74,7 @@ operator: image: # Use a different image entirely. Note that Dash0 does not offer support for Dash0 operator setups that do not use # Dash0's official image. - repository: "dash0-operator-controller" + repository: "ghcr.io/dash0hq/operator-controller" # overrides the image tag, which defaults to the chart appVersion. tag: # override the default image pull policy @@ -86,8 +86,8 @@ operator: # Use a different image for the init container entirely. Note that Dash0 does not offer support for Dash0 operator # setups that do not use Dash0's official init container image. repository: "ghcr.io/dash0hq/instrumentation" - # overrides the image tag for the init container image - tag: "1.0.0" + # overrides the image tag, which defaults to the chart appVersion. + tag: # override the default image pull policy pullPolicy: