-
Notifications
You must be signed in to change notification settings - Fork 49
213 lines (189 loc) · 9.24 KB
/
Copy pathDocumentation.yaml
File metadata and controls
213 lines (189 loc) · 9.24 KB
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
name: Documentation
run-name: 'Documentation - ${{ inputs.oci-image-name }} - ${{ github.ref }}'
on:
push:
paths:
- "oci/*/documentation.y*ml"
branches:
- main
workflow_dispatch:
inputs:
oci-image-name:
description: 'OCI image to generate the documentation for'
required: true
external_ref_id: # (1)
description: 'Optional ID for unique run detection'
required: false
type: string
default: "default-id"
workflow_call:
inputs:
oci-image-name:
description: 'OCI image to generate the documentation for'
required: true
type: string
# Dry-run on PRs to generate a preview of the compiled documentation.
pull_request:
paths:
- "oci/*/documentation.y*ml"
jobs:
validate-documentation-request:
runs-on: ubuntu-22.04
name: Validate documentation request
outputs:
oci-img-path: ${{ steps.validate-image.outputs.img-path }}
oci-img-name: ${{ steps.validate-image.outputs.img-name }}
steps:
- name: ${{ inputs.external_ref_id }} # (2)
if: ${{ github.event_name == 'workflow_dispatch' }}
run: echo 'Started by ${{ inputs.external_ref_id }}' >> "$GITHUB_STEP_SUMMARY"
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd
- name: Validate access to triggered image
uses: ./.github/actions/validate-actor
if: ${{ github.repository == 'canonical/oci-factory' && !github.event.pull_request.head.repo.fork }}
with:
admin-only: true
image-path: "oci/${{ inputs.oci-image-name }}"
github-token: ${{ secrets.ROCKSBOT_TOKEN }}
- name: Infer images to document
uses: tj-actions/changed-files@22103cc46bda19c2b464ffe86db46df6922fd323
id: changed-files
if: github.event_name != 'workflow_dispatch' && github.event_name != 'workflow_call'
with:
dir_names: "true"
separator: ","
files: |
oci/*/documentation.y*ml
- name: Validate image from dispatch
id: validate-image
run: |
set -e
if [[ "$RUNNER_DEBUG" == "1" ]]; then
set -x
fi
source src/shared/logs.sh
# check if this is coming from a workflow dispatch/call
# as checking github.event_name isn't reliable here
if [ "${{ inputs.oci-image-name }}" != "" ]
then
img_path="oci/${{ inputs.oci-image-name }}"
else
img_path="${{ steps.changed-files.outputs.all_changed_files }}"
occurrences="${img_path//[^,]}"
if [ ${#occurrences} -ne 0 ]
then
log_error "can only build documentation for 1 image at a time, but trying to document ${img_path}"
exit 1
fi
fi
test -d "${img_path}"
echo "img-name=$(basename ${img_path})" >> "$GITHUB_OUTPUT"
echo "img-path=${img_path}" >> "$GITHUB_OUTPUT"
do-documentation:
runs-on: ubuntu-22.04
name: Documentation
needs: [validate-documentation-request]
env:
IS_PROD: ${{ ! startsWith(needs.validate-documentation-request.outputs.oci-img-name, 'mock-') }}
steps:
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd
# This is needed to ensure that this workflow can run when called by workflows in external repositories
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd
with:
repository: canonical/oci-factory
path: oci-factory
- name: Fetch _releases.json
uses: ./oci-factory/.github/actions/fetch-releases-json
with:
image-name: ${{ needs.validate-documentation-request.outputs.oci-img-name }}
- uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405
with:
python-version: "3.x"
- name: Setup environment
run: pip install -r ./src/image/requirements.txt -r ./src/docs/requirements.txt
- name: Generate documentation for ${{ needs.validate-documentation-request.outputs.oci-img-name }}
id: generate-documentation
env:
ECR_CREDS_USR: ${{ env.IS_PROD == 'true' && secrets.ECR_CREDS_USR || secrets.ECR_CREDS_USR_DEV }}
ECR_CREDS_PSW: ${{ env.IS_PROD == 'true' && secrets.ECR_CREDS_PSW || secrets.ECR_CREDS_PSW_DEV }}
ECR_NAMESPACE: ${{ env.IS_PROD == 'true' && 'ubuntu' || secrets.ECR_NAMESPACE_DEV }}
DRY_RUN_FLAG: ${{ github.event_name == 'pull_request' && '--dry-run' || '' }}
run: |
set -e
if [[ "$RUNNER_DEBUG" == "1" ]]; then
set -x
fi
python3 -m src.docs.generate_oci_doc_yaml \
--ecr-api-key "${ECR_CREDS_USR}" \
--ecr-api-secret "${ECR_CREDS_PSW}" \
--oci-image-path "${{ needs.validate-documentation-request.outputs.oci-img-path }}" \
--repository "${ECR_NAMESPACE}" \
--doc-data-dir data \
${DRY_RUN_FLAG}
- name: Upload documentation data YAML
uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f
with:
name: '${{ needs.validate-documentation-request.outputs.oci-img-name }}-doc-data'
path: "${{ steps.generate-documentation.outputs.image_doc_folder }}/*.y*ml"
if-no-files-found: error
- name: Publish Documentation for ${{ needs.validate-documentation-request.outputs.oci-img-name }}
env:
# In Dev, images go into the personal space of DOCKER_HUB_CREDS_USR_DEV
# so we can reuse its username (with a more privileged PAT) for updating the docs.
# In Prod, however, since it's an org, the collaborating DH account is different to
# segregate access and ensure other jobs in this pipeline don't abuse the privileged
# access that is needed here, for updating the docs.
DOCKER_HUB_CREDS_PSW_DOC: ${{ env.IS_PROD == 'true' && secrets.DOCKER_HUB_CREDS_PSW_DOC || secrets.DOCKER_HUB_CREDS_PSW_DOC_DEV }}
DOCKER_HUB_CREDS_USR_DOC: ${{ env.IS_PROD == 'true' && secrets.DOCKER_HUB_CREDS_USR_DOC || secrets.DOCKER_HUB_CREDS_USR_DEV }}
ECR_CREDS_USR: ${{ env.IS_PROD == 'true' && secrets.ECR_CREDS_USR || secrets.ECR_CREDS_USR_DEV }}
ECR_CREDS_PSW: ${{ env.IS_PROD == 'true' && secrets.ECR_CREDS_PSW || secrets.ECR_CREDS_PSW_DEV }}
ECR_REGISTRY_ID: ${{ env.IS_PROD == 'true' && secrets.ECR_REGISTRY_ID || secrets.ECR_REGISTRY_ID_DEV }}
DOCKER_HUB_NAMESPACE: ${{ env.IS_PROD == 'true' && 'docker.io/ubuntu' || secrets.DOCKER_HUB_NAMESPACE_DEV }}
DRY_RUN: ${{ github.event_name == 'pull_request' && 'true' || 'false' }}
run: |
set -e
if [[ "$RUNNER_DEBUG" == "1" ]]; then
set -x
fi
./src/docs/publish_docs.sh "${{ needs.validate-documentation-request.outputs.oci-img-name }}" "${{ steps.generate-documentation.outputs.name_doc_file }}" "${{ steps.generate-documentation.outputs.image_doc_folder }}"
- name: Write documentation preview to summary
if: ${{ github.event_name == 'pull_request' }}
run: |
echo "# Preview for ${{ needs.validate-documentation-request.outputs.oci-img-name }}" >> "$GITHUB_STEP_SUMMARY"
cat "${{ steps.generate-documentation.outputs.image_doc_folder }}/doc-preview.md" >> "$GITHUB_STEP_SUMMARY"
notify:
runs-on: ubuntu-22.04
name: Notify on failure
needs: [validate-documentation-request, do-documentation]
if: ${{ !cancelled() && contains(needs.*.result, 'failure') && github.event_name != 'workflow_dispatch' && github.event_name != 'pull_request' }}
steps:
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd
- uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405
with:
python-version: '3.x'
- run: pip install -r src/notifications/requirements.txt
- name: Summarize workflow failure message
id: get-summary
run: |
echo '${{ toJson(needs) }}' > jobs.json
python3 -m src.notifications.mattermost_notifier summarize jobs.json
- name: Get contacts for ${{ needs.validate-documentation-request.outputs.oci-img-name }}
id: get-contacts
working-directory: ${{ needs.validate-documentation-request.outputs.oci-img-path }}
run: |
mm_channels=$(yq -r '.notify | ."mattermost-channels" | join(",")' < contacts.y*ml)
echo "mattermost-channels=${mm_channels}" >> "$GITHUB_OUTPUT"
- name: Notify via Mattermost
env:
MM_BOT_TOKEN: ${{ secrets.MM_BOT_TOKEN }}
FINAL_STATUS: failure
MM_SERVER: ${{ secrets.MM_SERVER }}
URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
SUMMARY: ${{ steps.get-summary.outputs.summary }}
FOOTER: "Triggered by ${{ github.triggering_actor }}. Ref: ${{ github.ref }}. Attempts: ${{ github.run_attempt }}"
TITLE: '${{ needs.validate-documentation-request.outputs.oci-img-name }}: failed to generate docs'
run: |
for channel in $(echo ${{ steps.get-contacts.outputs.mattermost-channels }} | tr ',' ' ')
do
MM_CHANNEL_ID="${channel}" python3 -m src.notifications.mattermost_notifier send
done