-
Notifications
You must be signed in to change notification settings - Fork 2.3k
242 lines (215 loc) · 8.68 KB
/
update-helm-repo.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
name: Release a chart
on:
workflow_call:
inputs:
charts_dir:
description: path to the helm charts
default: charts
required: false
type: string
cr_configfile:
description: location of the chart releaser (cr) config file
default: cr.yaml
required: false
type: string
ct_configfile:
description: location of the chart tester (ct) config file
default: ct.yaml
required: false
type: string
helm_tag_prefix:
description: prefix to use when publishing helm tags
default: ""
required: false
type: string
secrets:
github_app_id:
description: Github App ID to authenticate with
required: false
github_app_pem:
description: GitHub APP pem to authenticate with
required: false
helm_repo_token:
description: GitHub api token to use against the helm-charts repository
required: false
env:
CR_CONFIGFILE: "${{ github.workspace }}/source/${{ inputs.cr_configfile }}"
CT_CONFIGFILE: "${{ github.workspace }}/source/${{ inputs.ct_configfile }}"
CR_INDEX_PATH: "${{ github.workspace }}/.cr-index"
CR_PACKAGE_PATH: "${{ github.workspace }}/.cr-release-packages"
CR_TOOL_PATH: "${{ github.workspace }}/.cr-tool"
HELM_TAG_PREFIX: "${{ inputs.helm_tag_prefix }}"
jobs:
setup:
runs-on: ubuntu-latest
outputs:
changed: ${{ steps.list-changed.outputs.changed }}
chartpath: ${{ steps.list-changed.outputs.chartpath }}
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
path: source
- name: Set up chart-testing
uses: helm/[email protected]
- name: List changed charts
id: list-changed
run: |
cd source
latest_tag=$( if ! git describe --tags --abbrev=0 2> /dev/null ; then git rev-list --max-parents=0 --first-parent HEAD ; fi )
echo "Running: ct list-changed --config ${CT_CONFIGFILE} --since ${latest_tag} --target-branch ${{ github.ref_name }}"
changed=$(ct list-changed --config "${CT_CONFIGFILE}" --since "${latest_tag}" --target-branch "${{ github.ref_name }}")
echo "${changed}"
num_changed=$(wc -l <<< ${changed})
if [[ "${num_changed}" -gt "1" ]] ; then
echo "More than one chart changed, exiting"
exit 1
fi
if [[ -n "${changed}" ]]; then
name=$(yq ".name" < ${changed}/Chart.yaml)
version=$(yq ".version" < ${changed}/Chart.yaml)
if [ $(git tag -l "${name}-${version}") ]; then
echo "Tag ${tagname} already exists, skipping release"
echo "changed=false" >> $GITHUB_OUTPUT
else
echo "Releasing ${changed}"
echo "changed=true" >> $GITHUB_OUTPUT
echo "chartpath=${changed}" >> $GITHUB_OUTPUT
fi
else
echo "No charts have changed, skipping release"
echo "changed=false" >> $GITHUB_OUTPUT
fi
release:
needs: [setup]
runs-on: ubuntu-latest
permissions:
packages: write # allows GITHUB_TOKEN to push package to ghcr
env:
github_app_id: ${{ secrets.github_app_id }}
if: needs.setup.outputs.changed == 'true'
steps:
- name: Create a GitHub App installation access token
if: env.github_app_id != ''
uses: tibdex/github-app-token@v2
id: app-token
with:
app_id: ${{ secrets.github_app_id }}
private_key: ${{ secrets.github_app_pem }}
- name: Set the correct token (Github App or PAT)
run: |
if [[ "${{ env.github_app_id }}" == '' ]]; then
echo "AUTHTOKEN=${{ secrets.helm_repo_token }}" >> $GITHUB_ENV
else
echo "AUTHTOKEN=${{ steps.app-token.outputs.token }}" >> $GITHUB_ENV
fi
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
path: source
- name: Configure Git
run: |
cd source
git config user.name "$GITHUB_ACTOR"
git config user.email "[email protected]"
- name: Checkout helm-charts
# The cr tool only works if the target repository is already checked out
uses: actions/checkout@v4
with:
fetch-depth: 0
repository: grafana/helm-charts
path: helm-charts
token: ${{ env.AUTHTOKEN }}
- name: Configure Git for helm-charts
run: |
cd helm-charts
git config user.name "$GITHUB_ACTOR"
git config user.email "[email protected]"
- name: Install Helm
uses: azure/setup-helm@v4
with:
version: v3.16.2
- name: Parse Chart.yaml
id: parse-chart
run: |
cd source
changed="${{ needs.setup.outputs.chartpath }}"
description=$(yq ".description" < ${changed}/Chart.yaml)
name=$(yq ".name" < ${changed}/Chart.yaml)
version=$(yq ".version" < ${changed}/Chart.yaml)
echo "chartpath=${changed}" >> $GITHUB_OUTPUT
echo "desc=${description}" >> $GITHUB_OUTPUT
if [[ -n "${HELM_TAG_PREFIX}" ]]; then
echo "tagname=${HELM_TAG_PREFIX}-${name}-${version}" >> $GITHUB_OUTPUT
else
echo "tagname=${name}-${version}" >> $GITHUB_OUTPUT
fi
echo "packagename=${name}-${version}" >> $GITHUB_OUTPUT
- name: Add dependency chart repos
run: |
cd source
# Skip the header line and make sure that tabs are expanded into spaces
deps=$(helm dependency list "${{ steps.parse-chart.outputs.chartpath }}" | tail +2 | expand)
while read -r row; do
IFS=' ' read -ra parts <<< "$row"
name="${parts[0]}"
repo="${parts[2]}"
case "$repo" in
"https://"*) helm repo add "$name" "$repo" ;;
*) echo >&2 "Skipping dependency $name: unsupported schema for \"$repo\"" ;;
esac
done <<< "$deps"
- name: Install CR tool
run: |
mkdir "${CR_TOOL_PATH}"
mkdir "${CR_PACKAGE_PATH}"
mkdir "${CR_INDEX_PATH}"
curl -sSLo cr.tar.gz "https://github.com/helm/chart-releaser/releases/download/v1.4.0/chart-releaser_1.4.0_linux_amd64.tar.gz"
tar -xzf cr.tar.gz -C "${CR_TOOL_PATH}"
rm -f cr.tar.gz
- name: Create helm package
run: |
cd source
"${CR_TOOL_PATH}/cr" package "${{ steps.parse-chart.outputs.chartpath }}" --config "${CR_CONFIGFILE}" --package-path "${CR_PACKAGE_PATH}"
echo "Result of chart package:"
ls -l "${CR_PACKAGE_PATH}"
- name: Create tag and check if exists on origin
run: |
cd source
echo "Making tag ${{ steps.parse-chart.outputs.tagname }}"
git tag "${{ steps.parse-chart.outputs.tagname }}"
- name: Make github release
uses: softprops/action-gh-release@v1
with:
body: |
${{ steps.parse-chart.outputs.desc }}
Source commit: https://github.com/${{ github.repository }}/commit/${{ github.sha }}
Tag on source: https://github.com/${{ github.repository }}/releases/tag/${{ steps.parse-chart.outputs.tagname }}
files: |
${{ env.CR_PACKAGE_PATH }}/${{ steps.parse-chart.outputs.packagename }}.tgz
${{ env.CR_PACKAGE_PATH }}/${{ steps.parse-chart.outputs.packagename }}.tgz.prov
repository: grafana/helm-charts
tag_name: ${{ steps.parse-chart.outputs.tagname }}
token: ${{ env.AUTHTOKEN }}
- name: Push release tag on origin
run: |
cd source
echo "Pushing tag ${{ steps.parse-chart.outputs.tagname }}"
git push origin "${{ steps.parse-chart.outputs.tagname }}"
- name: Update helm repo index.yaml
run: |
cd helm-charts
"${CR_TOOL_PATH}/cr" index --config "${CR_CONFIGFILE}" --token "${{ env.AUTHTOKEN }}" --index-path "${CR_INDEX_PATH}" --package-path "${CR_PACKAGE_PATH}" --push
- name: Login to GHCR
uses: docker/[email protected]
with:
registry: ghcr.io
username: ${{ github.actor }}
# GitHub App installation token cannot push package to the org's package registry;
# using GITHUB_TOKEN here instead to avoid using PAT.
password: ${{ secrets.GITHUB_TOKEN }}
- name: Push charts to GHCR
run: |
helm push "${{ env.CR_PACKAGE_PATH }}/${{ steps.parse-chart.outputs.packagename }}.tgz" "oci://ghcr.io/${GITHUB_REPOSITORY_OWNER}/helm-charts"