Repository navigation
266 lines (244 loc) · 11.9 KB
/
Copy pathci-cd.yml
File metadata and controls
266 lines (244 loc) · 11.9 KB
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
name: CI/CD Pipeline
on: [push, pull_request, workflow_dispatch]
permissions:
contents: write
jobs:
ci:
name: Continuous Integration
runs-on: ubuntu-latest
outputs:
latest_version: ${{ steps.tag_generator.outputs.new_version }}
is_default_branch: ${{ steps.conditionals_handler.outputs.is_default_branch }}
env:
ARTIFACTS_FOLDER: ${{ github.workspace }}/Artifacts
GITHUB_RUN_NUMBER: ${{ github.run_number }}
steps:
- name: Setup .NET
uses: actions/setup-dotnet@a98b56852c35b8e3190ac28c8c2271da59106c68 # v6.0.0
with:
dotnet-version: |
8.0.x
10.0.x
- name: Data gatherer
id: data_gatherer
shell: pwsh
env:
GITHUB_TOKEN: ${{ github.token }}
run: |
# Get default branch
$repo = 'microsoft/OpenAPI.NET.OData'
$defaultBranch = Invoke-RestMethod -Method GET -Uri https://api.github.com/repos/$repo -Headers @{ Authorization = "Bearer $env:GITHUB_TOKEN" } | Select-Object -ExpandProperty default_branch
Write-Output "default_branch=$(echo $defaultBranch) >> $GITHUB_OUTPUT"
- name: Conditionals handler
id: conditionals_handler
shell: pwsh
run: |
$defaultBranch = "${{ steps.data_gatherer.outputs.default_branch }}"
$githubRef = "${{ github.ref }}"
$isDefaultBranch = 'false'
if ( $githubRef -like "*$defaultBranch*" ) {
$isDefaultBranch = 'true'
}
Write-Output "is_default_branch=$(echo $isDefaultBranch) >> $GITHUB_OUTPUT"
- name: Checkout repository
id: checkout_repo
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
token: ${{ secrets.GITHUB_TOKEN }}
fetch-depth: 0
- if: steps.conditionals_handler.outputs.is_default_branch == 'true'
name: Bump GH tag
id: tag_generator
uses: mathieudutour/github-tag-action@af99e60ce8132224b8e6ebab5023449fe256ed46 # v7.0.0
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
default_bump: false
release_branches: ${{ steps.data_gatherer.outputs.default_branch }}
- name: Build projects
id: build_projects
shell: pwsh
run: |
$projectsArray = @(
'.\src\Microsoft.OpenApi.OData.Reader\Microsoft.OpenAPI.OData.Reader.csproj'
)
$gitNewVersion = if ("${{ steps.tag_generator.outputs.new_version }}") {"${{ steps.tag_generator.outputs.new_version }}"} else {$null}
$projectCurrentVersion = ([xml](Get-Content .\src\Microsoft.OpenApi.OData.Reader\Microsoft.OpenAPI.OData.Reader.csproj)).Project.PropertyGroup.Version
$projectNewVersion = $gitNewVersion ?? $projectCurrentVersion
$projectsArray | ForEach-Object {
dotnet build $PSItem `
-c Release # `
# -o $env:ARTIFACTS_FOLDER `
# /p:Version=$projectNewVersion
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
}
# Move NuGet packages to separate folder for pipeline convenience
# New-Item Artifacts/NuGet -ItemType Directory
# Get-ChildItem Artifacts/*.nupkg | Move-Item -Destination "Artifacts/NuGet"
- name: Run unit tests
id: run_unit_tests
shell: pwsh
run: |
$testProjectsArray = @(
'.\test\Microsoft.OpenAPI.OData.Reader.Tests\Microsoft.OpenAPI.OData.Reader.Tests.csproj'
)
$testProjectsArray | ForEach-Object {
dotnet test $PSItem `
-c Release
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
}
- name: Run hidi tests (Microsoft.Testing.Platform)
shell: pwsh
run: |
dotnet run --project .\test\Microsoft.OpenApi.Hidi.Tests\Microsoft.OpenApi.Hidi.Tests.csproj -c Release -- --minimum-expected-tests 1
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
- name: Pack and install hidi locally
shell: pwsh
run: |
dotnet pack .\src\Microsoft.OpenApi.Hidi\Microsoft.OpenApi.Hidi.csproj -c Release --no-build -o .\artifacts\hidi\nuget
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
.\install-tool.ps1
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
$workspace = (Get-Location).Path
$fixtures = Join-Path $workspace 'test' 'Microsoft.OpenApi.Hidi.Tests' 'UtilityFiles'
$outputs = Join-Path $workspace 'artifacts' 'hidi'
$hidi = Join-Path $outputs 'installed' 'hidi'
$sampleInput = Join-Path $fixtures 'SampleOpenApi.yml'
$csdlInput = Join-Path $fixtures 'Todo.xml'
$jsonOutput = Join-Path $outputs 'openapi.json'
$defaultOutput = Join-Path $outputs 'openapi-default.json'
$csdlOutput = Join-Path $outputs 'csdl.yaml'
$diagramOutput = Join-Path $outputs 'diagram.md'
& $hidi --help
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
& $hidi validate --openapi $sampleInput
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
& $hidi transform --openapi $sampleInput --format json --version 3.0 --output $jsonOutput --clean-output
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
& $hidi transform --csdl $csdlInput --format yaml --version 3.0 --output $csdlOutput --clean-output
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
$document = Get-Content $jsonOutput -Raw | ConvertFrom-Json
if ($document.openapi -notlike '3.0.*' -or @($document.paths.PSObject.Properties).Count -eq 0) {
throw "Expected an OpenAPI 3.0 document with paths from SampleOpenApi."
}
& $hidi transform --openapi $sampleInput --format json --output $defaultOutput --clean-output
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
$defaultDocument = Get-Content $defaultOutput -Raw | ConvertFrom-Json
if ($defaultDocument.openapi -notlike '3.2.*' -or @($defaultDocument.paths.PSObject.Properties).Count -eq 0) {
throw "Expected main's default OpenAPI 3.2 behavior with paths."
}
$csdlDocument = Get-Content $csdlOutput -Raw
if ($csdlDocument -notmatch '(?m)^openapi:\s*3\.0\.' -or $csdlDocument -notmatch '(?m)^ /Todos:\s*$' -or $csdlDocument -notmatch '(?m)^ schemas:\s*$') {
throw "Expected OpenAPI 3.0, /Todos paths and schemas in the CSDL YAML output."
}
& $hidi show --openapi $sampleInput --output $diagramOutput
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
if (-not (Select-String $diagramOutput -Pattern 'mermaid' -Quiet)) { throw "Expected a Mermaid diagram from hidi show." }
- name: Upload hidi NuGet artifact (not a release)
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: hidi-nuget
path: artifacts/hidi/nuget
if-no-files-found: error
# - if: steps.tag_generator.outputs.new_version != ''
# name: Upload NuGet packages as artifacts
# id: ul_packages_artifact
# uses: actions/upload-artifact@v1
# with:
# name: NuGet packages
# path: Artifacts/NuGet/
hidi-windows:
name: Hidi Windows executable
runs-on: windows-latest
permissions:
contents: read
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- uses: actions/setup-dotnet@a98b56852c35b8e3190ac28c8c2271da59106c68 # v6.0.0
with:
dotnet-version: |
8.0.x
10.0.x
- name: Publish and smoke-test Windows executable
shell: pwsh
run: |
dotnet publish .\src\Microsoft.OpenApi.Hidi\Microsoft.OpenApi.Hidi.csproj -c Release -r win-x64 --self-contained true -p:PublishSingleFile=true -p:PackAsTool=false -p:GeneratePackageOnBuild=false -o .\artifacts\hidi\win-x64
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
& .\artifacts\hidi\win-x64\Microsoft.OpenApi.Hidi.exe --help
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
Compress-Archive -Path .\artifacts\hidi\win-x64\* -DestinationPath .\artifacts\hidi\hidi-win-x64.zip
- uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: hidi-windows-executable
path: |
artifacts/hidi/hidi-win-x64.zip
artifacts/hidi/win-x64/Microsoft.OpenApi.Hidi.exe
if-no-files-found: error
hidi-docker:
name: Hidi Docker build
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- name: Build and smoke-test Docker image (no push)
run: |
set -euo pipefail
docker build --tag hidi:ci .
docker run --rm hidi:ci --help
mkdir -p artifacts/hidi/docker
docker run --rm --user "$(id -u):$(id -g)" \
--mount "type=bind,source=$PWD/test/Microsoft.OpenApi.Hidi.Tests/UtilityFiles/SampleOpenApi.yml,target=/app/openapi.yml,readonly" \
--mount "type=bind,source=$PWD/artifacts/hidi/docker,target=/app/output" \
hidi:ci transform --openapi /app/openapi.yml --format json --output /app/output/openapi.json --clean-output
docker run --rm --user "$(id -u):$(id -g)" \
--mount "type=bind,source=$PWD/test/Microsoft.OpenApi.Hidi.Tests/UtilityFiles/Todo.xml,target=/app/api.csdl,readonly" \
--mount "type=bind,source=$PWD/artifacts/hidi/docker,target=/app/output" \
hidi:ci transform --csdl /app/api.csdl --format yaml --version 3.0 --output /app/output/csdl.yaml --clean-output
- name: Verify Docker transformation outputs
shell: pwsh
run: |
$outputs = Join-Path (Get-Location).Path 'artifacts' 'hidi' 'docker'
$document = Get-Content (Join-Path $outputs 'openapi.json') -Raw | ConvertFrom-Json
if ($document.openapi -notlike '3.2.*' -or @($document.paths.PSObject.Properties).Count -eq 0) {
throw "Expected main's default OpenAPI 3.2 behavior with paths in Docker."
}
$csdlDocument = Get-Content (Join-Path $outputs 'csdl.yaml') -Raw
if ($csdlDocument -notmatch '(?m)^openapi:\s*3\.0\.' -or $csdlDocument -notmatch '(?m)^ /Todos:\s*$' -or $csdlDocument -notmatch '(?m)^ schemas:\s*$') {
throw "Expected OpenAPI 3.0, /Todos paths and schemas in the Docker CSDL YAML output."
}
cd:
if: needs.ci.outputs.is_default_branch == 'true' && needs.ci.outputs.latest_version != ''
name: Continuous Deployment
needs: ci
runs-on: ubuntu-latest
steps:
# - name: Download and extract NuGet packages
# id: dl_packages_artifact
# uses: actions/download-artifact@v2
# with:
# name: NuGet packages
# path: NuGet/
# - name: Push NuGet packages to NuGet.org
# id: push_nuget_packages
# continue-on-error: true
# shell: pwsh
# run: |
# Get-ChildItem NuGet/*.nupkg | ForEach-Object {
# nuget push $PSItem `
# -ApiKey $env:NUGET_API_KEY `
# -Source https://api.nuget.org/v3/index.json
# }
# env:
# NUGET_API_KEY: ${{ secrets.NUGET_API_KEY }}
- name: Create and publish release
id: create_release
uses: softprops/action-gh-release@efb35369e0ad2afab669f228072c1b0d510eae64 # v3.0.3
with:
name: OpenAPI.Net.OData v${{ needs.ci.outputs.latest_version }}
tag_name: v${{ needs.ci.outputs.latest_version }}
# files: |
# NuGet/Microsoft.OpenApi.${{ needs.ci.outputs.latest_version }}.nupkg
# NuGet/Microsoft.OpenApi.Readers.${{ needs.ci.outputs.latest_version }}.nupkg
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# Built with ❤ by [Pipeline Foundation](https://pipeline.foundation)