11---
22name : " Test Helm Chart"
3- # yamllint disable-line rule:line-length
4- description : " Action to test a Helm chart. Mainly using [helm/chart-testing-action](https://github.com/helm/chart-testing-action)"
3+ description : |
4+ Action to lint and test installing some Helm chart(s).
5+ Mainly using [helm/chart-testing-action](https://github.com/helm/chart-testing-action).
6+
57branding :
68 icon : check-circle
79 color : gray-dark
@@ -41,15 +43,20 @@ inputs:
4143 See <https://github.com/docker/login-action#usage>.
4244 default : ${{ github.token }}
4345 required : false
46+ check-diff-only :
47+ description : |
48+ Only run lint and tests on changed charts.
49+ required : false
50+ default : " true"
4451 enable-lint :
4552 description : |
4653 Enable linting of the Helm chart.
4754 See <https://github.com/helm/chart-testing/blob/main/doc/ct_lint.md>.
4855 required : false
4956 default : " true"
50- enable-test :
57+ enable-install :
5158 description : |
52- Enable testing of the Helm chart.
59+ Enable installing the Helm chart.
5360 See <https://github.com/helm/chart-testing/blob/main/doc/ct_install.md>.
5461 required : false
5562 default : " true"
5865 using : " composite"
5966 steps :
6067 - shell : bash
61- if : ${{ inputs.enable-lint != 'true' && inputs.enable-test != 'true' }}
68+ if : ${{ inputs.enable-lint != 'true' && inputs.enable-install != 'true' }}
6269 run : |
63- echo "::error ::At least one of 'enable-lint' or 'enable-test ' input must be true"
70+ echo "::error ::At least one of 'enable-lint' or 'enable-install ' input must be true"
6471 exit 1
6572
6673 -
uses :
hoverkraft-tech/ci-github-common/actions/[email protected] 9299 fi
93100
94101 - uses : actions/setup-python@v5
95- with :
96- python-version : " 3.12"
97102
98103 - name : Set up chart-testing
99104@@ -110,86 +115,109 @@ runs:
110115 helm repo add $line
111116 done
112117
113- - name : Run chart-testing (lint)
114- if : ${{ inputs.enable-lint == 'true' }}
118+ - name : Prepare ct variables
119+ id : prepare-ct-variables
115120 shell : bash
116- working-directory : ${{ inputs.working-directory }}
117121 run : |
118- LINT_ARGS="--check-version-increment=false --target-branch ${{ github.event.repository.default_branch }}"
122+ if [ "${{ inputs.check-diff-only }}" == "true" ]; then
123+ if [ "${{ github.event_name }}" == "pull_request" ]; then
124+ TARGET_BRANCH="${{ github.event.pull_request.base.ref }}"
125+ else
126+ TARGET_BRANCH="${{ github.event.repository.default_branch }}"
127+ fi
128+ CT_ARGS="--target-branch $TARGET_BRANCH"
129+ fi
130+
119131 if [ -n "${{ steps.check-ct-yaml.outputs.path }}" ]; then
120- LINT_ARGS=" --config ${{ steps.check-ct-yaml.outputs.path }}"
132+ CT_ARGS="$CT_ARGS --config ${{ steps.check-ct-yaml.outputs.path }}"
121133 fi
122134
123- ct lint $LINT_ARGS
135+ if [ -z "$CT_ARGS" ]; then
136+ CT_ARGS="--all"
137+ fi
124138
125- - name : Create kind cluster
126- if : ${{ inputs.enable-test == 'true' }}
127- 139+ echo "args=$CT_ARGS" >> "$GITHUB_OUTPUT"
140+
141+ # Namespace for the test cluster
142+ NAMESPACE="test-chart-${{ github.run_id}}-$(uuidgen)"
143+ echo "namespace=$NAMESPACE" >> "$GITHUB_OUTPUT"
128144
129- - name : Install default registry secrets
130- if : ${{ inputs.enable-test == 'true' }}
145+ - name : Run chart-testing (lint)
146+ if : ${{ inputs.enable-lint == 'true' }}
131147 shell : bash
148+ working-directory : ${{ inputs.working-directory }}
132149 run : |
133- echo "DOCKER_REGISTRY=$DOCKER_REGISTRY"
134- echo "DOCKER_USERNAME=${DOCKER_USERNAME: -4}" # last 4 characters
135- echo "DOCKER_PASSWORD=${DOCKER_PASSWORD: -4}" # last 4 characters
136- if [ -n "$DOCKER_USERNAME" ] && [ -n "$DOCKER_PASSWORD" ]; then
137- kubectl create secret docker-registry regcred \
138- --docker-server=$DOCKER_REGISTRY \
139- --docker-username=$DOCKER_USERNAME \
140- --docker-password=$DOCKER_PASSWORD
141- else
142- echo "Docker credentials not provided, skipping secret creation"
143- fi
144- env :
145- DOCKER_REGISTRY : ${{ inputs.oci-registry }}
146- DOCKER_USERNAME : ${{ inputs.oci-registry-username }}
147- DOCKER_PASSWORD : ${{ inputs.oci-registry-password }}
150+ ct lint ${{ steps.prepare-ct-variables.outputs.args }}
148151
149- - name : Define target branch
150- if : ${{ inputs.enable-test == 'true' }}
151- id : define-target-branch
152+ - name : Create kind cluster
153+ if : ${{ inputs.enable-install == 'true' }}
154+ 155+
156+ - name : Install default OCI registry secrets
157+ id : oci-registry-secret
158+ if : ${{ inputs.enable-install == 'true' && inputs.oci-registry != '' && inputs.oci-registry-username != '' && inputs.oci-registry-password != '' }}
152159 shell : bash
153160 run : |
154- if [ "${{ github.event_name }}" == "pull_request" ]; then
155- TARGET_BRANCH="${{ github.event.pull_request.base.ref }}"
156- else
157- TARGET_BRANCH="${{ github.event.repository.default_branch }}"
158- fi
159- echo "target-branch=$TARGET_BRANCH" >> "$GITHUB_OUTPUT"
161+ # See https://kubernetes.io/docs/tasks/configure-pod-container/pull-image-private-registry/
162+ NAMESPACE="${{ steps.prepare-ct-variables.outputs.namespace }}"
163+ kubectl --context kind-chart-testing create namespace "$NAMESPACE"
164+
165+ SECRET_NAME="regcred"
166+ DOCKER_REGISTRY="${{ inputs.oci-registry }}"
167+ DOCKER_USERNAME="${{ inputs.oci-registry-username }}"
168+ DOCKER_PASSWORD="${{ inputs.oci-registry-password }}"
169+
170+ kubectl --context kind-chart-testing create secret docker-registry "$SECRET_NAME" \
171+ --namespace="$NAMESPACE" \
172+ --docker-server=$DOCKER_REGISTRY \
173+ --docker-username=$DOCKER_USERNAME \
174+ --docker-password=$DOCKER_PASSWORD
175+
176+ echo "oci-registry-secret=$SECRET_NAME" >> "$GITHUB_OUTPUT"
160177
161178 - name : Run chart-testing (install)
162- if : ${{ inputs.enable-test == 'true' }}
179+ if : ${{ inputs.enable-install == 'true' }}
163180 shell : bash
164181 working-directory : ${{ inputs.working-directory }}
165182 env :
166183 HELM_EXPERIMENTAL_OCI : true
167184 run : |
168- HELM_SET="${{ inputs.helm-set }}"
169- HELM_EXTRA_SET_ARGS="imagePullSecrets[0].name=regcred"
185+ NAMESPACE="${{ steps.prepare-ct-variables.outputs.namespace }}"
186+
187+ HELM_SET="namespace=$NAMESPACE
188+ ${{ inputs.helm-set }}"
189+
190+ OCI_REGISTRY_SECRET="${{ steps.oci-registry-secret.outputs.oci-registry-secret }}"
191+ if [ -n "$OCI_REGISTRY_SECRET" ]; then
192+ # Ensure secret exists
193+ kubectl --context kind-chart-testing get secret "$OCI_REGISTRY_SECRET" --output=yaml --namespace=$NAMESPACE
194+
195+ HELM_SET="$HELM_SET
196+ imagePullSecrets[0].name=${OCI_REGISTRY_SECRET}"
197+ fi
198+
199+ HELM_EXTRA_SET_ARGS=""
170200 if [ -n "$HELM_SET" ]; then
171201 IFS=$'\n' read -r -d '' -a lines <<< "$HELM_SET" || true
172-
173202 for line in "${lines[@]}"; do
174203 if [ -z "$line" ]; then
175204 continue
176205 fi
177206 # Escape commas in the line
178207 line=$(echo "$line" | sed 's/,/\\,/g') || true
179- HELM_EXTRA_SET_ARGS+=",${line}"
180- done
181208
182- # Format HELM_EXTRA_SET_ARGS for helm command
183- if [ -n "$HELM_EXTRA_SET_ARGS" ]; then
184- HELM_EXTRA_SET_ARGS="--set ${HELM_EXTRA_SET_ARGS:1}"
185- fi
209+ if [ -n "$HELM_EXTRA_SET_ARGS" ]; then
210+ HELM_EXTRA_SET_ARGS="${HELM_EXTRA_SET_ARGS},"
211+ fi
186212
187- echo "::debug::helm-extra-set-args: $HELM_EXTRA_SET_ARGS"
213+ HELM_EXTRA_SET_ARGS="${HELM_EXTRA_SET_ARGS}${line}"
214+ done
188215 fi
189216
190- TARGET_BRANCH="${{ steps.define-target-branch.outputs.target-branch }}"
217+ HELM_EXTRA_SET_ARGS="--set=${HELM_EXTRA_SET_ARGS}"
218+
219+ echo "::debug::ct install ${{ steps.prepare-ct-variables.outputs.args }} --helm-extra-set-args ${HELM_EXTRA_SET_ARGS}"
191220
192- ct install \
193- --target-branch "$TARGET_BRANCH" \
194- --helm-extra-args "--wait" \
195- --helm-extra-set-args "$HELM_EXTRA_SET_ARGS"
221+ ct install ${{ steps.prepare-ct-variables.outputs.args }} \
222+ --namespace $NAMESPACE \
223+ --helm-extra-set-args ${HELM_EXTRA_SET_ARGS}
0 commit comments