/
action.yml
306 lines (278 loc) · 10.5 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
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
name: 'Build Image w/ Yarn'
description: 'Build an image using yarn'
author: 'GarnerCorp'
branding:
icon: 'aperture'
color: 'purple'
inputs:
checkout:
description: Whether or not to checkout the repository you are currently working in
required: false
default: "true"
npm-registry:
description: "The npm registry from which to retrieve packages"
default: "https://registry.npmjs.org/"
required: false
npm-username:
description: "Username for the npm-registry"
required: false
npm-password:
description: "Password for the npm-registry"
required: false
npm-email:
description: "Email for the npm-registry (required yarn v1)"
required: false
node-version:
description: "Node version to use"
default: "18"
required: false
yarn-version:
description: "Yarn version to use"
default: "3.6.4"
required: false
yarn-use-berry:
description: "Use yarn berry (~3)"
required: false
deprecationMessage: "Use yarn-version instead"
working-directory:
description: "Directory containing package.json"
default: "."
required: false
yarn-build-command:
description: "Command to build project"
default: "yarn build"
required: false
source-directory:
description: "Source directory within working directory (relative to working-directory)"
default: "src"
required: false
test-file-pattern:
description: "Array of globs for test files"
default: '["*.spec.js", "*.spec.ts"]'
required: false
yarn-test-command:
description: "Command to run tests"
default: "yarn test"
required: false
yarn-build-directory:
description: "Directory containing output from yarn-build-command (relative to working-directory)"
default: "dist"
required: false
build-output-tarball:
description: "Output file path (relative to yarn-build-directory)"
default: "../dist.tar.gz"
required: false
publish-to-registry:
description: "Optionally publish to npm-registry"
default: "false"
required: false
publishing-branch:
description: "Branch to publish registry off of"
default: "master"
required: false
publishing-directory:
description: "Directory to publish to npm-registry"
default: "dist"
required: false
skip-package:
description: "Skip packaging"
required: false
runs:
using: 'composite'
steps:
- name: Validate inputs (yarn berry)
if: ${{ !inputs.google-credentials-json != !inputs.google-cloud-sdk-version || (inputs.yarn-version >= '2' && !inputs.npm-username != !inputs.npm-password) }}
shell: bash
env:
CODEPENDENT: '[ [ "google-cloud-sdk-version", "google-credentials-json" ], [ "npm-username", "npm-password" ] ]'
INPUTS: ${{ toJSON(inputs) }}
run: |
"$GITHUB_ACTION_PATH/../scripts/report-missing-inputs.pl"
- name: Validate inputs (yarn v1)
if: ${{ !inputs.google-credentials-json != !inputs.google-cloud-sdk-version || (inputs.yarn-version < '2' && (!inputs.npm-username != !inputs.npm-password || !inputs.npm-username != !inputs.npm-email)) }}
shell: bash
env:
CODEPENDENT: '[ [ "google-cloud-sdk-version", "google-credentials-json" ], [ "npm-username", "npm-password", "npm-email" ] ]'
INPUTS: ${{ toJSON(inputs) }}
run: |
"$GITHUB_ACTION_PATH/../scripts/report-missing-inputs.pl"
- name: Checkout
if: ${{ inputs.checkout == 'true' }}
uses: actions/checkout@v4
- name: Enable corepack
shell: bash
run: corepack enable
- name: Set up Node ${{ inputs.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ inputs.node-version }}
- name: Use Yarn Berry
shell: bash
if: inputs.yarn-version >= '2'
env:
yarn_version: ${{ inputs.yarn-version }}
run: |
$GITHUB_ACTION_PATH/../scripts/add-matchers.sh
if ! command -v yarn >/dev/null; then
corepack prepare yarn@stable --activate
fi
yarn set version "$yarn_version"
- name: Ensure yarn is available
shell: bash
if: inputs.yarn-version < '2'
run: |
if ! command -v yarn >/dev/null; then
npm install --global yarn
fi
- name: Get yarn cache directory path (yarn berry)
if: inputs.yarn-version >= '2'
working-directory: ${{ inputs.working-directory }}
shell: bash
run: |
dir="$(yarn config get cacheFolder)"
mkdir -p "$dir"
echo "CACHE_DIR_YARN=$dir" | tee -a "$GITHUB_ENV"
- name: Get yarn cache directory path (yarn v1)
if: inputs.yarn-version < '2'
working-directory: ${{ inputs.working-directory }}
shell: bash
run: |
dir="$(yarn cache dir)"
mkdir -p "$dir"
echo "CACHE_DIR_YARN=$dir" | tee -a "$GITHUB_ENV"
- name: Use Yarn Cache
uses: actions/cache@v4
id: yarn-cache
with:
path: ${{ env.CACHE_DIR_YARN }}
key: yarn-${{ runner.os }}-${{ inputs.working-directory }}-${{ hashFiles(format('{0}/{1}', inputs.working-directory, 'yarn.lock')) }}
restore-keys: yarn-${{ runner.os }}-${{ inputs.working-directory }}
- name: Set up yarn credentials
if: inputs.npm-username && inputs.npm-password && inputs.yarn-version >= '2'
shell: bash
env:
NPM_USER: ${{ inputs.npm-username }}
NPM_PASSWORD: ${{ inputs.npm-password }}
NPM_REGISTRY: ${{ inputs.npm-registry }}
run: |
perl -i -e 'next if /CACHE_DIR_YARN/;print ' "$GITHUB_ENV"
yarn_credentials=$(echo -n "$NPM_USER:$NPM_PASSWORD" | base64)
yarn config set npmAlwaysAuth true
yarn config set npmRegistryServer "$NPM_REGISTRY"
yarn config set npmAuthIdent "$yarn_credentials"
- name: Set up npm credentials
if: inputs.npm-username && inputs.npm-password && inputs.npm-email && inputs.yarn-version < '2'
# https://github.com/npm/cli/issues/2852
shell: bash
env:
NPM_USER: ${{ inputs.npm-username }}
NPM_PASSWORD: ${{ inputs.npm-password }}
NPM_REGISTRY: ${{ inputs.npm-registry }}
NPM_EMAIL: ${{ inputs.npm-email }}
run: |
npm_credentials=$(echo -n "$NPM_USER:$NPM_PASSWORD" | base64)
npm config set _auth "$npm_credentials"
npm config set email "$NPM_EMAIL"
npm config set registry "$NPM_REGISTRY"
npm config set always-auth true
- name: Show yarn version
run: yarn --version
shell: bash
working-directory: ${{ inputs.working-directory }}
- name: Install Dependencies
id: install-dependencies
run: yarn install
shell: bash
working-directory: ${{ inputs.working-directory }}
- name: Regenerate yarn.lock on failure
if: ${{ failure() && steps.install-dependencies.conclusion == 'failure' }}
shell: bash
env:
YARN_CHECKSUM_BEHAVIOR: update
run: |
yarn install
(
echo '# Bad `yarn.lock`'
echo '## Bad local cache'
echo 'This could be caused by bad locally cached packages.'
echo 'Try running: [`yarn cache clean --all`](https://yarnpkg.com/cli/cache/clean#options)'
echo '## Generated `yarn.lock` file'
echo 'You can retrieve the `yarn.lock` file from artifacts (above)'
) >> "$GITHUB_STEP_SUMMARY"
working-directory: ${{ inputs.working-directory }}
- name: Upload regenerated yarn.lock on failure
if: ${{ failure() && steps.install-dependencies.conclusion == 'failure' }}
uses: actions/upload-artifact@v4
with:
name: ${{ inputs.working-directory != '.' && format('{0}-', inputs.working-directory) || '' }}yarn.lock
path: ${{ inputs.working-directory }}/yarn.lock
- name: Build
run: ${{ inputs.yarn-build-command }}
shell: bash
working-directory: ${{ inputs.working-directory }}
- name: Look for tests
id: test-searcher
if: inputs.test-file-pattern && fromJSON(inputs.test-file-pattern)
shell: bash
working-directory: ${{ inputs.working-directory }}
env:
SOURCE_DIRECTORY: ${{ inputs.source-directory }}
PATTERNS: ${{ inputs.test-file-pattern }}
run: |
TEST_FLAGS=$(
echo "$PATTERNS" |
jq -r '. | map(. | tojson) | map(" -name " + .) | join(" -o")'
)
if echo "$TEST_FLAGS" | xargs find "$SOURCE_DIRECTORY"
then
echo "has-tests=1" >> "$GITHUB_OUTPUT"
fi
- name: Test
if: steps.test-searcher.outputs.has-tests
shell: bash
run: ${{ inputs.yarn-test-command }}
working-directory: ${{ inputs.working-directory }}
- name: "Google Auth"
uses: "google-github-actions/auth@v2"
if: ${{ inputs.google-credentials-json && inputs.google-cloud-sdk-version && ! env.ACT }}
with:
credentials_json: "${{ inputs.google-credentials-json }}"
- name: Set up Cloud SDK
uses: google-github-actions/setup-gcloud@v2
if: ${{ inputs.google-credentials-json && inputs.google-cloud-sdk-version && ! env.ACT }}
with:
version: ${{ inputs.google-cloud-sdk-version }}
project_id: ${{ inputs.container-project }}
- name: Configure GCloud and Docker
if: ${{ inputs.google-credentials-json && inputs.google-cloud-sdk-version && ! env.ACT }}
shell: bash
run: |
gcloud auth configure-docker
- name: Package
if: ${{ ! inputs.skip-package }}
shell: bash
env:
WORKING_DIRECTORY: ${{ inputs.working-directory }}
BUILD_DIRECTORY: ${{ inputs.yarn-build-directory }}
TARBALL: ${{ inputs.build-output-tarball }}
run: tar czf "$TARBALL" *
working-directory: "${{ env.WORKING_DIRECTORY }}/${{ env.BUILD_DIRECTORY }}"
- name: Publish to NPM registry
if: inputs.publish-to-registry == 'true'
shell: bash
env:
BRANCH_NAME: ${{ github.event.pull_request.head.ref || github.ref_name }}
COMMIT_SHA: "${{ github.event.pull_request.head.sha || github.sha }}"
PUBLISH_BRANCH: ${{ inputs.publishing-branch }}
PUBLISHING_DIRECTORY: ${{ inputs.publishing-directory }}
run: |
version="$(jq -r < package.json '.version')"
if [ "$BRANCH_NAME" = "$PUBLISH_BRANCH" ]; then
if [ -e "$PUBLISHING_DIRECTORY"/package.json ]; then
cd $PUBLISHING_DIRECTORY
cp ../yarn.lock .
fi
jq '.version="'"$version"'"' package.json > new_package.json
mv new_package.json package.json
yarn npm publish
fi