-
Notifications
You must be signed in to change notification settings - Fork 2
326 lines (273 loc) · 9.97 KB
/
test.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
320
321
322
323
324
325
326
name: Python CI
on:
push:
branches: [ master ]
paths-ignore:
# Can we use '**.md' ?
- 'README.md'
- 'SECURITY.md'
- 'CONTRIBUTING.md'
- 'CODE_OF_CONDUCT.md'
- 'LICENSE'
- '.gitignore'
pull_request:
branches: [ master ]
jobs:
pre_ci:
name: Prepare CI environment
runs-on: ubuntu-latest
outputs:
commit_message: ${{ steps.get_commit_message.outputs.commit_message }}
branch: ${{ steps.extract_branch.outputs.branch }}
steps:
- name: Checkout Project
uses: actions/checkout@v3
with:
# We need to fetch with a depth of 2 for pull_request so we can do HEAD^2
fetch-depth: 2
- name: "Get commit message"
id: get_commit_message
env:
COMMIT_PUSH: ${{ github.event.head_commit.message }}
run: |
COMMIT_MESSAGE="${COMMIT_PUSH:-$(git log --format=%B -n 1 HEAD^2)}"
echo "::set-output name=commit_message::${COMMIT_MESSAGE}"
- name: Extract branch name
id: extract_branch
shell: bash
run: |
TMP_PULL_HEAD_REF="${{ github.head_ref }}"
TMP_GITHUB_REF="${GITHUB_REF#refs/heads/}"
EXPORT_VALUE=""
if [ "${TMP_PULL_HEAD_REF}" != "" ]
then
EXPORT_VALUE="${TMP_PULL_HEAD_REF}"
else
EXPORT_VALUE="${TMP_GITHUB_REF}"
fi
echo "##[set-output name=branch;]${EXPORT_VALUE}"
test-code:
name: Test code
runs-on: ubuntu-latest
# Map a step output to a job output
outputs:
coverage: ${{ steps.coverage.outputs.coverage }}
coverage-rounded-display: ${{ steps.coverage.outputs.coverage-rounded-display }}
steps:
- uses: actions/setup-python@v2
with:
python-version: '3.x' # Version range or exact version of a Python version to use, using SemVer's version range syntax
- uses: actions/checkout@v3
- name: Cache pip
uses: actions/cache@v3
with:
# This path is specific to Ubuntu
path: ~/.cache/pip
# Look to see if there is a cache hit for the corresponding requirements file
key: ${{ runner.os }}-pip-${{ hashFiles('requirements.txt') }}
restore-keys: |
${{ runner.os }}-pip-
${{ runner.os }}-
- name: Install dependencies
run: |
pip install -r pywisp_emibcn/requirements.txt
pip install pytest coverage
pip install .
- name: Run tests
run: |
coverage run --branch --include pywisp_emibcn/\* -m pytest
coverage xml
- name: Code Coverage Summary Report (Text & Value)
uses: irongut/CodeCoverageSummary@v1.0.5
with:
filename: coverage.xml
output: both
- name: Check code coverage
id: coverage
env:
VALUE: "Branch Rate"
run: |
COVERAGE="$( cat code-coverage-results.txt | egrep "^${VALUE}" | sed -e 's#^.* \([0-9]*\)%.*#\1#' )"
echo "##[set-output name=coverage;]${COVERAGE}"
echo "##[set-output name=coverage-rounded-display;]${COVERAGE}%"
- name: Code Coverage Summary Report (Markdown)
uses: irongut/CodeCoverageSummary@v1.0.5
if: ${{ github.event_name == 'pull_request' && github.actor == github.repository_owner }}
with:
filename: coverage.xml
output: file
format: markdown
badge: false
- name: Comment PR with code coverage
uses: machine-learning-apps/pr-comment@1.0.0
if: ${{ github.event_name == 'pull_request' && github.actor == github.repository_owner }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
path: code-coverage-results.md
test-build:
name: Test build
runs-on: ubuntu-latest
steps:
- uses: actions/setup-python@v2
with:
python-version: '3.x' # Version range or exact version of a Python version to use, using SemVer's version range syntax
- uses: actions/checkout@v3
- name: Cache pip
uses: actions/cache@v3
with:
# This path is specific to Ubuntu
path: ~/.cache/pip
# Look to see if there is a cache hit for the corresponding requirements file
key: ${{ runner.os }}-pip-${{ hashFiles('requirements.txt') }}
restore-keys: |
${{ runner.os }}-pip-
${{ runner.os }}-
- name: Install dependencies
run: |
pip install -r pywisp_emibcn/requirements.txt
pip install build
- name: Build package
run: |
python3 -m build
test:
name: Tests waiter
needs: [test-code, test-build]
runs-on: ubuntu-latest
outputs:
coverage: ${{ needs.test-code.outputs.coverage }}
coverage-rounded-display: ${{ needs.test-code.outputs.coverage-rounded-display }}
steps:
- name: Check test coverage
env:
COVERAGE: ${{ needs.test-code.outputs.coverage }}
COVERAGE_ROUNDED: ${{ needs.test-code.outputs.coverage-rounded-display }}
run: |
echo "Coverage: ${COVERAGE}"
echo "Coverage Rounded: ${COVERAGE_ROUNDED}"
badge:
# Only generate and publish if these conditions are met:
# - The test step ended successfully
# - One of these is met:
# - This is a pull request event and the pull actor is the same as the repo owner
# - This is a push event and the push event is on branch 'master'
name: Generate badge image with test coverage value
needs: [test, pre_ci]
if: ${{ ( github.event_name == 'pull_request' && github.actor == github.repository_owner ) || github.ref == 'refs/heads/master' }}
outputs:
url: ${{ steps.url.outputs.url }}
markdown: ${{ steps.url.outputs.markdown }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
ref: badges
path: badges
# Use the output from the `coverage` step
- name: Generate the badge SVG image
uses: emibcn/badge-action@v2.0.2
id: badge
with:
label: 'Coverage'
status: ${{ needs.test.outputs.coverage-rounded-display }}
color: ${{
needs.test.outputs.coverage > 90 && 'green' ||
needs.test.outputs.coverage > 80 && 'yellow,green' ||
needs.test.outputs.coverage > 70 && 'yellow' ||
needs.test.outputs.coverage > 60 && 'orange,yellow' ||
needs.test.outputs.coverage > 50 && 'orange' ||
needs.test.outputs.coverage > 40 && 'red,orange' ||
needs.test.outputs.coverage > 30 && 'red,red,orange' ||
needs.test.outputs.coverage > 20 && 'red,red,red,orange' ||
'red' }}
path: badges/test-coverage.svg
- name: Commit badge
env:
BRANCH: ${{ needs.pre_ci.outputs.branch }}
FILE: 'test-coverage.svg'
working-directory: ./badges
run: |
git config --local user.email "action@github.com"
git config --local user.name "GitHub Action"
mkdir -p "${BRANCH}"
mv "${FILE}" "${BRANCH}"
git add "${BRANCH}/${FILE}"
# Will give error if badge did not changed
git commit -m "Add/Update badge" || true
- name: Push badge commit
uses: ad-m/github-push-action@master
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
branch: badges
directory: badges
- name: Badge image URL
id: url
env:
BRANCH: ${{ needs.pre_ci.outputs.branch }}
FILE: 'test-coverage.svg'
COVERAGE: ${{ needs.test.outputs.coverage-rounded-display }}
run: |
URL="https://raw.githubusercontent.com/${GITHUB_REPOSITORY}/badges/${BRANCH}/${FILE}"
MARKDOWN="![Coverage ${COVERAGE}](${URL})"
echo "Badge URL: ${URL}"
echo "Badge image for Markdown: ${MARKDOWN}"
echo "##[set-output name=url;]${URL}"
echo "##[set-output name=markdown;]${MARKDOWN}"
comment_pr_badge:
name: Comment on PR with generated badge
needs: [pre_ci, badge]
if: ${{ github.event_name == 'pull_request' && github.actor == github.repository_owner }}
runs-on: ubuntu-latest
steps:
- name: Generate comment file with test coverage badge
shell: bash
env:
BADGE: ${{ needs.badge.outputs.markdown }}
run: |
echo "Badge: ${BADGE}"
echo "${BADGE}" > output.md
- name: Comment PR with test coverage badge
uses: marocchino/sticky-pull-request-comment@v2
with:
recreate: true
path: output.md
publish:
# Only generate and publish if these conditions are met:
# - Commit message does not contains #no-publish
# - The test step ended successfully
# - One of these is met:
# - This is a pull request event and the pull has been merged
# - This is a push event and the push event is on branch 'master'
name: Publish code to PyPi
needs: [pre_ci, test]
if: ${{ !contains(github.event.head_commit.message, '#no-publish') && (( github.event_name == 'pull_request' && github.event.pull_request.merged == true ) || github.ref == 'refs/heads/master' ) }}
runs-on: ubuntu-latest
steps:
- uses: actions/setup-python@v2
with:
python-version: '3.x' # Version range or exact version of a Python version to use, using SemVer's version range syntax
- uses: actions/checkout@v3
- name: Cache pip
uses: actions/cache@v3
with:
# This path is specific to Ubuntu
path: ~/.cache/pip
# Look to see if there is a cache hit for the corresponding requirements file
key: ${{ runner.os }}-pip-${{ hashFiles('requirements.txt') }}
restore-keys: |
${{ runner.os }}-pip-
${{ runner.os }}-
- name: Install dependencies
run: |
pip install -r pywisp_emibcn/requirements.txt
pip install build twine
- name: Build package
run: |
python3 -m build
- name: Publish package
env:
# Use dedicated PyPi token from secrets
TWINE_USERNAME: __token__
TWINE_PASSWORD: ${{ secrets.PYWISP_EMIBCN }}
run: |
python3 -m twine upload --repository pypi dist/*