-
Notifications
You must be signed in to change notification settings - Fork 15
319 lines (284 loc) · 12.1 KB
/
monorepo.yml
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
name: Monorepo Versioning
concurrency: #avoid concurrent runs on label events, might cause issues on super fast commits ¯\_(ツ)_/¯
group: ${{ github.head_ref }}
cancel-in-progress: true
on:
pull_request:
types: [opened, synchronize, reopened, labeled, unlabeled]
pull_request_target:
types: [closed]
jobs:
detect:
runs-on: ubuntu-latest
name: 'Detect pull request context'
outputs:
directories: ${{ steps.condense.outputs.result }}
release-type: ${{ steps.check_pr_label.outputs.release-type}}
is-merge-event: >-
${{ github.event_name == 'pull_request_target'
&& github.event.action == 'closed'
&& github.event.pull_request.merged == true }}
steps:
# I'm getting the labels from the API and not the context("contains(github.event.pull_request.labels.*.name, 'Env Promote')") as the labels
# are added in 2nd API call so they aren't included in the PR context
- name: Check PR labels
id: check_pr_label
env:
PR_URL: ${{github.event.pull_request.html_url}} #TODO: check if needed (we don't checkout the code..)
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
run: |
LABELS=$(gh pr view $PR_URL --json labels --jq '.labels[]| select((.name=="minor") or (.name=="major") or (.name=="patch") or (.name=="no-release")) |.name')
NUMBER_OF_LABELS=$(echo "$LABELS" |wc -w)
if [ "$NUMBER_OF_LABELS" -eq "1" ] ; then
echo "Found: $LABELS"
echo "::set-output name=release-type::$LABELS"
elif [ "$NUMBER_OF_LABELS" -gt "1" ] ; then
echo "::error ::Too many release type labels: $( echo $LABELS | tr '\n' ' ' )"
exit 1
else
echo "::error ::No release type labels found(patch/minor/major/no-release)"
exit 2
fi
- name: Get changed files
uses: Ana06/get-changed-files@v1.2
id: raw-files
with:
format: 'json'
- name: Condense to directory list
uses: actions/github-script@v4
id: condense
env:
RAW_FILES: '${{ steps.raw-files.outputs.all }}'
with:
script: |
const raw = JSON.parse(process.env.RAW_FILES);
const directories = Array.from(new Set(raw
.filter(x => !x.startsWith('.'))
.filter(x => x.includes('/'))
.map(x => x.split('/')[0])
));
if (directories.length < 1) return {};
return {
include: directories.map(directory => ({ directory })),
};
plan:
needs: detect
if: ${{ needs.detect.outputs.directories != '{}' }}
runs-on: ubuntu-latest
strategy:
matrix: "${{ fromJson(needs.detect.outputs.directories) }}"
fail-fast: false
name: 'Module: ${{ matrix.directory }}'
steps:
- name: Detect previous version number
uses: actions/github-script@v4
id: prev-version
env:
PACKAGE_NAME: '${{ matrix.directory }}'
with:
script: |
const { owner, repo } = context.repo;
const query = `query($owner: String!, $repo: String!, $refPrefix: String!) {
repository(owner: $owner, name: $repo) {
refs(refPrefix: $refPrefix, first: 1, orderBy: {field: TAG_COMMIT_DATE, direction: DESC}) {
edges { node { name } }
}
}
}`;
const result = await github.graphql(query, { owner, repo,
refPrefix: `refs/tags/rel/${process.env.PACKAGE_NAME}/`,
});
const prevNode = result.repository.refs.edges[0];
const prevVer = prevNode ? prevNode.node.name : '0.0.0';
console.log('Found previous version', prevVer);
return prevVer;
result-encoding: string
- name: Determine new version number
uses: actions/github-script@v4
id: new-version
env:
PREV_VERSION: '${{ steps.prev-version.outputs.result }}'
RELEASE_TYPE: '${{ needs.detect.outputs.release-type }}'
with:
script: |
const { PREV_VERSION, RELEASE_TYPE } = process.env;
console.log('Previous version was', PREV_VERSION);
console.log('Release type is', RELEASE_TYPE);
const numbers = PREV_VERSION.split('.');
const numberIdx = ['major', 'minor', 'patch'].indexOf(RELEASE_TYPE);
numbers[numberIdx] = parseInt(numbers[numberIdx]) + 1;
for (let i = numberIdx + 1; i < numbers.length; i++) {
numbers[i] = 0;
}
return numbers.join('.');
result-encoding: string
- name: Store version numbers
env:
PR_URL: ${{github.event.pull_request.html_url}}
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
run: |
mkdir output
echo '${{ steps.prev-version.outputs.result }}' > output/previous-version.txt
echo '${{ steps.new-version.outputs.result }}' > output/new-version.txt
- name: Extract changelog entry
uses: actions/github-script@v5
with:
script: |
const { owner, repo } = context.repo;
const { data: prInfo } = await github.rest.pulls.get({
owner, repo,
pull_number: context.issue.number,
});
console.log('Found PR body:|');
console.log(prInfo.body);
const changelogEntry = ((prInfo.body
.split(/^#+ ?/m)
.find(x => x.startsWith('Changelog'))
|| '').split(/^```/m)[1] || '').trim();
if (!changelogEntry)
throw `'Changelog' section not found in PR body! Please add it back.`;
if (changelogEntry.match(/^TODO:/m))
throw `'Changelog' section needs proper text, instead of 'TODO'`;
const { writeFile } = require('fs').promises;
await writeFile('output/changelog.md', `
* PR [#${ prInfo.number }](${ prInfo.html_url }) - ${ prInfo.title }
\`\`\`
${changelogEntry}
\`\`\`
`.trimLeft(), { encoding: 'utf-8' })
- name: Document example 'source' line
run: |
cat > output/documentation.md <<'EOF'
# Module Location
To use this module in your Terraform, use the below source value.
```hcl
module "my_${{ matrix.directory }}" {
source = "s3::https://forto-terrform-modules-demo.s3.eu-west-1.amazonaws.com/${{ matrix.directory }}-${{ steps.new-version.outputs.result }}.zip"
# also any inputs for the module (see below)
}
```
EOF
- name: Install terraform docs
run: |
wget https://github.com/terraform-docs/terraform-docs/releases/download/v0.15.0/terraform-docs-v0.15.0-linux-amd64.tar.gz \
--output-document - \
--progress dot:mega \
| tar -xvz
- name: Clone repository for analysis
uses: actions/checkout@v2
with:
ref: ${{ github.sha }}
path: src
- name: Render terraform docs
run: |
echo '# Module Attributes' >> output/documentation.md
./terraform-docs markdown table \
--output-file "$(pwd)"/output/documentation.md \
--sort-by required \
'src/${{ matrix.directory }}'
- name: Bundle up Terraform module
working-directory: src/${{ matrix.directory }}
run: zip -vr ../../output/terraform-module.zip *
- name: Upload result artifacts
uses: actions/upload-artifact@v2
with:
name: '${{ matrix.directory }}'
path: output
retention-days: 5
comment:
needs: [detect, plan]
if: needs.detect.outputs.is-merge-event == 'false'
runs-on: ubuntu-latest
name: 'Comment on PR'
steps:
- uses: actions/download-artifact@v4.1.7
with:
path: outputs
- name: Display structure of downloaded files
run: ls -R
working-directory: outputs
- uses: actions/github-script@v2
with:
script: |
const { owner, repo } = context.repo;
const { number: issue_number } = context.issue;
const { readdir, readFile } = require('fs').promises;
const utf8 = { encoding: 'utf-8' };
const lines = [
'# Release plan', '',
'| Directory | Previous version | New version |',
'|--|--|--|',
];
const sections = [];
for (const folder of await readdir('outputs', { withFileTypes: true })) {
if (!folder.isDirectory()) continue;
const readText = (name) => readFile(name, utf8).then(x => x.trim());
lines.push('| '+[
`\`${folder.name}\``,
`${await readText(`outputs/${folder.name}/previous-version.txt`)}`,
`**${await readText(`outputs/${folder.name}/new-version.txt`)}**`,
].join(' | ')+' |');
sections.push(`<details><summary>Changelog preview: ${folder.name}</summary>\n\n${await readText(`outputs/${folder.name}/changelog.md`)}\n</details>`);
}
const finalBody = [lines.join('\n'), ...sections].join('\n\n');
const {data: allComments} = await github.issues.listComments({ issue_number, owner, repo });
const ourComments = allComments
.filter(comment => comment.user.login === 'github-actions[bot]')
.filter(comment => comment.body.startsWith(lines[0]+'\n'));
const latestComment = ourComments.slice(-1)[0];
if (latestComment && latestComment.body === finalBody) {
console.log('Existing comment is already up to date.');
return;
}
const {data: newComment} = await github.issues.createComment({ issue_number, owner, repo, body: finalBody });
console.log('Posted comment', newComment.id, '@', newComment.html_url);
// Delete all our previous comments
for (const comment of ourComments) {
if (comment.id === newComment.id) continue;
console.log('Deleting previous PR comment from', comment.created_at);
await github.issues.deleteComment({ comment_id: comment.id, owner, repo });
}
trigger-release:
needs: [detect, plan]
if: needs.detect.outputs.is-merge-event == 'true'
runs-on: ubuntu-latest
name: 'Dispatch release event'
steps:
- uses: actions/download-artifact@v4.1.7
with:
path: outputs
- name: Combine version information
id: extract-releases
uses: actions/github-script@v2
with:
script: |
const { readdir, readFile } = require('fs').promises;
const utf8 = { encoding: 'utf-8' };
const readText = (name) => readFile(name, utf8).then(x => x.trim());
const directories = await readdir('outputs', { withFileTypes: true });
return await Promise.all(directories
.filter(x => x.isDirectory())
.map(async folder => ({
module: folder.name,
prevVersion: await readText(`outputs/${folder.name}/previous-version.txt`),
newVersion: await readText(`outputs/${folder.name}/new-version.txt`),
})));
- name: Dispatch monorepo_release event
uses: actions/github-script@v2
env:
RELEASE_LIST: '${{ steps.extract-releases.outputs.result }}'
with:
github-token: ${{ secrets.GH_PAT_FREIGHTBOT }}
script: |
const payload = {
run_id: "${{ github.run_id }}",
sha: context.sha,
releases: JSON.parse(process.env.RELEASE_LIST),
};
console.log('Event payload:', JSON.stringify(payload, null, 2));
const { owner, repo } = context.repo;
await github.repos.createDispatchEvent({
owner, repo,
event_type: 'monorepo_release',
client_payload: payload,
});