-
Notifications
You must be signed in to change notification settings - Fork 4
237 lines (235 loc) · 8.59 KB
/
pre-build.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
name: pre-build
# Events configuration
on:
# Execute it on pushing to next branches
push:
branches:
- main
- release
# Execute it on opening any pull request to main or release
pull_request:
env:
# @INFO: If independent builds are enabled, only affected projects are going to be deployed.
IS_INDEPENDENT_BUILD_ENABLED: "true"
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NX_CLOUD_AUTH_TOKEN: ${{ secrets.NX_CLOUD_AUTH_TOKEN }}
NX_BRANCH: ${{ github.head_ref || github.ref_name }}
jobs:
# Get branch info
branch-info:
runs-on: ubuntu-latest
steps:
# Get current branch name
- name: Get branch name
id: branch-name
uses: tj-actions/branch-names@v6
# Get base branch name to compare with. Base branch on a PR, "main" branch on pushing.
- name: Get base branch name
id: get-base-branch-name
run: |
if [[ "${{github.event.pull_request.base.ref}}" != "" ]]; then
echo "branch=${{github.event.pull_request.base.ref}}" >> $GITHUB_OUTPUT
else
echo "branch=main" >> $GITHUB_OUTPUT
fi
outputs:
# Export the branch names as output to be able to use it in other jobs
base-branch-name: ${{ steps.get-base-branch-name.outputs.branch }}
branch-name: ${{ steps.branch-name.outputs.current_branch }}
get-affected:
needs: [branch-info]
runs-on: ubuntu-latest
steps:
# Check out the repository
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0
# Install Pnpm
- name: Install pnpm
uses: pnpm/action-setup@v2.2.4
with:
version: "7.x"
# Install Node.js
- name: Setup node
uses: actions/setup-node@v3
with:
node-version: "18.13.0"
cache: 'pnpm'
# Install workspace dependencies
- name: Install dependencies
run: pnpm install
# Configure Nx to be able to detect changes between branches when we are in a PR
- name: Derive shas for Nx
id: set-shas
uses: nrwl/nx-set-shas@v3
with:
main-branch-name: ${{needs.branch-info.outputs.base-branch-name}}
# Set the affected arrays
- name: Get affected
id: get-projects-arrays
# main and release branches will run all the tests once workflow is run
run: |
if [[ "${{github.event.pull_request.base.ref}}" == "" ]]; then
echo "lint=$(node tools/dev-scripts/cmd-print-affected-array.js lint)" >> $GITHUB_OUTPUT
echo "test-unit=$(node tools/dev-scripts/cmd-print-affected-array.js test-unit)" >> $GITHUB_OUTPUT
# Running on origin/release or origin/main branch
if [[ $IS_INDEPENDENT_BUILD_ENABLED == "true" ]]; then
echo "build=$(node tools/dev-scripts/cmd-print-affected-array.js build)" >> $GITHUB_OUTPUT
else
echo "build=$(node tools/dev-scripts/cmd-print-affected-array.js build --all)" >> $GITHUB_OUTPUT
fi
else
# Running on PR
echo "test-unit=$(node tools/dev-scripts/cmd-print-affected-array.js test origin/${{needs.branch-info.outputs.base-branch-name}})" >> $GITHUB_OUTPUT
echo "lint=$(node tools/dev-scripts/cmd-print-affected-array.js lint origin/${{needs.branch-info.outputs.base-branch-name}})" >> $GITHUB_OUTPUT
echo "build=[]" >> $GITHUB_OUTPUT
fi
- name: Print affected
run: |
echo "Test-Unit (Affected): ${{steps.get-projects-arrays.outputs.test-unit}}"
echo "Lint (Affected): ${{steps.get-projects-arrays.outputs.lint}}"
echo "Build (Affected): ${{steps.get-projects-arrays.outputs.build}}"
outputs:
test-unit: ${{ steps.get-projects-arrays.outputs.test-unit }}
lint: ${{ steps.get-projects-arrays.outputs.lint }}
build: ${{ steps.get-projects-arrays.outputs.build }}
lint:
runs-on: ubuntu-latest
needs: [get-affected]
# Skip the job if there are not affected projects containing lint
if: ${{ fromJson(needs.get-affected.outputs.build)[0] }}
strategy:
# Run in parallel
max-parallel: 4
# One job for each different project and node version
matrix:
node: ["18.13.0"]
projectName: ${{fromJson(needs.get-affected.outputs.build)}}
env:
NODE: ${{ matrix.node }}
steps:
# Checkout and setup environment
- name: Checkout
uses: actions/checkout@v3
- name: Install pnpm
uses: pnpm/action-setup@v2.2.4
with:
version: "7.x"
- name: Setup node
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node }}
cache: 'pnpm'
- name: Install dependencies
run: pnpm install
# Run test:unit script in the affected project
- name: Lint
run: pnpm nx run ${{ matrix.projectName }}:lint
test-unit:
runs-on: ubuntu-latest
needs: [get-affected]
# Skip the job if there are not affected projects containing unit tests
if: ${{ fromJson(needs.get-affected.outputs.build)[0] }}
strategy:
# Run in parallel
max-parallel: 4
# One job for each different project and node version
matrix:
node: ["18.13.0"]
projectName: ${{fromJson(needs.get-affected.outputs.build)}}
env:
NODE: ${{ matrix.node }}
steps:
# Checkout and setup environment
- name: Checkout
uses: actions/checkout@v3
- name: Install pnpm
uses: pnpm/action-setup@v2.2.4
with:
version: "7.x"
- name: Setup node
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node }}
cache: 'pnpm'
- name: Install dependencies
run: pnpm install
# Run test:unit script in the affected project
- name: Test unit
run: pnpm nx run ${{ matrix.projectName }}:test
pre-build-finished:
runs-on: ubuntu-latest
needs: [test-unit, lint, get-affected]
if: |
always() &&
(needs.get-affected.result == 'success') &&
(needs.test-unit.result == 'success' || needs.test-unit.result == 'skipped') &&
(needs.lint.result == 'success' || needs.lint.result == 'skipped')
steps:
- name: Trace
run: echo "All jobs finished"
tag:
runs-on: ubuntu-latest
needs: [pre-build-finished, branch-info]
if: |
always() &&
(needs.pre-build-finished.result == 'success') &&
(github.event.pull_request.base.ref == '') &&
(needs.branch-info.outputs.branch-name == 'main')
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0
persist-credentials: false
- name: Install pnpm
uses: pnpm/action-setup@v2.2.4
with:
version: "7.x"
- name: Setup node
uses: actions/setup-node@v3
with:
node-version: "18.13.0"
cache: 'pnpm'
- name: Install dev dependencies
run: pnpm install --dev
- name: Trigger semantic-release
id: trigger-semantic-release
env:
GITHUB_TOKEN: ${{ secrets.GH_TOKEN_SEMANTIC_RELEASE }}
run: |
pnpm semantic-release --dry-run --branches main --no-ci
test -e .VERSION || (echo $(git describe --abbrev=0 --tags | tr -d v) > .VERSION && touch .NORELEASE)
echo "tag=$(cat .VERSION)" >> $GITHUB_OUTPUT
pnpm semantic-release
outputs:
tag: ${{ steps.trigger-semantic-release.outputs.tag }}
dispatch-build:
runs-on: ubuntu-latest
needs: [branch-info, get-affected, pre-build-finished, tag]
# Only dispatch if:
# - Any project build is affected by the latest changes.
# - The branch is main or release.
if: |
always() &&
(needs.pre-build-finished.result == 'success') &&
(needs.tag.result == 'success' || needs.tag.result == 'skipped') &&
fromJson(needs.get-affected.outputs.build)[0] &&
(github.event.pull_request.base.ref == '') &&
contains(fromJson('["main"]'), needs.branch-info.outputs.branch-name)
env:
BUILD_REQUIRED_APPS: ${{needs.get-affected.outputs.build}}
BRANCH_NAME: ${{needs.branch-info.outputs.branch-name}}
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Print dispatch variables
run: |
echo "BRANCH_NAME=$BRANCH_NAME"
echo "BUILD_REQUIRED_APPS=$BUILD_REQUIRED_APPS"
echo "TAG=${{needs.tag.outputs.tag}}"
- name: Dispatch build
run: |
echo "Dispatching build event..."
gh workflow run build.yml -f apps=$BUILD_REQUIRED_APPS