generated from insightsengineering/r.pkg.template
-
Notifications
You must be signed in to change notification settings - Fork 0
557 lines (522 loc) · 22.2 KB
/
build-check-install.yaml
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
---
name: R CMD Check 🧬
on:
push:
tags:
- "v*"
branches:
- main
pull_request:
types:
- opened
- synchronize
- reopened
- ready_for_review
branches:
- main
workflow_dispatch:
workflow_call:
secrets:
REPO_GITHUB_TOKEN:
description: |
Github token with read access to repositories, required for staged.dependencies installation
required: false
inputs:
install-system-dependencies:
description: Check for and install system dependencies
required: false
default: false
type: boolean
enable-staged-dependencies-check:
description: Enable staged dependencies YAML check
required: false
default: true
type: boolean
R_CHECK_FORCE_SUGGESTS:
description: If true, give an error if suggested packages are not available
required: false
default: true
type: boolean
skip-r-cmd-check:
description: Skip the R CMD check step in this workflow
required: false
default: false
type: boolean
skip-r-cmd-install:
description: Skip the R CMD INSTALL step in this workflow
required: false
default: false
type: boolean
enforce-note-blocklist:
description: Whether to check for specific NOTEs via regexes that should cause the pipeline to fail
required: false
default: false
type: boolean
note-blocklist:
description: |
List of regular expressions appearing in NOTEs that should cause the pipeline to fail.
Example usage:
note-blocklist: |
checking package dependencies ... NOTE( )+Depends: includes the non-default packages
checking R code for possible problems ... NOTE( )+.*: no visible global function definition for
checking for unstated dependencies in vignettes ... NOTE( )+\'.*\' import not declared from
checking dependencies in R code ... NOTE( )+Namespace in Imports field not imported from
checking installed package size ... NOTE( )+installed size is
required: false
default: ""
type: string
additional-r-cmd-check-params:
description: Additional flags or parameters to add to R CMD check
required: false
default: ""
type: string
disable-unit-test-reports:
description: Do not produce unit test reports
required: false
default: false
type: boolean
additional-env-vars:
description: |
Extra environment variables, as a 'key=value' pair, with each pair on a new line.
Example usage:
additional-env-vars: |
ABC=123
XYZ=456
required: false
default: ""
type: string
junit-xml-storage:
description: Branch name to store JUnit XML reports
required: false
default: "_junit_xml_reports"
type: string
junit-xml-diff-branch:
description: Against which branch should the JUnit XML report from the current branch be compared?
required: false
default: main
type: string
junit-xml-comparison:
description: Turn on JUnit XML comparison
required: false
default: true
type: boolean
junit-xml-negative-threshold:
description: |
If time difference during JUnit XML comparison is between negative-threshold and 0: it's treated as 0.
This means that if test suite/case executes up to negative-threshold seconds faster,
this time difference is ignored as insignificant, and test suite/case is not shown in the table
required: false
default: 1.0
type: string
junit-xml-positive-threshold:
description: |
If time difference during JUnit XML comparison is between 0 and positive-threshold: it's treated as 0.
This means that if test suite/case executes up to positive-threshold seconds slower,
this time difference is ignored as insignificant, and test suite/case is not shown in the table
required: false
default: 1.0
type: string
concurrency:
group: r-cmd-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true
jobs:
build-install-check:
strategy:
fail-fast: false
matrix:
config:
- image: ghcr.io/insightsengineering/rstudio_4.2.2_bioc_3.16
tag: latest
name: ${{ matrix.config.image }}, version ${{ matrix.config.tag }}
runs-on: ubuntu-latest
if: >
!contains(github.event.commits[0].message, '[skip r-cmd]')
&& github.event.pull_request.draft == false
container:
image: ${{ matrix.config.image }}:${{ matrix.config.tag }}
steps:
- name: Get branch names 🌿
id: branch-name
uses: tj-actions/branch-names@v6
- name: Normalize variables 📏
run: |
junit_xml_storage_input="${{ inputs.junit-xml-storage }}"
junit_xml_diff_branch_input="${{ inputs.junit-xml-diff-branch }}"
junit_xml_comparison_input="${{ inputs.junit-xml-comparison }}"
junit_xml_positive_threshold="${{ inputs.junit-xml-positive-threshold }}"
junit_xml_negative_threshold="${{ inputs.junit-xml-negative-threshold }}"
echo "junit_xml_storage=${junit_xml_storage_input:-_junit_xml_reports}" >> $GITHUB_ENV
echo "junit_xml_diff_branch=${junit_xml_diff_branch_input:-main}" >> $GITHUB_ENV
echo "junit_xml_comparison=${junit_xml_comparison_input:-true}" >> $GITHUB_ENV
echo "junit_xml_positive_threshold=${junit_xml_positive_threshold:-1.0}" >> $GITHUB_ENV
echo "junit_xml_negative_threshold=${junit_xml_negative_threshold:-1.0}" >> $GITHUB_ENV
shell: bash
- name: Checkout repo (PR) 🛎
uses: actions/checkout@v3
if: github.event_name == 'pull_request'
with:
ref: ${{ steps.branch-name.outputs.head_ref_branch }}
path: ${{ github.event.repository.name }}
repository: ${{ github.event.pull_request.head.repo.full_name }}
- name: Checkout repo 🛎
uses: actions/checkout@v3
if: github.event_name != 'pull_request'
with:
ref: ${{ steps.branch-name.outputs.head_ref_branch }}
path: ${{ github.event.repository.name }}
- name: Restore SD cache 💰
uses: actions/cache@v3
with:
key: sd-${{ runner.os }}-${{ github.event.repository.name }}
path: ~/.staged.dependencies
- name: Run Staged dependencies 🎦
uses: insightsengineering/staged-dependencies-action@v1
env:
GITHUB_PAT: ${{ secrets.REPO_GITHUB_TOKEN }}
with:
path: ${{ github.event.repository.name }}
enable-check: ${{ inputs.enable-staged-dependencies-check }}
run-system-dependencies: ${{ inputs.install-system-dependencies }}
- name: Show session info and installed packages ℹ
run: |
sessionInfo()
as.data.frame(installed.packages()[, c("LibPath", "Version")])
if (grepl("--as-cran", "${{ inputs.additional-r-cmd-check-params }}", fixed = TRUE)) {
x <- desc::desc_del("Remotes")
}
if ("${{ inputs.disable-unit-test-reports }}" != "true") {
x <- desc::desc_set_dep("xml2", "Suggests")
}
shell: Rscript {0}
working-directory: ${{ github.event.repository.name }}
- name: Replace testthat.R for test reporting 🎚
if: inputs.disable-unit-test-reports != 'true'
run: |
if [ -f "tests/testthat.R" ]; then {
# Overwrite testthat.R for JUnit XML reporting
cat > tests/testthat.R <<EOF
pkg_name <- "${{ github.event.repository.name }}"
if (requireNamespace("testthat", quietly = TRUE)) {
library(testthat)
reporter <- MultiReporter\$new(list(
CheckReporter\$new(),
JunitReporter\$new(file = "junit-result.xml")
))
test_check(pkg_name, reporter = reporter)
}
EOF
}
cat tests/testthat.R
fi
shell: bash
working-directory: ${{ github.event.repository.name }}
- name: Build R package 🏗
run: |
if [ "${{ inputs.additional-env-vars }}" != "" ]
then {
echo -e "${{ inputs.additional-env-vars }}" > /tmp/dotenv.env
export $(tr '\n' ' ' < /tmp/dotenv.env)
}
fi
R CMD build ${{ github.event.repository.name }}
echo "PKGBUILD=$(echo ${{ github.event.repository.name }}_*.tar.gz)" >> $GITHUB_ENV
shell: bash
- name: Set TESTING_DEPTH ⚙
env:
COMMIT_NEWEST_MESSAGE: ${{ github.event.head_commit.message }}
COMMIT_OLDEST_MESSAGE: ${{ github.event.commits[0].message }}
run: |
# set TESTING_DEPTH for PR
if [[ ! -z "${GITHUB_HEAD_REF}" ]]; then
TESTING_DEPTH=3
echo "TESTING_DEPTH=3" >> $GITHUB_ENV
COMMIT_NEWEST_MESSAGE=$(git log --format=%B -n 1 ${{ github.event.after }})
fi
if [[ $COMMIT_NEWEST_MESSAGE == *"[skip tests]"* ]]; then
echo "NO_TESTS=1" >> $GITHUB_ENV
fi
# default TESTING_DEPTH
if [[ -z "${TESTING_DEPTH}" ]]; then
echo "TESTING_DEPTH=1" >> $GITHUB_ENV
fi
shell: bash
working-directory: ${{ github.event.repository.name }}
- name: Run R CMD check 🏁
run: |
if [ "${{ inputs.skip-r-cmd-check }}" == "true" ]
then {
echo "Skipping R CMD check as 'skip-r-cmd-check' was set to 'true'"
exit 0
}
fi
if [ "${{ inputs.R_CHECK_FORCE_SUGGESTS }}" == "" ]
then {
_R_CHECK_FORCE_SUGGESTS_="FALSE"
}
fi
ADDITIONAL_PARAMS=${{ inputs.additional-r-cmd-check-params }}
if [[ ! -z "${{ env.NO_TESTS }}" ]]
then {
ADDITIONAL_PARAMS="${ADDITIONAL_PARAMS} --no-tests"
}
fi
if [ "${{ inputs.additional-env-vars }}" != "" ]
then {
echo -e "${{ inputs.additional-env-vars }}" > /tmp/dotenv.env
export $(tr '\n' ' ' < /tmp/dotenv.env)
}
fi
Rscript - <<EOF
if ("${{ inputs.disable-unit-test-reports }}" != "true") {
if (!require("xml2")) {
install.packages("xml2", repos = "https://cloud.r-project.org", quiet = TRUE)
}
}
EOF
R CMD check ${ADDITIONAL_PARAMS} ${{ env.PKGBUILD }}
shell: bash
continue-on-error: true
env:
_R_CHECK_TESTS_NLINES_: 0
_R_CHECK_VIGNETTES_NLINES_: 0
_R_CHECK_FORCE_SUGGESTS_: ${{ inputs.R_CHECK_FORCE_SUGGESTS }}
- name: Fetch report from ${{ env.junit_xml_storage }} ⤵️
if: env.junit_xml_comparison == 'true'
uses: actions/checkout@v3
with:
path: ${{ env.junit_xml_storage }}
fetch-depth: 0
- name: Initialize storage branch ${{ env.junit_xml_storage }} 🗄️
if: env.junit_xml_comparison == 'true'
working-directory: ${{ env.junit_xml_storage }}
run: |
git config --global --add safe.directory ${PWD}
git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
git config --global user.name "github-actions[bot]"
# Switch to the branch if it already exists
git switch ${{ env.junit_xml_storage }} || true
git pull origin ${{ env.junit_xml_storage }} || true
# Create the branch if it doesn't exist yet
git checkout --orphan ${{ env.junit_xml_storage }} || true
# Ensure that the bare minimum components exist in the branch
mkdir -p data
touch README.md data/.gitkeep
# Copy necessary files and folders to a temporary location
mkdir -p /tmp/${{ github.sha }}
echo "Copying data to /tmp/${{ github.sha }}"
cp -r .git README.md data /tmp/${{ github.sha }}
# Remove everything else
# Attribution: https://unix.stackexchange.com/a/77313
rm -rf ..?* .[!.]* *
# Restore files from the temporary location
echo "Copying data from /tmp/${{ github.sha }}"
cp -r /tmp/${{ github.sha }}/.git /tmp/${{ github.sha }}/README.md /tmp/${{ github.sha }}/data .
rm -rf /tmp/${{ github.sha }}
git add --all -f
git commit -m "Update storage branch: $(date)" || true
shell: bash
- name: Push storage branch ${{ env.junit_xml_storage }} 🗄️
if: env.junit_xml_comparison == 'true'
uses: ad-m/github-push-action@master
with:
github_token: ${{ secrets.REPO_GITHUB_TOKEN }}
branch: ${{ env.junit_xml_storage }}
directory: ${{ env.junit_xml_storage }}
force: true
- name: Check whether JUnit XML report exists 🚦
id: check-junit-xml
uses: andstor/file-existence-action@v2
with:
files: "${{ github.event.repository.name }}.Rcheck/tests/testthat/junit-result.xml"
- name: Publish Unit Test Summary 📑
uses: EnricoMi/publish-unit-test-result-action@v2
id: test-results
if: steps.check-junit-xml.outputs.files_exists == 'true' && github.event_name == 'pull_request'
with:
check_name: Unit Tests Summary
junit_files: "${{ github.event.repository.name }}.Rcheck/tests/testthat/junit-result.xml"
- name: Fetch JUnit XML reports from ${{ env.junit_xml_storage }} ⤵️
if: steps.check-junit-xml.outputs.files_exists == 'true' && env.junit_xml_comparison == 'true'
uses: actions/checkout@v3
with:
path: ${{ env.junit_xml_storage }}
fetch-depth: 0
- name: Set up ${{ env.junit_xml_storage }} branch for reports 🗃️
if: steps.check-junit-xml.outputs.files_exists == 'true' && env.junit_xml_comparison == 'true'
run: |
BRANCH=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}
echo "diff_storage_branch=$BRANCH" >> $GITHUB_ENV
mkdir -p ${{ env.junit_xml_storage }}/data/${BRANCH}
shell: bash
- name: Commit JUnit XML report 📄
if: steps.check-junit-xml.outputs.files_exists == 'true' && env.junit_xml_comparison == 'true'
working-directory: ${{ env.junit_xml_storage }}/data
run: |
git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
git config --global user.name "github-actions[bot]"
git switch ${{ env.junit_xml_storage }} || true
git config pull.rebase false
git pull origin ${{ env.junit_xml_storage }}
cp ../../${{ github.event.repository.name }}.Rcheck/tests/testthat/junit-result.xml \
./${{ env.diff_storage_branch }}/junit-result.xml
git add -f "${{ env.diff_storage_branch }}/junit-result.xml"
git commit -m "Add/Update JUnit report: ${{ github.sha }}" || true
shell: bash
- name: Push JUnit XML report to ${{ env.junit_xml_storage }} 📄
if: steps.check-junit-xml.outputs.files_exists == 'true' && env.junit_xml_comparison == 'true'
uses: ad-m/github-push-action@master
with:
github_token: ${{ secrets.REPO_GITHUB_TOKEN }}
branch: ${{ env.junit_xml_storage }}
directory: ${{ env.junit_xml_storage }}/data
- name: Run XML comparison 🔍
if: steps.check-junit-xml.outputs.files_exists == 'true' && env.junit_xml_comparison == 'true'
run: |
cp ${{ env.junit_xml_storage }}/data/${{ env.diff_storage_branch }}/junit-result.xml new.xml
if [ ! -f ${{ env.junit_xml_storage }}/data/${{ env.junit_xml_diff_branch }}/junit-result.xml ]; then
# If XML for the branch against which we're comparing doesn't exist, we're skipping the comparison.
echo "JUnit report for branch \`${{ env.junit_xml_diff_branch }}\` " \
"doesn't exist on \`${{ env.junit_xml_storage }}\` branch yet." > output.md
echo "Once this workflow runs on \`${{ env.junit_xml_diff_branch }}\` branch, " \
"you'll see comparison of tests performance between \`${{ env.junit_xml_diff_branch }}\` " \
"and \`${{ env.diff_storage_branch }}\` as a PR comment." >> output.md
else
cp ${{ env.junit_xml_storage }}/data/${{ env.junit_xml_diff_branch }}/junit-result.xml old.xml
wget https://github.com/insightsengineering/junit-xml-diff/releases/download/v0.3.0/junit-xml-diff_0.3.0_linux_amd64.tar.gz \
-O junit-xml-diff.tar.gz
tar xzf junit-xml-diff.tar.gz
./junit-xml-diff old.xml new.xml output.md '${{ env.junit_xml_diff_branch }}' \
'${{ env.junit_xml_positive_threshold }}' '${{ env.junit_xml_negative_threshold }}'
echo "Results for commit ${{ github.sha }}" >> output.md
echo "" >> output.md
echo "♻️ This comment has been updated with latest results." >> output.md
cp output.md output-artifact.md
echo "Size of markdown: $(du -b output.md | awk '{print $1}') bytes"
if [ "$(du -b output.md | awk '{print $1}')" -ge "60000" ]; then
echo "The result of JUnit XML file comparison exceeded maximum size. " \
"The report has therefore been uploaded as an R CMD check workflow artifact." > output.md
echo "junit_xml_comparison_result_method=artifact" >> $GITHUB_ENV
fi
fi
echo "Markdown output:"
cat output.md
- name: Post JUnit XML comparison as comment 💬
if: >
steps.check-junit-xml.outputs.files_exists == 'true' &&
env.junit_xml_comparison == 'true'
uses: marocchino/sticky-pull-request-comment@v2
with:
header: markdown-table
path: output.md
- name: Upload JUnit XML comparison ⤴
if: >
steps.check-junit-xml.outputs.files_exists == 'true' &&
env.junit_xml_comparison == 'true' &&
env.junit_xml_comparison_result_method == 'artifact'
uses: actions/upload-artifact@v3
with:
path: output-artifact.md
name: junit-xml-report-comparison
- name: Catch warnings in R CMD check output 🗳
id: catch-errors
run: |
check_log <- "${{ github.event.repository.name }}.Rcheck/00check.log"
if (file.exists(check_log)) {
x <- tail(readLines(check_log), 1)
if (grepl("ERROR", x)) {
writeLines(readLines(check_log))
stop("❌ R CMD check has errors")
}
if (grepl("WARNING", x)) {
writeLines(readLines(check_log))
stop("⚠ R CMD check has warnings")
}
if ("${{ inputs.enforce-note-blocklist }}" == "true") {
print("Checking notes...")
regexes <- "${{ inputs.note-blocklist }}"
regexes <- unlist(strsplit(regexes, split = "\n"))
lines <- paste(readLines(check_log), collapse = " ")
notes_result <- vapply(
regexes,
function(r){
if (grepl(r, lines)) {
print(r)
return(TRUE)
}
return(FALSE)
},
logical(1)
)
if (any(notes_result)) {
stop("NOTEs on the blocklist were found (see above).")
}
}
}
shell: Rscript {0}
- name: Install R package 🚧
run: |
if [ "${{ inputs.skip-r-cmd-install }}" == "true" ]
then {
echo "Skipping R CMD INSTALL as 'skip-r-cmd-install' was set to 'true'"
exit 0
}
fi
if [ "${{ inputs.additional-env-vars }}" != "" ]
then {
echo -e "${{ inputs.additional-env-vars }}" > /tmp/dotenv.env
export $(tr '\n' ' ' < /tmp/dotenv.env)
}
fi
R CMD INSTALL ${{ env.PKGBUILD }}
shell: bash
- name: Rebuild R package 🏗
if: inputs.disable-unit-test-reports != 'true'
run: |
# Undo changes to DESCRIPTION and tests/testthat.R
git checkout DESCRIPTION
if [ -f "tests/testthat.R" ]; then
git checkout tests/testthat.R
fi
if [ "${{ inputs.additional-env-vars }}" != "" ]
then {
echo -e "${{ inputs.additional-env-vars }}" > /tmp/dotenv.env
export $(tr '\n' ' ' < /tmp/dotenv.env)
}
fi
R CMD build .
echo "PKGBUILD=$(echo ${{ github.event.repository.name }}_*.tar.gz)" >> $GITHUB_ENV
shell: bash
working-directory: ${{ github.event.repository.name }}
- name: Upload package build ⤴
if: startsWith(github.ref, 'refs/tags/v')
uses: actions/upload-artifact@v3
with:
path: ${{ github.event.repository.name }}/${{ env.PKGBUILD }}
name: ${{ env.PKGBUILD }}
upload-release-assets:
name: Upload build tar.gz
needs: build-install-check
runs-on: ubuntu-latest
if: >
startsWith(github.ref, 'refs/tags/v')
&& (!contains(github.event.commits[0].message, '[skip r-cmd]'))
&& github.event.pull_request.draft == false
steps:
- name: Checkout repo 🛎
uses: actions/checkout@v3
- name: Set release version and package build filename 📐
run: echo "RELEASE_VERSION=${GITHUB_REF#refs/*/v}" >> $GITHUB_ENV
- name: Get package build filename 📦
run: echo "PKGBUILD=${{ github.event.repository.name }}_${{ env.RELEASE_VERSION }}.tar.gz" >> $GITHUB_ENV
- name: Download artifact ⏬
uses: actions/download-artifact@v3
with:
name: ${{ env.PKGBUILD }}
- name: Upload binaries to release 🔼
uses: svenstaro/upload-release-action@v2
with:
repo_token: ${{ secrets.GITHUB_TOKEN }}
file: ${{ env.PKGBUILD }}
asset_name: ${{ env.PKGBUILD }}
tag: ${{ github.ref }}
overwrite: true