-
Notifications
You must be signed in to change notification settings - Fork 2
/
action.yml
217 lines (198 loc) · 7.34 KB
/
action.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
name: Update JS
description: Updates JS dependencies in Silverstripe core modules
runs:
using: composite
steps:
- name: Checkout code
uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4.2.1
- name: Read .nvmrc
id: read-nvm
shell: bash
run: |
echo "version=$(cat .nvmrc)" >> "$GITHUB_OUTPUT"
- name: Setup node
uses: actions/setup-node@0a44ba7841725637a19e28fa30b79a866c81b0a6 # v4.0.4
with:
node-version: ${{ steps.read-nvm.outputs.version }}
- name: Create temporary artifacts directory
shell: bash
run: |
# Note: creating the directory for artifacts one level up from the checkout directory so that we do not commit
# the artifacts to the repository is not allowed by actions/upload-artifact
mkdir __artifacts
# Ensure there will always at least one file in the archive so that there are no issues creating it
touch __artifacts/placeholder
- name: Output versions
shell: bash
run: |
echo "node version is:"
node --version
echo "npm version is:"
npm --version
echo "yarn version is:"
yarn --version
- name: Install admin JS
if: github.event.repository.name != 'silverstripe-admin'
shell: bash
run: |
# Install admin js in sibling directory so shared components are available
DIR=$(pwd)
cd ..
git clone https://github.com/silverstripe/silverstripe-admin.git admin
# Assumption is that the default branch of admin is the correct one to build against
cd admin
# yarn comes preinstalled with actions/setup-node
yarn install
cd $DIR
# Use `yarn install` rather than `yarn upgrade` to prevent the following error:
# "error Outdated lockfile. Please run `yarn install` and try again."
- name: Update yarn.lock
shell: bash
run: |
if [[ -f yarn.lock ]]; then
rm yarn.lock
fi
yarn install
- name: Read package.json
id: package-json
shell: bash
run: |
# Read package.json to see if lint and test are runnable scripts
LINT="false"
TEST="false"
if [[ "$(jq .scripts.lint? package.json)" != "null" ]]; then LINT="true"; fi
if [[ "$(jq .scripts.test? package.json)" != "null" ]]; then TEST="true"; fi
echo "lint=$LINT" >> "$GITHUB_OUTPUT"
echo "test=$TEST" >> "$GITHUB_OUTPUT"
echo "LINT is $LINT"
echo "TEST is $TEST"
# The following 3 steps make up `yarn build`
# Splitting apart to make it easier to see where any failures originate from
- name: Yarn lint
if: steps.package-json.outputs.lint == 'true'
shell: bash
run: |
yarn lint
- name: Yarn test
if: steps.package-json.outputs.test == 'true'
shell: bash
run: |
yarn test
- name: Build JS with webpack
# always run this and subsequent steps even if yarn.lint/yarn.test fails so that pull-request is
# created which will result in a red pull-request build so it's easy to see where things are at
if: always()
shell: bash
run: |
NODE_ENV=production node_modules/.bin/webpack --mode production --bail --progress
- name: Remove any old pull-requests
if: always()
shell: bash
env:
GITHUB_REPOSITORY: ${{ github.repository }}
run: |
# https://docs.github.com/en/rest/pulls/pulls#list-pull-requests
RESP_CODE=$(curl -w %{http_code} -s -o __response.json \
-X GET https://api.github.com/repos/$GITHUB_REPOSITORY/pulls \
-H "Accept: application/vnd.github.v3+json")
if [[ $RESP_CODE != "200" ]]; then
echo "Unable to list pull-requests - HTTP response code was $RESP_CODE"
exit 1
fi
JSON=$(cat __response.json)
rm __response.json
NUMBERS=$(echo $JSON | jq '.[] | select(.title=="DEP Update JS dependencies" and .user.login=="github-actions[bot]") | .number')
for NUMBER in $NUMBERS; do
# https://docs.github.com/en/rest/pulls/pulls#update-a-pull-request
RESP_CODE=$(curl -w %{http_code} -s -o /dev/null \
-X PATCH https://api.github.com/repos/$GITHUB_REPOSITORY/pulls/$NUMBER \
-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: token ${{ github.token }}" \
-d @- << EOF
{
"state": "closed"
}
EOF
)
if [[ $RESP_CODE != "200" ]]; then
echo "Unable to close pull-request $NUMBER - HTTP response code was $RESP_CODE"
exit 1
fi
echo "Closed old pull-request $NUMBER"
done
- name: Remove any old branches
if: always()
shell: bash
env:
GITHUB_REPOSITORY: ${{ github.repository }}
run: |
# Gets all branches from GitHub API
# https://docs.github.com/en/rest/branches/branches?apiVersion=2022-11-28#list-branches
RESP_CODE=$(curl -w %{http_code} -s -o __branches.json \
-X GET "https://api.github.com/repos/$GITHUB_REPOSITORY/branches?per_page=100" \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ github.token }}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
)
if [[ $RESP_CODE != "200" ]]; then
echo "Unable to read list of branches - HTTP response code was $RESP_CODE"
cat __branches.json
exit 1
fi
BRANCHES=$(cat __branches.json | jq -r '.[] | .name | select(.|test("^pulls\/[0-9]\/update-js-[0-9]{10}$"))')
for BRANCH in $BRANCHES; do
if [[ "$BRANCH" =~ ^pulls/[0-9\.]+/update\-js\-[0-9]+$ ]]; then
git push origin --delete "$BRANCH"
echo "Deleted old branch $BRANCH"
fi
done
- name: Copy artifacts
if: always()
shell: bash
run: |
AT_LEAST_ONE_FILE=0
if [[ -f package.json ]]; then
cp package.json __artifacts
AT_LEAST_ONE_FILE=1
fi
if [[ -f yarn.lock ]]; then
cp yarn.lock __artifacts
AT_LEAST_ONE_FILE=1
fi
if [[ $AT_LEAST_ONE_FILE == 1 ]]; then
rm __artifacts/placeholder
fi
# https://docs.github.com/en/actions/using-workflows/storing-workflow-data-as-artifacts
- name: Upload artifacts
uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882 # @v4.4.3
if: always()
with:
name: artifacts
path: __artifacts
- name: Delete temporary files
shell: bash
if: always()
run: |
rm -rf __artifacts
if [[ -f __branches.json ]]; then
rm __branches.json
fi
- name: Generate branch name
if: always()
id: generate-branch-name
shell: bash
env:
GITHUB_REF: ${{ github.ref }}
run: |
# Convert refs/heads/mybranch to mybranch
CUT=$(echo $GITHUB_REF | cut -c 12-)
# e.g. pulls/1/update-js-1647810133
BRANCH=pulls/$CUT/update-js-$(date +%s)
echo "branch=$BRANCH" >> "$GITHUB_OUTPUT"
- name: Git
if: always()
uses: silverstripe/gha-pull-request@v1
with:
branch: ${{ steps.generate-branch-name.outputs.branch }}
title: DEP Update JS dependencies
description: Automated yarn upgrade and yarn build