-
Notifications
You must be signed in to change notification settings - Fork 0
296 lines (257 loc) · 10.9 KB
/
ci.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
# name of the workflow. Link to the documentation - https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#name
name: CI
# running on push to main and develop branches or on pull reuqests or on manual trigger
on:
# manual trigger
workflow_dispatch:
inputs:
ssh_debug_enabled:
type: boolean
description: 'Run the build/test with ssh debugging enabled (https://github.com/marketplace/actions/debugging-with-tmate)'
required: false
default: false
debug_deployment:
type: string
description: 'Run the pipeline with debug deployment enabled'
required: false
default: 'false'
push:
branches:
- main
- develop
paths-ignore:
- '**/README.md'
- '.devcontainer/**'
- '.github/ISSUE_TEMPLATE/**'
- '.github/workflows/housekeeping*.yml'
pull_request_target:
branches:
- main
- develop
paths-ignore:
- '**/README.md'
- '.devcontainer/**'
- '.github/ISSUE_TEMPLATE/**'
- '.github/workflows/housekeeping*.yml'
# defining global environment variables for all jobs
env:
# define runner indexes for tests splitting and parallel execution
total-runners: 4
# defining GitHub registry for docker images
REGISTRY: ghcr.io
# github.repository as <account>/<repo>
IMAGE_NAME: ${{ github.repository }}
jobs:
build:
runs-on: ${{ matrix.runner }}
name: Build (${{ matrix.language }})
permissions:
actions: read
contents: read
packages: write
id-token: write
security-events: write
strategy:
matrix:
include:
- language: csharp
build-mode: manual
runner: tsvi-linux8cores
- language: javascript-typescript
build-mode: none
runner: ubuntu-latest
steps:
- name: Checkout code
uses: actions/[email protected]
- name: Initialize CodeQL
uses: github/codeql-action/init@v3
with:
languages: ${{ matrix.language }}
build-mode: ${{ matrix.build-mode }}
- name: Setup .NET
uses: actions/[email protected]
with:
dotnet-version: '6.0.x'
- name: Cache NuGet packages
if: matrix.build-mode == 'manual'
uses: actions/[email protected]
with:
path: ~/.nuget/packages
key: ${{ runner.os }}-nuget-${{ hashFiles('**/*.csproj', '**/global.json') }}
restore-keys: |
${{ runner.os }}-nuget-
- name: Restore dependencies
if: matrix.language == 'csharp' && matrix.build-mode == 'manual'
run: dotnet restore RazorPagesMovie.sln
- name: Build App project
if: matrix.language == 'csharp' && matrix.build-mode == 'manual'
run: dotnet build RazorPagesMovie.sln --configuration Release --no-restore
# - name: Set runtime
# if: matrix.language == 'csharp'
# id: set-runtime
# run: echo "RUNTIME=${{ matrix.os == 'ubuntu-latest' && 'linux-x64' || matrix.os == 'windows-latest' && 'win-x64' || 'osx-x64' }}" >> $GITHUB_ENV
- name: Publish
if: matrix.language == 'csharp' && matrix.build-mode == 'manual'
run: dotnet publish RazorPagesMovie.csproj --configuration Release --output publish --self-contained --runtime linux-x64
working-directory: src
- name: Upload published app
if: matrix.language == 'csharp' && matrix.build-mode == 'manual'
uses: actions/[email protected]
with:
name: razor-linux-arm64
path: src/publish/
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v3
with:
category: "/language:${{matrix.language}}"
test:
runs-on: ${{ matrix.os }}
needs:
- build
- runner-indexes
permissions:
contents: read # read access to the repository contents
packages: write # write access to the repository packages
id-token: write # write access to the repository id token
strategy:
matrix:
# os: [ubuntu-latest, windows-latest, macos-latest]
os: [ubuntu-latest]
index: ${{ fromJson(needs.runner-indexes.outputs.json) }}
steps:
- name: Checkout code
uses: actions/[email protected]
- name: Setup .NET
uses: actions/[email protected]
with:
dotnet-version: '6.0.x'
- name: Cache NuGet packages
uses: actions/[email protected]
with:
path: ~/.nuget/packages
key: ${{ runner.os }}-nuget-${{ hashFiles('**/*.csproj', '**/global.json') }}
restore-keys: |
${{ runner.os }}-nuget-
- name: Setup tmate session
uses: mxschmitt/[email protected]
if: ${{ github.event_name == 'workflow_dispatch' && inputs.ssh_debug_enabled }}
- name: Split Tests
id: split-test
uses: scruplelesswizard/split-tests@4f1ca766cb93923ca216e02f1aefed20944e313f
with:
glob: tests/RazorPagesMovie.Tests/**/*Tests.cs
split-total: ${{ env.total-runners }}
split-index: ${{ matrix.index }}
line-count: true
- name: Restore dependencies
run: dotnet restore RazorPagesMovie.Tests/RazorPagesMovie.Tests.csproj
working-directory: tests
- name: Convert Test File Path to Fully Qualified Name
id: convert-path
run: |
test_suite="${{ steps.split-test.outputs.test-suite }}"
echo "test_suite=$test_suite"
fully_qualified_name=$(echo $test_suite | sed 's/\//./g' | sed 's/.cs//g' | sed 's/^tests\.//g' | xargs)
echo "fully_qualified_name=$fully_qualified_name" >> $GITHUB_ENV
working-directory: tests
- run: 'echo "This runner will execute the following tests: ${{ steps.split-test.outputs.test-suite }}"'
- run: 'echo "Fully qualified name: ${{ env.fully_qualified_name }}"'
- run: |
dotnet test RazorPagesMovie.Tests/RazorPagesMovie.Tests.csproj \
--filter "FullyQualifiedName~${{ env.fully_qualified_name }}" \
--logger "console;verbosity=detailed" \
--logger "trx;LogFileName=testresults-${{ matrix.index }}-testresults-${{ matrix.os }}-${{ github.run_id }}-${{ github.run_attempt }}.trx" \
--results-directory testresults
working-directory: tests
- name: Upload test results
if: always()
uses: actions/[email protected]
with:
name: testresults-${{ github.run_id }}-split-${{ matrix.index }}
path: tests/testresults/
if-no-files-found: warn
compression-level: 6
publish-test-results:
needs: test
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/[email protected]
- name: List Artifacts
id: list-artifacts
run: |
curl -s -u ${{ github.actor }}:${{ secrets.GITHUB_TOKEN }} \
-H 'Accept: application/vnd.github.v3+json' \
https://api.github.com/repos/octodemo/dotnet-razor-pages-movie/actions/runs/${{ github.run_id }}/artifacts > artifacts.json
- name: Download Artifacts
run: |
mkdir -p test_results
for url in $(jq -r '.artifacts[] | select(.name | startswith("testresults-")) | .archive_download_url' artifacts.json); do
artifact_name=$(echo $url | awk -F/ '{print $NF}' | awk -F? '{print $1}')
curl -s -u ${{ github.actor }}:${{ secrets.GITHUB_TOKEN }} -L -o test_results/testresults.zip $url
unzip -o test_results/testresults.zip -d test_results
rm test_results/testresults.zip
done
- name: Publish Test Results
uses: dorny/[email protected]
if: always()
with:
reporter: dotnet-trx
name: xUnit Test Results
path: test_results/**/*.trx
build-and-publish-docker-image: # job to build the docker image and publish it to the GitHub Container Registry
runs-on: ubuntu-latest # using the latest ubuntu runner
outputs:
image_tag: ${{ github.run_number }} # output the image tag to be used in the build-and-publish-docker-image job
needs: [build, test] # depend on the build job to get the published app artifact
if: github.event_name == 'push' || (github.event_name == 'pull_request_target' && github.base_ref == 'main' && github.head_ref == 'develop')
permissions:
packages: write
id-token: write
contents: write
steps:
- name: Checkout repository
uses: actions/[email protected]
- uses: actions/[email protected] # download the published app artifact from the build job
with:
name: razor-linux-arm64
path: publish/
# build the docker image using the Dockerfile in the root of the repository
# and tag it with the current run number from the github action workflow run
- name: Log in to the GH Container Registry
uses: docker/[email protected] # using the docker login action from the github marketplace - github.com/marketplace/actions/docker-login
with:
registry: ${{ env.REGISTRY }} # using the registry environment variable
username: ${{ github.actor }} # using the github.actor context
password: ${{ secrets.GITHUB_TOKEN }} # using the GITHUB_TOKEN secret
- name: Build and push Docker image
id: build_image
uses: docker/[email protected] # using the docker build and push action from the github marketplace - github.com/marketplace/actions/build-and-push-docker-images
with:
context: . # using the current directory as the context
push: true # push the docker image to the registry
tags: |
ghcr.io/${{ github.repository }}:${{ github.run_number }}
ghcr.io/${{ github.repository }}:latest
cache-from: type=registry,ref=ghcr.io/${{ github.repository }}:latest # use the docker layer caching to speed up the docker image build process
cache-to: type=inline
deploy:
needs: [build-and-publish-docker-image] # this job needs build-and-publish-docker-image job as a requirement to run
uses: ./.github/workflows/cd.yml
with:
# with tag from the build-and-publish-docker-image job in the output_tags step
image_tag: "${{ needs.build-and-publish-docker-image.outputs.image_tag }}"
debug: "${{ github.event.inputs.debug_deployment }}"
secrets: inherit
runner-indexes: # job to generate the runner indexes for the unit-parallel-tests job
runs-on: ubuntu-latest
name: Generate runner indexes
outputs:
json: ${{ steps.generate-index-list.outputs.json }} # output the json with the runner indexes
steps:
- id: generate-index-list # generate the runner indexes and save them to the json file
run: |
MAX_INDEX=$((${{ env.total-runners }}-1)) # calculate the max index
INDEX_LIST=$(seq 0 ${MAX_INDEX}) # generate the list of indexes
INDEX_JSON=$(jq --null-input --compact-output '. |= [inputs]' <<< ${INDEX_LIST}) # convert the list to the json
echo "json=${INDEX_JSON}" >> $GITHUB_OUTPUT # save the json to the GITHUB_OUTPUT environment variable