-
Notifications
You must be signed in to change notification settings - Fork 1
429 lines (399 loc) · 15.6 KB
/
publish_charm.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
# Copyright 2024 Canonical Ltd.
# See LICENSE file for licensing details.
name: Publish charm
on:
push:
branches:
- main
pull_request:
env:
OWNER: ${{ github.repository_owner }}
REGISTRY: ghcr.io
channel: latest/edge
charm_working_directory: "./charm"
charmcraft_ref: feat-12f-django
charmcraft_repository: weiiwang01/charmcraft
paas-app-charmer-oci-name: django-app-image
tag_prefix:
working_directory: "./"
jobs:
branch-up-to-date-check-enabled:
runs-on: ubuntu-22.04
steps:
- uses: octokit/[email protected]
id: get-branch-protection
env:
GITHUB_TOKEN: ${{ secrets.REPO_ACCESS_TOKEN }}
with:
route: GET /repos/{owner}/{repo}/branches/{branch}/protection/required_status_checks
repo: ${{ github.event.repository.name }}
owner: ${{ github.event.repository.owner.login }}
branch: main
- run: |
if [ ${{ fromJson(steps.get-branch-protection.outputs.data).strict }} != "true" ]; then
echo "::error::Strict checks are not enabled for this repository"
exit 1
fi
draft-publish-docs:
name: Draft publish docs
needs: [ branch-up-to-date-check-enabled ]
runs-on: ubuntu-22.04
defaults:
run:
working-directory: ${{ env.charm_working_directory || env.working_directory }}
steps:
- uses: actions/[email protected]
- name: Search for docs folder
id: docs-exist
run: echo "docs_exist=$([[ -d docs ]] && echo 'True' || echo 'False')" >> $GITHUB_OUTPUT
- name: Publish documentation
if: steps.docs-exist.outputs.docs_exist == 'True'
uses: canonical/discourse-gatekeeper@stable
with:
discourse_host: discourse.charmhub.io
discourse_api_username: ${{ secrets.DISCOURSE_API_USERNAME }}
discourse_api_key: ${{ secrets.DISCOURSE_API_KEY }}
dry_run: true
github_token: ${{ secrets.GITHUB_TOKEN }}
charm_dir: ${{ env.charm_working_directory || env.working_directory }}
vars:
runs-on: ubuntu-22.04
outputs:
charm_working_directory: ${{ env.charm_working_directory || env.working_directory }}
channel: ${{ env.channel }}
steps:
- run: echo "Exposing env vars"
get-runner-image:
name: Get runner image
needs: vars
uses: canonical/operator-workflows/.github/workflows/get_runner_image.yaml@test-netbox
with:
working-directory: ${{ needs.vars.outputs.charm_working_directory }}
secrets: inherit
release-charm-libs:
name: Release charm libs
needs: [ branch-up-to-date-check-enabled ]
runs-on: ubuntu-22.04
steps:
- uses: actions/[email protected]
- if: env.charm_working_directory != ''
name: Change directory
run: |
TEMP_DIR=$(mktemp -d)
cp -rp ./${{env.charm_working_directory}}/. $TEMP_DIR
rm -rf .* * || :
cp -rp $TEMP_DIR/. .
rm -rf $TEMP_DIR
- uses: canonical/charming-actions/[email protected]
name: Release libs
with:
credentials: ${{ secrets.CHARMHUB_TOKEN }}
github-token: ${{ secrets.GITHUB_TOKEN }}
get-run-id:
name: Get workflow run id
needs: [ branch-up-to-date-check-enabled ]
runs-on: ubuntu-22.04
outputs:
run-id: ${{ env.RUN_ID }}
steps:
- name: Get workflow run id
# TODO JAVI TEST if: ${{ github.event_name == 'push' }}
if: false
shell: bash
run: |
# Get commit info
TREE_SHA=$(gh api \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
/repos/${{ github.repository }}/commits/${GITHUB_SHA} \
--jq '.commit.tree.sha')
# Get workflow run id from this specific tree id, paginate until found
TOTAL_COUNT=$(gh api \
--method GET \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
/repos/${{ github.repository }}/actions/runs \
-f status=completed \
-f event=pull_request | jq ".total_count")
PER_PAGE=100
MAX_PAGES=$(( (TOTAL_COUNT + 99) / $PER_PAGE ))
PAGE=1
while true; do
RESULT=$( gh api \
--method GET \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
/repos/${{ github.repository }}/actions/runs \
-f page=$PAGE \
-f per_page=$PER_PAGE \
-f status=completed \
-f event=pull_request \
--jq "[
.workflow_runs[]
| select(.path == \".github/workflows/integration_test.yaml\")
| select(.head_commit.tree_id == \"$TREE_SHA\")
] | max_by(.updated_at) | .id"
)
if [[ -n $RESULT ]]; then
RUN_ID=$RESULT
break
fi
if [[ "PAGE" -eq "$MAX_PAGES" ]]; then
echo "::error::No workflow run id found for specific tree id"
exit 1
fi
((PAGE++))
done
echo "RUN_ID=$RUN_ID" >> $GITHUB_ENV
env:
GITHUB_TOKEN: ${{ secrets.REPO_ACCESS_TOKEN }}
- name: Get current run id
# TODO JAVI TEST if: ${{ github.event_name == 'pull_request' }}
if: false
shell: bash
run: |
echo "RUN_ID=${{ github.run_id }}" >> $GITHUB_ENV
- name: Get current run id (TODO TO TEST)
shell: bash
run: |
echo "RUN_ID=8597165771" >> $GITHUB_ENV
get-images:
name: Get images
runs-on: ubuntu-22.04
outputs:
images: ${{ env.IMAGES }}
steps:
- uses: actions/[email protected]
- name: Get images
working-directory: ${{ env.working_directory }}
run: |
lines=$(find . -type f -name rockcraft.yaml | wc -l)
if [ $lines -ne 0 ]; then
IMAGES=$(find . -type f -name rockcraft.yaml | xargs -l yq '.name' | jq -Rsc '. / "\n" - [""]')
else
IMAGES=$(ls *.Dockerfile 2> /dev/null | sed s/\.Dockerfile// | jq -Rsc '. / "\n" - [""]')
fi
echo "IMAGES=$IMAGES" >> $GITHUB_ENV
publish-images:
name: Publish images to charmhub
runs-on: ${{ needs.get-runner-image.outputs.runs-on }}
needs: [ get-images, get-run-id, get-runner-image]
if: ${{ needs.get-images.outputs.images != '[]' }}
steps:
- uses: actions/checkout@v4
if: env.charmcraft_repository != ''
with:
repository: ${{ env.charmcraft_repository }}
ref: ${{ env.charmcraft_ref }}
path: ./charmcraft
fetch-depth: 0
- name: Get Charmcraft SHA
if: env.charmcraft_repository != ''
id: charmcraft-sha
working-directory: ./charmcraft
run: echo "sha=$(git rev-parse HEAD)" >> $GITHUB_OUTPUT
- name: Restore Charmcraft Cache
if: env.charmcraft_repository != ''
id: restore-charmcraft
uses: actions/cache/restore@v4
with:
path: ./charmcraft*.snap
key: charmcraft-${{ steps.charmcraft-sha.outputs.sha }}
- name: Install Snapcraft
if: steps.restore-charmcraft.outputs.cache-hit != 'true' && env.charmcraft_repository != ''
run: sudo snap install snapcraft --classic
- name: Build Charmcraft
if: steps.restore-charmcraft.outputs.cache-hit != 'true' && env.charmcraft_repository != ''
working-directory: ./charmcraft
run: |
snapcraft --use-lxd
cp charmcraft*.snap ../
- name: Save Charmcraft Cache
uses: actions/cache/save@v4
if: steps.restore-charmcraft.outputs.cache-hit != 'true' && env.charmcraft_repository != ''
with:
path: ./charmcraft*.snap
key: ${{ steps.restore-charmcraft.outputs.cache-primary-key }}
- name: Install charmcraft
if: env.charmcraft_repository != ''
run: sudo snap install --dangerous --classic charmcraft*.snap
- name: Clean up
if: env.charmcraft_repository != ''
run: rm -rf *
- name: Install charmcraft
if: env.charmcraft_repository == ''
run: |
sudo snap install charmcraft --classic --channel ${{ env.charmcraft_channel }}
- uses: actions/[email protected]
- if: env.charm_working_directory != ''
name: Change directory
run: |
TEMP_DIR=$(mktemp -d)
cp -rp ./${{env.charm_working_directory}}/. $TEMP_DIR
rm -rf .* * || :
cp -rp $TEMP_DIR/. .
rm -rf $TEMP_DIR
- name: Get charm name
id: get-charm-name
run: |
CHARM_NAME="$([ -f metadata.yaml ] && yq '.name' metadata.yaml || echo UNKNOWN)"
if [ "$CHARM_NAME" == "UNKNOWN" ]; then
CHARM_NAME="$([ -f charmcraft.yaml ] && yq '.name' charmcraft.yaml || echo UNKNOWN)"
fi
echo "CHARM_NAME=$CHARM_NAME">> $GITHUB_ENV
- name: Log in to the Container registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Download images artifact
# TODO JAVI TEST if: ${{ github.event_name == 'push' }}
run: |
gh run download ${{ needs.get-run-id.outputs.run-id }} -R ${{ github.repository }} -n ${{ env.CHARM_NAME }}-images
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Download images artifact (for testing)
uses: actions/download-artifact@v4
# TODO JAVI TEST if: ${{ github.event_name == 'pull_request' }}
if: false
with:
name: ${{ env.CHARM_NAME }}-images
- name: Publish image
env:
CHARMCRAFT_ENABLE_EXPERIMENTAL_EXTENSIONS: "true"
CHARMCRAFT_AUTH: ${{ secrets.CHARMHUB_TOKEN }}
run: |
set -x
charm=${{ env.CHARM_NAME }}
declare -a resources
declare -a images
if [ -f metadata.yaml ]; then
for resource in $(yq -er '.resources | with_entries(select(.value.type=="oci-image")) | keys | join(" ")' metadata.yaml); do
resources+=("$resource")
done
else
for resource in $(yq -er '.resources | with_entries(select(.value.type=="oci-image")) | keys | join(" ")' <(charmcraft expand-extensions)); do
resources+=("$resource")
done
fi
for image in $(cat ${{ env.CHARM_NAME }}-images); do
images+=("$image")
done
echo resources: -"${resources[@]}"-
echo images: -"${images[@]}"-
for image in "${images[@]}"; do
if [[ "${{ paas-app-charmer-oci-name }}" != "" ]]; then
# This is for paas-app-charmer . The main image name (resource oci in charmcraft.yaml) is
# defined with the env variable paas-app-charmer-oci-name, and corresponds to the main
# image in rockcraft.yaml.
resource_name="{{ paas-app-charmer-oci-name }}"
else
resource_name=$(echo $image | awk -F '/' '{print $NF}' | cut -d ':' -f 1)-image
fi
if [[ " ${resources[@]} " =~ " $resource " ]]; then
docker pull $image
image_id=$(docker images $image --format "{{.ID}}")
charmcraft upload-resource $charm $resource --image=$image_id --verbosity=brief
fi
done
publish-charm:
name: Publish charm to ${{ needs.vars.outputs.channel }}
runs-on: ${{ needs.get-runner-image.outputs.runs-on }}
needs: [get-run-id, get-runner-image, publish-images, vars]
if: ${{ !failure() }}
steps:
- uses: actions/checkout@v4
if: env.charmcraft_repository != ''
with:
repository: ${{ env.charmcraft_repository }}
ref: ${{ env.charmcraft_ref }}
path: ./charmcraft
fetch-depth: 0
- name: Get Charmcraft SHA
if: env.charmcraft_repository != ''
id: charmcraft-sha
working-directory: ./charmcraft
run: echo "sha=$(git rev-parse HEAD)" >> $GITHUB_OUTPUT
- name: Restore Charmcraft Cache
if: env.charmcraft_repository != ''
id: restore-charmcraft
uses: actions/cache/restore@v4
with:
path: ./charmcraft*.snap
key: charmcraft-${{ steps.charmcraft-sha.outputs.sha }}
- name: Install Snapcraft
if: steps.restore-charmcraft.outputs.cache-hit != 'true' && env.charmcraft_repository != ''
run: sudo snap install snapcraft --classic
- name: Build Charmcraft
if: steps.restore-charmcraft.outputs.cache-hit != 'true' && env.charmcraft_repository != ''
working-directory: ./charmcraft
run: |
snapcraft --use-lxd
cp charmcraft*.snap ../
- name: Save Charmcraft Cache
uses: actions/cache/save@v4
if: steps.restore-charmcraft.outputs.cache-hit != 'true' && env.charmcraft_repository != ''
with:
path: ./charmcraft*.snap
key: ${{ steps.restore-charmcraft.outputs.cache-primary-key }}
- name: Install charmcraft
if: env.charmcraft_repository != ''
run: sudo snap install --dangerous --classic charmcraft*.snap
- name: Clean up
if: env.charmcraft_repository != ''
run: rm -rf *
- name: Install charmcraft
if: env.charmcraft_repository == ''
run: |
sudo snap install charmcraft --classic --channel ${{ env.charmcraft_channel }}
- uses: actions/[email protected]
- if: env.charm_working_directory != ''
name: Change directory
run: |
set -x
ls
TEMP_DIR=$(mktemp -d)
cp -rp ./${{env.charm_working_directory}}/. $TEMP_DIR
rm -rf .* * || :
cp -rp $TEMP_DIR/. .
rm -rf $TEMP_DIR
ls
- name: Get charm name
id: get-charm-name
run: |
set -x
CHARM_NAME="$([ -f metadata.yaml ] && yq '.name' metadata.yaml || echo UNKNOWN)"
if [ "$CHARM_NAME" == "UNKNOWN" ]; then
CHARM_NAME="$([ -f charmcraft.yaml ] && yq '.name' charmcraft.yaml || echo UNKNOWN)"
fi
echo "CHARM_NAME=$CHARM_NAME">> $GITHUB_ENV
- name: Download charm artifact
# TODO JAVI TEST if: ${{ github.event_name == 'push' }}
run: |
gh run download ${{ needs.get-run-id.outputs.run-id }} -R ${{ github.repository }} -n ${{ env.CHARM_NAME }}-charm
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Download charm artifact (for testing)
uses: actions/download-artifact@v4
# TODO JAVI TEST if: ${{ github.event_name == 'pull_request' }}
if: false
with:
name: ${{ env.CHARM_NAME }}-charm
path: ${{ env.working_directory }}
- name: Get charm file
id: get-charm-file
run: |
set -x
echo charm_name: ${{ env.CHARM_NAME }}
echo "CHARM_FILE=$(ls ${{ env.CHARM_NAME }}_*.charm)"
echo "CHARM_FILE=$(ls ${{ env.CHARM_NAME }}_*.charm)" >> $GITHUB_OUTPUT
- name: Upload charm to charmhub
uses: canonical/charming-actions/[email protected]
with:
built-charm-path: ${{ env.CHARM_FILE }}
credentials: ${{ secrets.CHARMHUB_TOKEN }}
github-token: ${{ secrets.GITHUB_TOKEN }}
channel: ${{ inputs.channel }}
tag-prefix: ${{ inputs.tag-prefix }}
upload-image: false