-
Notifications
You must be signed in to change notification settings - Fork 7
260 lines (234 loc) · 9.63 KB
/
nx-cloud-agents.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
name: Nx Cloud Agents
on:
workflow_call:
secrets:
NPM_TOKEN:
required: false
NX_CLOUD_ACCESS_TOKEN:
required: false
NX_CLOUD_AUTH_TOKEN:
required: false
CODECOV_TOKEN:
required: false
BOT_APP_ID:
required: false
BOT_PRIVATE_KEY:
required: false
inputs:
enable-github-app:
required: false
type: boolean
default: false
number-of-agents:
required: true
type: number
environment-variables:
required: false
type: string
node-version:
required: false
type: string
yarn-version:
required: false
type: string
npm-version:
required: false
type: string
pnpm-version:
required: false
type: string
install-commands:
required: false
type: string
runs-on:
required: false
type: string
default: ubuntu-latest
# We needed this input in order to be able to configure out integration tests for this repo, it is not documented
# so as to not cause confusion/add noise, but technically any consumer of the workflow can use it if they want to.
working-directory:
required: false
type: string
env:
NX_CLOUD_DISTRIBUTED_EXECUTION: true
NX_BRANCH: ${{ github.event.number || github.ref_name }}
NX_CLOUD_ACCESS_TOKEN: ${{ secrets.NX_CLOUD_ACCESS_TOKEN }}
NX_CLOUD_AUTH_TOKEN: ${{ secrets.NX_CLOUD_AUTH_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
jobs:
set-agents:
runs-on: ${{ inputs.runs-on }}
name: Init
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }}
steps:
- id: set-matrix
shell: bash
# Turn the number-of-agents input into a JSON structure which is compatible with a Github job matrix strategy
run: |
AGENTS_JSON_ARRAY=$(node -e "console.log(JSON.stringify(Array.from(new Array(${{ inputs.number-of-agents }})).map((_, i) => i + 1)));")
echo $AGENTS_JSON_ARRAY
echo "matrix=$AGENTS_JSON_ARRAY" >> $GITHUB_OUTPUT
# Intentionally using capital letter in order to make the Github UI for the matrix look better
Run:
needs: set-agents
runs-on: ${{ inputs.runs-on }}
name: Agent ${{ matrix.agent }}
strategy:
matrix:
agent:
- ${{fromJson(needs.set-agents.outputs.matrix)}}
defaults:
run:
working-directory: ${{ inputs.working-directory || github.workspace }}
# Specify shell to help normalize across different operating systems
shell: bash
steps:
- name: Generate token
id: generate_token
if: ${{ inputs.enable-github-app }}
uses: tibdex/github-app-token@v1
with:
app_id: ${{ secrets.BOT_APP_ID }}
private_key: ${{ secrets.BOT_PRIVATE_KEY }}
- uses: actions/checkout@v3
with:
token: ${{ steps.generate_token.outputs.token }}
- name: Detect package manager
id: package_manager
run: |
echo "name=$([[ -f ./yarn.lock ]] && echo "yarn" || ([[ -f ./pnpm-lock.yaml ]] && echo "pnpm") || echo "npm")" >> $GITHUB_OUTPUT
# Set node/npm/yarn versions using volta, with optional overrides provided by the consumer
- uses: volta-cli/action@v4
with:
node-version: '${{ inputs.node-version }}'
npm-version: '${{ inputs.npm-version }}'
yarn-version: '${{ inputs.yarn-version }}'
# Install pnpm with exact version provided by consumer or fallback to latest
- name: Install PNPM
if: steps.package_manager.outputs.name == 'pnpm'
uses: pnpm/action-setup@v2
with:
version: ${{ inputs.pnpm-version || 'latest' }}
- name: Print node/npm/yarn versions
id: versions
run: |
node_ver=$( node --version )
yarn_ver=$( yarn --version || true )
pnpm_ver=$( pnpm --version || true )
echo "Node: ${node_ver:1}"
echo "NPM: $( npm --version )"
if [[ $yarn_ver != '' ]]; then echo "Yarn: $yarn_ver"; fi
if [[ $pnpm_ver != '' ]]; then echo "PNPM: $pnpm_ver"; fi
echo "node_version=${node_ver:1}" >> $GITHUB_OUTPUT
echo "yarn_version=${yarn_ver}" >> $GITHUB_OUTPUT
- name: Use the node_modules cache if available [npm]
if: steps.package_manager.outputs.name == 'npm'
uses: actions/cache@v3
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ steps.versions.outputs.node_version }}-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-${{ steps.versions.outputs.node_version }}-
- name: Get pnpm cache directory path
if: steps.package_manager.outputs.name == 'pnpm'
id: pnpm-cache-dir-path
run: echo "dir=$(pnpm store path)" >> $GITHUB_OUTPUT
- name: Use the node_modules cache if available [pnpm]
if: steps.package_manager.outputs.name == 'pnpm'
uses: actions/cache@v3
with:
path: ${{ steps.pnpm-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-node-${{ steps.versions.outputs.node_version }}-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-node-${{ steps.versions.outputs.node_version }}-
- name: Get yarn cache directory path [yarn 1.x]
if: steps.package_manager.outputs.name == 'yarn' && startsWith(steps.versions.outputs.yarn_version, '1.')
id: yarn-cache-dir-path
run: echo "dir=$(yarn cache dir)" >> $GITHUB_OUTPUT
- name: Use the node_modules cache if available [yarn 1.x]
if: steps.package_manager.outputs.name == 'yarn' && startsWith(steps.versions.outputs.yarn_version, '1.')
uses: actions/cache@v3
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-node-${{ steps.versions.outputs.node_version }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-node-${{ steps.versions.outputs.node_version }}-yarn-
- name: Get yarn cache directory path [yarn berry]
if: steps.package_manager.outputs.name == 'yarn' && !startsWith(steps.versions.outputs.yarn_version, '1.')
id: yarn-berry-cache-dir-path
run: echo "dir=$(yarn config get cacheFolder)" >> $GITHUB_OUTPUT
- name: Use the node_modules cache if available [yarn berry]
if: steps.package_manager.outputs.name == 'yarn' && !startsWith(steps.versions.outputs.yarn_version, '1.')
uses: actions/cache@v3
with:
path: ${{ steps.yarn-berry-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-node-${{ steps.versions.outputs.node_version }}-yarn-berry-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-node-${{ steps.versions.outputs.node_version }}-yarn-berry-
- name: Process environment-variables
if: ${{ inputs.environment-variables != '' }}
uses: actions/github-script@v6
env:
ENV_VARS: ${{ inputs.environment-variables }}
with:
script: |
const { appendFileSync } = require('fs');
// trim spaces and escape quotes
const cleanStr = str => str
.trim()
.replaceAll(/`/g, "\`");
// parse variable to correct type
const parseStr = str =>
str === 'true' || str === 'TRUE'
? true
: str === 'false' || str === 'FALSE'
? false
: isNaN(str)
? str
: parseFloat(str);
const varsStr = process.env.ENV_VARS || '';
const vars = varsStr
.split('\n')
.map(variable => variable.trim())
.filter(variable => variable.indexOf('=') > 0)
.map(variable => ({
name: cleanStr(variable.split('=')[0]),
value: cleanStr(variable.slice(variable.indexOf('=') + 1))
}));
for (const v of vars) {
console.log(`Appending environment variable \`${v.name}\` with value \`${v.value}\` to ${process.env.GITHUB_ENV}`);
appendFileSync(process.env.GITHUB_ENV, `${v.name}=${parseStr(v.value)}\n`);
}
- name: Run any configured install-commands
if: ${{ inputs.install-commands != '' }}
env:
GITHUB_TOKEN: ${{ steps.generate_token.outputs.token }}
GH_TOKEN: ${{ steps.generate_token.outputs.token }}
run: |
${{ inputs.install-commands }}
- name: Install dependencies
if: ${{ inputs.install-commands == '' }}
run: |
if [ "${{ steps.package_manager.outputs.name == 'yarn' }}" == "true" ]; then
if [ "${{ startsWith(steps.versions.outputs.yarn_version, '1.') }}" == "true" ]; then
echo "Running yarn install --frozen-lockfile"
yarn install --frozen-lockfile
else
echo "Running yarn install --immutable"
yarn install --immutable
fi
elif [ "${{ steps.package_manager.outputs.name == 'pnpm' }}" == "true" ]; then
echo "Running pnpm install --frozen-lockfile"
pnpm install --frozen-lockfile
else
echo "Running npm ci"
npm ci
fi
- name: Start Nx Agent ${{ matrix.agent }}
run: npx nx-cloud start-agent
env:
NX_AGENT_NAME: ${{matrix.agent}}
GITHUB_TOKEN: ${{ steps.generate_token.outputs.token }}
GH_TOKEN: ${{ steps.generate_token.outputs.token }}