This repository has been archived by the owner on Mar 29, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.yml
205 lines (177 loc) · 7.04 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
name: "NodeJS Continuous Integration steps"
description: "Composite Github Action to provides opinionated NodeJS steps to performs Continuous Integration"
branding:
icon: "box"
color: "green"
inputs:
checkout:
description: Checkout parameters. Must be a json object. See https://github.com/actions/checkout
default: "{ }"
package-manager:
description: "Used to specify a package manager. Supported values: 'yarn'"
default: "yarn"
build:
description: Build parameters. Must be a string or a json object.
default: "build"
checks:
description: "Optional flag to enable check steps."
lint:
description: "Optional flag to enable linting"
default: "true"
code-ql:
description: "Code QL analysis language. See https://github.com/github/codeql-action"
default: "typescript"
test:
description: "Optional flag to enable test. See https://github.com/github/codeql-action"
default: "true"
coverage:
description: "Optional flag to enable coverage report. See https://github.com/codecov/codecov-action"
default: "true"
runs:
using: "composite"
steps:
- id: prepare-checkout
if: inputs.checkout != 'false'
uses: actions/github-script@v7.0.1
with:
script: |
const checkoutInput = `${{ inputs.checkout }}`;
core.debug(`Provided "checkout" input: ${checkoutInput}`);
if(!checkoutInput){
core.setFailed(`Input "checkout" is empty`);
return;
}
const checkoutParams = JSON.parse(checkoutInput);
if(typeof checkoutParams !== 'object'){
core.setFailed('Given "checkout" input is not a valid JSON object.');
return;
}
if(checkoutParams.token){
core.info('Checkout token has been provided');
core.setOutput('token', checkoutParams.token);
}
- uses: actions/checkout@v4.1.2
if: inputs.checkout != 'false'
with:
token: ${{ steps.prepare-checkout.outputs.token || github.token }}
- uses: actions/setup-node@v4.0.2
id: setup-node
with:
node-version-file: ".nvmrc"
cache: ${{ inputs.package-manager }}
cache-dependency-path: "**/yarn.lock"
- name: ⚙️ Install Dependencies
shell: bash
run: yarn install --frozen-lockfile;
- id: yarn-installed-packages
if: inputs.build != 'false'
shell: bash
run: |
echo "nx=$([[ `yarn list --pattern "nx" --depth=1 | grep " nx@"` ]] && echo "true" || echo "false")" >> $GITHUB_OUTPUT
echo "gatsby=$([[ `yarn list --pattern "gatsby" --depth=1 | grep " gatsby@"` ]] && echo "true" || echo "false")" >> $GITHUB_OUTPUT
echo "storybook=$([[ `yarn list --pattern "storybook" --depth=1 | grep " @storybook/[-a-z]*@"` ]] && echo "true" || echo "false")" >> $GITHUB_OUTPUT
echo "prettier=$([[ `yarn list --pattern "prettier" --depth=1 | grep " prettier@"` ]] && echo "true" || echo "false")" >> $GITHUB_OUTPUT
- name: ♻️ NX cache
if: inputs.build != 'false' && steps.yarn-installed-packages.outputs.nx == 'true'
uses: actions/cache@v4.0.2
with:
path: node_modules/.cache/nx
key: ${{ runner.os }}-cache-nx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-cache-nx-
- name: ♻️ Prettier cache
if: inputs.checks == 'true' && inputs.lint == 'true' && steps.yarn-installed-packages.outputs.prettier == 'true'
uses: actions/cache@v4.0.2
with:
path: node_modules/.cache/prettier
key: ${{ runner.os }}-cache-prettier-${{ github.sha }}
restore-keys: |
${{ runner.os }}-cache-prettier-
- name: 👕 Lint
if: inputs.checks == 'true' && inputs.lint == 'true'
shell: bash
run: yarn lint
- name: 🛡️ CodeQL Analysis
if: inputs.checks == 'true' && inputs.code-ql != 'false'
uses: github/codeql-action/init@v3.24.9
with:
languages: ${{ inputs.code-ql }}
- uses: github/codeql-action/analyze@v3.24.9
if: inputs.checks == 'true' && inputs.code-ql != 'false'
- name: ♻️ Gatsby cache
if: inputs.build != 'false' && steps.yarn-installed-packages.outputs.gatsby == 'true'
uses: actions/cache@v4.0.2
with:
path: |
.cache
public
key: ${{ runner.os }}-cache-gatsby-${{ github.sha }}
restore-keys: |
${{ runner.os }}-cache-gatsby-
- name: ♻️ Storybook cache
if: inputs.build != 'false' && steps.yarn-installed-packages.outputs.storybook == 'true'
uses: actions/cache@v4.0.2
with:
path: node_modules/.cache/storybook
key: ${{ runner.os }}-cache-storybook-${{ github.sha }}
restore-keys: |
${{ runner.os }}-cache-storybook-
- id: prepare-build
uses: actions/github-script@v7.0.1
with:
script: |
const buildInput = `${{ inputs.build }}`;
core.debug(`Provided "build" input: ${buildInput}`);
core.setOutput('env', {});
core.setOutput('command', null);
if(!buildInput || buildInput === 'false'){
return;
}
const isJsonString = (str) => { try { JSON.parse(str); return true; } catch (e) { return false; }};
if(!isJsonString(buildInput)){
core.info('Build command has been provided');
core.setOutput('command', buildInput);
return;
}
const buildParams = JSON.parse(buildInput);
if(typeof buildParams === 'object'){
if(buildParams.command){
core.info('Build command has been provided');
core.setOutput('command', buildParams.command);
}
if(buildParams.env){
core.info('Build env has been provided');
core.setOutput('env', buildParams.env);
}
return;
}
core.setFailed('Given \"build\" input is not a string nor a JSON object.');
- name: 🏗️ Build
shell: bash
if: inputs.build != 'false' && steps.prepare-build.outputs.command
env: ${{ fromJSON(steps.prepare-build.outputs.env) }}
run: yarn ${{ steps.prepare-build.outputs.command }}
- name: ♻️ Get Jest cache dir
if: inputs.checks == 'true' && inputs.test == 'true'
id: jest-cache-dir-path
shell: bash
run: |
JEST_CACHE_DIR=$(yarn jest --showConfig | grep -oP '(?<="cacheDirectory": ")[^"]+(?=")')
echo "dir=$JEST_CACHE_DIR" >> $GITHUB_OUTPUT
- name: ♻️ Test cache
if: inputs.checks == 'true' && inputs.test == 'true'
uses: actions/cache@v4.0.2
with:
path: ${{ steps.jest-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-test-${{ github.sha }}
restore-keys: |
${{ runner.os }}-test-
- name: 🧪 Test
if: inputs.checks == 'true' && inputs.test == 'true'
shell: bash
env:
CI: "true"
run: yarn test:ci
- name: 📊 Code coverage
if: inputs.checks == 'true' && inputs.coverage == 'true'
uses: codecov/codecov-action@v4.1.1