-
Notifications
You must be signed in to change notification settings - Fork 7
268 lines (238 loc) · 10.8 KB
/
apps-test-os.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
on:
workflow_call:
inputs:
r-version:
type: string
default: release
required: false
os:
type: string
default: ubuntu-latest
required: false
extra-key:
type: string
default: ""
required: false
extra-packages:
type: string
default: ""
required: false
cache-version:
type: string
default: "2"
required: false
http-user-agent:
type: string
default: ""
required: false
pandoc-version:
type: string
default: "3.x"
required: false
# rtools-35:
# type: boolean
# default: true
# required: false
name: Test apps - single
jobs:
test:
runs-on: ${{ inputs.os }}
name: Test - R ${{ inputs.r-version }}${{ inputs.extra-key }}
env:
R_REMOTES_NO_ERRORS_FROM_WARNINGS: true
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
steps:
- name: Windows git setup
if: runner.os == 'Windows'
run: git config --global core.autocrlf false
- name: GitHub Pull (PR)
if: github.event_name == 'pull_request'
uses: actions/checkout@v4
with:
# check out the commit sha, not the merged sha. Avoids the "Merge SHA_A into SHA_B" into commits
ref: ${{ github.event.pull_request.head.sha }} # # Ref: https://github.com/actions/checkout/pull/115/files#diff-04c6e90faac2675aa89e2176d2eec7d8R203-R209
fetch-depth: 0
- name: GitHub Pull (Branch)
if: github.event_name != 'pull_request'
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Gather branch name
id: current_branch
shell: bash
run: |
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
echo "name=$CURRENT_BRANCH" >> $GITHUB_OUTPUT
- name: Collect changed file names
if: github.event_name == 'pull_request'
id: files_changed
uses: Ana06/[email protected]
with:
format: "json"
filter: "*"
- name: R Options
shell: bash
run: |
echo 'MAKEFLAGS=-j2' >> .Renviron
- name: Install R, shinycoreci, and shinyverse
timeout-minutes: 180 # Should be < 10 mins, except for mac
uses: ./.github/internal/install-shinyverse
with:
r-version: ${{ inputs.r-version }}
cache-version: ${{ inputs.cache-version }}
http-user-agent: ${{ inputs.http-user-agent }}
pandoc-version: ${{ inputs.pandoc-version }}
extra-packages: ${{ inputs.extra-packages }}
- name: Short R version, SHA, and OS
id: short
shell: bash
run: |
# keep only the major.minor values
RVERSION=`echo '${{ inputs.r-version }}' | sed 's/\([0-9]\.[0-9]\).*/\1/'`
echo "r-version: $RVERSION"
echo "r-version=$RVERSION" >> $GITHUB_OUTPUT
SHA="${{github.event.pull_request.head.sha}}${{ github.sha }}"
if [[ -z "$SHA" ]]; then
SHA="${{ github.sha }}"
fi
SHORT_SHA="${SHA:0:7}"
echo "sha: $SHORT_SHA"
echo "sha=$SHORT_SHA" >> $GITHUB_OUTPUT
# The `github.sha` value is the commit sha as if the PR was merged. :-(
# https://help.github.com/en/actions/automating-your-workflow-with-github-actions/events-that-trigger-workflows#pull-request-event-pull_request
# Checking for the pull request sha and then pasting it to the possibly 'merged' sha. If main branch, only the `.sha` exists. If a PR, the PR sha exists.
# Either way, take the first 7 characters.
# Found `github.event.pull_request.head.sha` example here: https://github.com/actions/checkout/commit/cab31617d857bf9e70dc35fd9e4dafe350794082#diff-04c6e90faac2675aa89e2176d2eec7d8R92
# The webhook payload example (equivalent to `github.event.pull_request`) shows the keys `head > sha` : https://developer.github.com/v3/activity/events/types/#pullrequestevent
- name: Create failure branch name
id: failed_branch
shell: Rscript {0}
run: |
cat("name=", '${{ steps.short.outputs.sha }}', '${{ inputs.extra-key }}', '-', format(Sys.time(), '%Y_%m_%d_%H_%M'), "\n", file = Sys.getenv("GITHUB_OUTPUT"), sep = "", append = TRUE)
- name: Create GHA branch name (i.e., test run identifier)
id: gha_branch
shell: Rscript {0}
run: |
cat("name=gha-", '${{ steps.failed_branch.outputs.name }}', '-', '${{ steps.short.outputs.r-version }}', '-', '${{ runner.os }}', "\n", file = Sys.getenv("GITHUB_OUTPUT"), sep = "", append = TRUE)
# Install packages as necessary!
- name: Run tests
timeout-minutes: 180 # 3 hrs
env:
# firefox needs creds to download drivers?
# https://github.com/bonigarcia/webdrivermanager#http-response-code-403
# https://github.com/schloerke/shinyjster/pull/42
WDM_GITHUBTOKENNAME: ${{ github.actor }}
WDM_GITHUBTOKENSECRET: ${{ secrets.GITHUB_TOKEN }}
shell: Rscript {0}
run: |
apps <- shinycoreci:::apps_with_tests()
is_pull_request <- isTRUE(jsonlite::parse_json("${{ github.event_name == 'pull_request' }}"))
if (is_pull_request) {
# reduce apps to only changed apps
files_changed <- jsonlite::parse_json('${{ steps.files_changed.outputs.added_modified }}', simplifyVector = TRUE)
has_altered_github <- any(unlist(lapply(strsplit(files_changed, "/"), function(folder_components) { folder_components[[1]] == ".github" })))
changed_app_folders <-
Filter(strsplit(files_changed, "/"), f = function(item) {
length(item) >= 3 &&
identical(item[[2]], "apps") &&
dir.exists(file.path(item[[1]], item[[2]], item[[3]]))
})
unique_changed_apps <- unique(unlist(lapply(changed_app_folders, `[[`, 3)))
if (has_altered_github) {
message("Altered `.github` folder. Testing all apps!")
} else if (length(unique_changed_apps) > 10) {
message("More than ten apps were altered. Testing all apps!")
} else if (length(changed_app_folders) > 250) {
message("More than 250 app files were altered. Testing all apps!")
} else {
apps <- apps[apps %in% unique_changed_apps]
if (length(apps) == 0) stop("No apps to check")
cat("Testing apps:\n", paste0("* ", apps, "\n"))
}
}
test_output <- shinycoreci::test_in_local(
apps = apps,
assert = FALSE
)
shinycoreci::save_test_results(
test_output,
gha_branch_name = '${{steps.gha_branch.outputs.name}}',
pr_number = '${{github.event.pull_request.number}}',
username = '${GITHUB_ACTOR}'
)
shinycoreci::assert_test_output(test_output)
- name: Remove R options file
if: always()
shell: bash
run: |
rm .Renviron || echo "No .Renviron file to remove"
- name: Git config
if: always()
shell: bash
# set local email to actor
# set local name to actor
# set to merge when pulling (used for test results)
# need to unset the extra header to allow for regular https push
# display config
run: |
git config --local user.email "${GITHUB_ACTOR}@users.noreply.github.com" && \
git config --local user.name "${GITHUB_ACTOR}" && \
git config --local pull.rebase false && \
git config --local --unset http.https://github.com/.extraheader && \
git config --local --list
- name: Push test files to rstudio/shinycoreci `gha-` branch
if: always()
shell: bash
# git checkout -B -- Creates the branch <new_branch> and start it at <start_point>; if it already exists, then reset it to <start_point>.
# git push --force -- JUST in case the prior branch existed
run: |
git checkout -B ${{ steps.gha_branch.outputs.name }}
git add inst/apps/sysinfo-* && \
git commit -m '`sysinfo` Changes - rstudio/shinycoreci@${{ steps.short.outputs.sha }} ${{ inputs.extra-key }}' || \
echo "No apps/sysinfo-* changes to commit"
git add inst/apps && \
git commit -m 'App changes - rstudio/shinycoreci@${{ steps.short.outputs.sha }} ${{ inputs.extra-key }}'|| \
echo "No app changes to commit"
git log -n 4 --pretty=oneline --simplify-by-decoration
# if any commits occured, then push to repo (compare to sha of current execution)
echo "`git rev-list --count HEAD ^${{ steps.short.outputs.sha }}`"
if (( `git rev-list --count HEAD ^${{ steps.short.outputs.sha }}` > 0 )); then
# This branch should never exist. Force push
git push --force "https://$GITHUB_ACTOR:${{secrets.GITHUB_TOKEN}}@github.com/rstudio/shinycoreci.git" "HEAD:${{ steps.gha_branch.outputs.name }}"
fi
git checkout ${{ steps.current_branch.outputs.name }}
- name: Push test results to rstudio/shinycoreci
if: always()
shell: bash
# Add saved test results (in __test_results/) to _test_results branch
# (this assumes the __test_results branch already exists, which was setup manually like this
# https://stackoverflow.com/questions/34100048/github-create-empty-branch)
run: |
if [ "${{ steps.current_branch.outputs.name }}" != "main" ]; then
echo "Not on main branch. Not going to save results"
echo "Exiting! (safely)"
exit 0
fi
git fetch origin _test_results
git checkout _test_results --
git status
git add __test_results/
git commit -m "Add test results for ${{steps.gha_branch.outputs.name}}"
# Attempt to push until success. Allow for 15 attempts
counter=0
while true; do
git pull --rebase origin _test_results
# Capture push status and do not exit action
PUSH_STATUS=0
output=$(git push "https://$GITHUB_ACTOR:${{secrets.GITHUB_TOKEN}}@github.com/rstudio/shinycoreci.git" _test_results) && PUSH_STATUS=$? || PUSH_STATUS=$?
# If push was successful, exit loop
if [ "$PUSH_STATUS" = 0 ]; then
break; # break `while true` loop
fi
# Wait 5-15 seconds to avoid deadlock
sleep $[ ($RANDOM % 10) + 5 ]s
((counter++))
[[ counter -gt 15 ]] && echo "Too many attempts!" && exit 1
echo "Push failed. Trying again. Try #$counter"
done
git checkout ${{ steps.current_branch.outputs.name }}