-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
242 lines (230 loc) · 10.2 KB
/
on-workflow-run.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
name: on workflow run
on:
workflow_run:
workflows: ['on pull request']
types:
- completed
env:
RUN_NUMBER: ${{ github.run_number }}
jobs:
# Stop previous runs
stop-previous-run:
runs-on: ubuntu-22.04
steps:
- name: Cancel Previous Runs
uses: styfle/[email protected]
with:
access_token: ${{ secrets.GITHUB_TOKEN }}
check-if-allure-artifacts-exist:
runs-on: ubuntu-22.04
needs: stop-previous-run
outputs:
artifacts-exist: ${{ steps.check-artifacts-exist.outputs.artifacts-exist }}
steps:
- name: Check if allure artifacts exist
id: check-artifacts-exist
uses: actions/github-script@v6
with:
script: |
const artifacts = await github.rest.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: ${{github.event.workflow_run.id }}
});
const matchArtifact = artifacts.data.artifacts.find(artifact => artifact.name === 'allure-results');
if (!matchArtifact) {
core.setOutput('artifacts-exist', 'false');
} else {
core.setOutput('artifacts-exist', 'true');
}
generate-allure-report:
runs-on: ubuntu-22.04
needs: check-if-allure-artifacts-exist
if: ${{ needs.check-if-allure-artifacts-exist.outputs.artifacts-exist != 'false' }}
steps:
- name: Dowload artifacts
uses: actions/github-script@v6
with:
script: |
const artifacts = await github.rest.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: ${{github.event.workflow_run.id }}
});
const matchArtifact = artifacts.data.artifacts.find(artifact => artifact.name === 'allure-results');
if (!matchArtifact) {
throw new Error('Artifact not found');
}
const download = await github.rest.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: matchArtifact.id,
archive_format: 'zip'
});
const fs = require('fs');
fs.writeFileSync('${{github.workspace}}/allure-results.zip', Buffer.from(download.data));
- run: unzip allure-results.zip
- name: Get Allure history
uses: actions/checkout@v3
if: always()
continue-on-error: true
with:
ref: gh-pages
path: gh-pages
- name: Allure Report action from marketplace
uses: simple-elf/allure-report-action@master
if: always()
with:
allure_results: ./
allure_history: allure-history
keep_reports: 30
- name: Deploy report to Github Pages
if: always()
uses: peaceiris/actions-gh-pages@v2
env:
PERSONAL_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PUBLISH_BRANCH: gh-pages
PUBLISH_DIR: allure-history
# Comment to PR, Add labels to PR, Remove unnecessary labels
actions-with-pr:
runs-on: ubuntu-22.04
needs: generate-allure-report
if: always() && github.event.workflow_run.event == 'pull_request'
steps:
- name: 'Download artifact'
uses: actions/github-script@v6
with:
script: |
const artifacts = await github.rest.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: ${{github.event.workflow_run.id }}
});
const matchArtifact = artifacts.data.artifacts.find(artifact => artifact.name === 'env_for_comment');
if (!matchArtifact) {
throw new Error('Artifact not found');
}
const download = await github.rest.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: matchArtifact.id,
archive_format: 'zip'
});
const fs = require('fs');
fs.writeFileSync('${{github.workspace}}/env_for_comment.zip', Buffer.from(download.data));
- run: |
unzip env_for_comment.zip
echo ${{ needs.generate-allure-report.result }}
- name: 'Comment to PR -- if report generated'
if: needs.generate-allure-report.result == 'success'
uses: actions/github-script@v6
env:
WORKFLOW_CONCLUSION: ${{ github.event.workflow_run.conclusion }}
with:
script: |
const fs = require('fs');
const issue_number = Number(fs.readFileSync('./prNum'));
const runNumber = process.env.RUN_NUMBER;
const conclusion = process.env.WORKFLOW_CONCLUSION;
const runId = Number(fs.readFileSync('./runId'));
const repoFullName = fs.readFileSync('./repoFullName');
const ownerName = fs.readFileSync('./ownerName');
const githubPagesLink = 'https://' + ownerName.toString().toLowerCase().trim() + '.github.io/' + String(repoFullName).replace(`${ownerName.toString().trim()}/`, '') + '/';
const githubRunLink = 'https://github.com/' + repoFullName + '/actions/runs/' + runId;
await github.rest.issues.createComment({
issue_number: issue_number,
owner: context.repo.owner,
repo: context.repo.repo,
body: `Workflow status is ${conclusion === 'success'? conclusion + ' \u2705' : conclusion + ' \u274C'} \n Checkout tests results here: <a href="${githubPagesLink}${runNumber}">Cypress e2e Test Report</a> \n Link to GitHub workflow: <a href="${githubRunLink}">Github Workflow Link</a> \n❕Note: Report deployment may take up to 10 minutes.`
})
- name: 'Comment to PR -- if report wasnt generated'
if: needs.generate-allure-report.result == 'skipped'
uses: actions/github-script@v6
env:
WORKFLOW_CONCLUSION: ${{ github.event.workflow_run.conclusion }}
with:
script: |
const fs = require('fs');
const issue_number = Number(fs.readFileSync('./prNum'));
const runNumber = process.env.RUN_NUMBER;
const conclusion = process.env.WORKFLOW_CONCLUSION;
const runId = Number(fs.readFileSync('./runId'));
const repoFullName = fs.readFileSync('./repoFullName');
const ownerName = fs.readFileSync('./ownerName');
const githubPagesLink = 'https://' + ownerName.toString().toLowerCase().trim() + '.github.io/' + String(repoFullName).replace(`${ownerName.toString().trim()}/`, '') + '/';
const githubRunLink = 'https://github.com/' + repoFullName + '/actions/runs/' + runId;
await github.rest.issues.createComment({
issue_number: issue_number,
owner: context.repo.owner,
repo: context.repo.repo,
body: `Workflow status is ${conclusion === 'success'? conclusion + ' \u2705' : conclusion === 'cancelled'? conclusion + ' \u2757' : conclusion + ' \u274C'} \n Unfortunately test report wasn't generated, it can be if no workspaces changed. Please check the workflow run below. \n Link to GitHub workflow: <a href="${githubRunLink}">Github Workflow Link</a>`
})
- name: Remove old lables
if: always()
uses: actions/github-script@v6
env:
WORKFLOW_CONCLUSION: ${{ github.event.workflow_run.conclusion }}
GENERAT_ALLURE_REPORT_STATUS: ${{ needs.generate-allure-report.result }}
with:
script: |
const fs = require('fs');
const issue_number = Number(fs.readFileSync('./prNum'));
const conclusion = process.env.WORKFLOW_CONCLUSION;
const generateAllureReportStatus = process.env.GENERAT_ALLURE_REPORT_STATUS;
const labels = await github.rest.issues.listLabelsOnIssue({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issue_number
});
if (labels) {
for (const label of labels.data) {
if ((label.name == `e2e-${conclusion}` && generateAllureReportStatus == 'success') || label.name == `workflow-${conclusion}`) {
console.log(`Label ${label.name} already exists`);
continue;
}
if (label.name.startsWith("workflow-") || label.name.startsWith("e2e-")) {
await github.rest.issues.removeLabel({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issue_number,
name: label.name
});
}
}
} else {
console.log('No labels found');
}
- name: 'Add e2e label to PR -- if report generated'
if: needs.generate-allure-report.result == 'success'
uses: actions/github-script@v6
env:
WORKFLOW_CONCLUSION: ${{ github.event.workflow_run.conclusion }}
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const fs = require('fs');
const issue_number = Number(fs.readFileSync('./prNum'));
const conclusion = process.env.WORKFLOW_CONCLUSION;
await github.rest.issues.addLabels({
issue_number: issue_number,
owner: context.repo.owner,
repo: context.repo.repo,
labels: [`e2e-${conclusion}`]
})
- name: 'Add workflow label to PR'
if: always()
uses: actions/github-script@v6
env:
WORKFLOW_CONCLUSION: ${{ github.event.workflow_run.conclusion }}
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const fs = require('fs');
const issue_number = Number(fs.readFileSync('./prNum'));
const conclusion = process.env.WORKFLOW_CONCLUSION;
await github.rest.issues.addLabels({
issue_number: issue_number,
owner: context.repo.owner,
repo: context.repo.repo,
labels: [`workflow-${conclusion}`]
})