-
Notifications
You must be signed in to change notification settings - Fork 0
211 lines (174 loc) · 8.3 KB
/
sync-docs-receiver.yml
File metadata and controls
211 lines (174 loc) · 8.3 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
name: Sync Documentation Receiver
on:
repository_dispatch:
types: [sync-documentation]
permissions:
contents: write
pull-requests: write
jobs:
sync-documentation:
name: Sync Documentation
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
with:
persist-credentials: false
- name: Validate input
id: validate
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
with:
script: |
const payload = context.payload.client_payload || {};
const required = ['artifact-id', 'repository', 'run-id'];
const missing = required.filter(field => !payload[field]);
if (missing.length) {
return core.setFailed(`Missing required inputs: ${missing.join(', ')}`);
}
function sanitizeSegment(segment) {
return segment
.replace(/^[.\s]+/, '')
.replace(/[^a-zA-Z0-9._-]/g, '-')
.replace(/-+/g, '-')
.replace(/^-|-$/g, '')
.toLowerCase();
}
const repoName = payload.repository.split('/')[1] || payload.repository;
const sanitizedRepoName = sanitizeSegment(repoName);
if(!sanitizedRepoName) {
return core.setFailed(`Unable to sanitize repository ${repoName} name for documentation path.`);
}
const docsPath = `application/docs/projects/${sanitizedRepoName}`;
const staticPath = `application/static/${sanitizedRepoName}`;
core.debug(`Sanitized docs path: ${docsPath}`);
core.setOutput('docs-path', docsPath);
core.debug(`Static asset path: ${staticPath}`);
core.setOutput('static-path', staticPath);
core.debug(`Repository: ${payload.repository}`);
core.setOutput('repository', payload.repository);
core.debug(`Run ID: ${payload['run-id']}`);
core.setOutput('run-id', payload['run-id']);
core.debug(`Artifact ID: ${payload['artifact-id']}`);
core.setOutput('artifact-id', payload['artifact-id']);
- name: Download documentation artifact
id: download-artifact
uses: actions/download-artifact@018cc2cf5baa6db3ef3c5f8a56943fffe632ef53 # v6.0.0
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
artifact-ids: ${{ steps.validate.outputs['artifact-id'] }}
path: ${{ runner.temp }}/documentation-download-${{ github.run_id }}
repository: ${{ steps.validate.outputs['repository'] }}
run-id: ${{ steps.validate.outputs['run-id'] }}
- name: Prepare documentation content
id: prepare-docs
uses: ./.github/actions/prepare-docs
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
artifact-path: ${{ steps.download-artifact.outputs.download-path }}
source-repository: ${{ steps.validate.outputs['repository'] }}
run-id: ${{ steps.validate.outputs['run-id'] }}
docs-path: ${{ steps.validate.outputs['docs-path'] }}
static-path: ${{ steps.validate.outputs['static-path'] }}
- name: Inject documentation
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
env:
SOURCE_REPOSITORY: ${{ steps.validate.outputs['repository'] }}
DOCS_PATH: ${{ steps.validate.outputs['docs-path'] }}
STATIC_PATH: ${{ steps.validate.outputs['static-path'] }}
PREPARED_DIR: ${{ steps.prepare-docs.outputs.output-path }}
with:
script: |
const fs = require('fs');
const path = require('path');
const sourceRepository = process.env.SOURCE_REPOSITORY;
const docsPath = process.env.DOCS_PATH;
const staticPath = process.env.STATIC_PATH;
const preparedDir = process.env.PREPARED_DIR;
if (!sourceRepository) {
return core.setFailed('Missing source repository from prepare-docs step.');
}
if (!docsPath) {
return core.setFailed('Missing docs path from prepare-docs step.');
}
if (!staticPath) {
return core.setFailed('Missing static path from prepare-docs step.');
}
if (!preparedDir) {
return core.setFailed('Missing prepared directory configuration.');
}
core.info(`Starting documentation injection from ${sourceRepository}`);
if (!fs.existsSync(preparedDir)) {
return core.setFailed('Prepared documentation directory not found.');
}
await io.rmRF(docsPath);
await io.mkdirP(path.dirname(docsPath));
await io.cp(preparedDir, docsPath, { recursive: true });
// Move static assets if present
const staticAssetsSrc = path.join(docsPath, 'static');
if (fs.existsSync(staticAssetsSrc)) {
await io.rmRF(staticPath);
await io.mkdirP(path.dirname(staticPath));
await io.mv(staticAssetsSrc, staticPath);
}
core.info(`Documentation injected into ${docsPath}`);
- name: Generate Documentation
uses: ./.github/actions/generate-docs
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
- uses: actions/create-github-app-token@67018539274d69449ef7c02e8e71183d1719ab42 # v2.1.4
id: generate_token
with:
app-id: ${{ vars.CI_BOT_APP_ID }}
private-key: ${{ secrets.CI_BOT_APP_PRIVATE_KEY }}
- uses: hoverkraft-tech/ci-github-common/actions/create-and-merge-pull-request@5f11437c716059f30c635f90055060e4ef8b31a0 # 0.28.0
with:
github-token: ${{ steps.generate_token.outputs.token }}
branch: docs/sync-documentation-${{ github.event.client_payload.repository }}
title: "docs(${{ github.event.client_payload.repository }}): update documentation"
body: Update documentation for ${{ github.event.client_payload.repository }}
commit-message: |
docs(${{ github.event.client_payload.repository }}): update documentation
- name: Summary
if: always()
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
env:
STATUS: ${{ job.status }}
SOURCE_REPOSITORY: ${{ steps.validate.outputs['repository'] }}
ARTIFACT_ID: ${{ steps.validate.outputs['artifact-id'] }}
DOCS_PATH: ${{ steps.validate.outputs['docs-path'] }}
STATIC_PATH: ${{ steps.validate.outputs['static-path'] }}
SOURCE_BRANCH: ${{ steps.prepare-docs.outputs.source-branch }}
PROCESSED_FILES: ${{ steps.prepare-docs.outputs.processed-files }}
with:
script: |
const files = process.env.PROCESSED_FILES ? JSON.parse(process.env.PROCESSED_FILES) : [];
const statusText = process.env.STATUS === 'success'
? 'Documentation committed to public-docs'
: 'Error occurred during documentation sync';
const filesSummary = files.length
? `Processed files:\n${files.join('\n')}`
: 'No files were processed.';
const summaryBuilder = core.summary
.addHeading('Documentation Sync Summary', 2)
.addRaw('\n')
.addList([
`Source Repository: ${process.env.SOURCE_REPOSITORY}`,
`Source Branch: ${process.env.SOURCE_BRANCH}`,
`Docs Path: ${process.env.DOCS_PATH}`,
`Static Path: ${process.env.STATIC_PATH}`,
`Processed file(s): ${files.length}`,
`Artifact: ${process.env.ARTIFACT_ID}`,
`Status: ${statusText}`
])
.addRaw('\n');
if (files.length > 0) {
summaryBuilder
.addRaw("Processed files:")
.addList(files);
} else {
summaryBuilder.addRaw("Processed files: no files were processed.");
}
summaryBuilder
.addRaw('\n\n')
.addRaw('Build and deployment will be handled by the push to main workflow.');
await summaryBuilder.write();