-
-
Notifications
You must be signed in to change notification settings - Fork 0
255 lines (219 loc) · 8.41 KB
/
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
name: Build
on:
push:
branches:
- main
tags:
- '*'
paths-ignore:
- .devcontainer/**
- .vscode/**
- docs/**
- README.md
- LICENSE
pull_request:
branches:
- main
paths-ignore:
- .devcontainer/**
- .vscode/**
- docs/**
- README.md
- LICENSE
workflow_dispatch:
env:
AZURE_SUBSCRIPTION_ID: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
AZURE_RESOURCE_GROUP: ${{ secrets.AZURE_RESOURCE_GROUP }}
jobs:
Build:
runs-on: ubuntu-latest
env:
DOTNET_SKIP_FIRST_TIME_EXPERIENCE: 1
IMAGE_NAME: 'azure-resources-cleaner'
DOCKER_BUILDKIT: 1 # Enable Docker BuildKit
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0 # Required for GitVersion
- name: Install GitVersion
uses: gittools/actions/gitversion/setup@v0
with:
versionSpec: '5.x'
- name: Determine Version
uses: gittools/actions/gitversion/execute@v0
id: gitversion
with:
useConfigFile: true
- name: Setup .NET SDK
uses: actions/setup-dotnet@v4
with:
dotnet-version: 8.x
- name: Test
run: dotnet test -c Release --collect "Code coverage"
- name: Publish
run: >
dotnet publish
${{ github.workspace }}/Tingle.AzureCleaner/Tingle.AzureCleaner.csproj
--configuration Release
--output ${{ github.workspace }}/drop/Tingle.AzureCleaner
-p:PackageVersion=${{ steps.gitversion.outputs.nuGetVersion }}
-p:VersionPrefix=${{ steps.gitversion.outputs.nuGetVersion }}
- name: Pack
run: >
dotnet pack
--no-build
--configuration Release
--output ${{ github.workspace }}/drop
-p:PackageVersion=${{ steps.gitversion.outputs.nuGetVersion }}
-p:VersionPrefix=${{ steps.gitversion.outputs.nuGetVersion }}
- name: Replace tokens
uses: cschleiden/replace-tokens@v1
with:
files: '["${{ github.workspace }}/main.bicep"]'
- name: Build bicep file
uses: azure/cli@v2
with:
inlineScript: |
cp ${{ github.workspace }}/main.bicep ${{ github.workspace }}/drop/main.bicep && \
az bicep build --file main.bicep --outfile ${{ github.workspace }}/drop/main.json
- name: Upload Artifact (drop)
uses: actions/upload-artifact@v4
with:
path: ${{ github.workspace }}/drop/*
name: drop
retention-days: 1
- name: Create deploy folder
run: |
mkdir -p deploy
cp ${{ github.workspace }}/main.bicep ${{ github.workspace }}/deploy/main.bicep
cp ${{ github.workspace }}/main.parameters.json ${{ github.workspace }}/deploy/main.parameters.json
- name: Replace tokens in deploy folder
uses: cschleiden/replace-tokens@v1
with:
files: '["${{ github.workspace }}/deploy/main.parameters.json"]'
env:
DOCKER_IMAGE_TAG: ${{ steps.gitversion.outputs.shortSha }}
NOTIFICATION_PASSWORD: ${{ secrets.NOTIFICATION_PASSWORD }}
CLEANER_PROJECT_TOKEN: ${{ secrets.CLEANER_PROJECT_TOKEN }}
- name: Upload Artifact (drop)
uses: actions/upload-artifact@v4
with:
path: deploy
name: deploy
retention-days: 1
- name: Pull Docker base image & warm Docker cache
run: docker pull "ghcr.io/${{ github.repository_owner }}/${{ env.IMAGE_NAME }}:latest"
continue-on-error: true
- name: Build image
run: |
docker build \
-f Tingle.AzureCleaner/Dockerfile \
--label com.github.image.run.id=${{ github.run_id }} \
--label com.github.image.run.number=${{ github.run_number }} \
--label com.github.image.job.id=${{ github.job }} \
--label com.github.image.source.sha=${{ github.sha }} \
--label com.github.image.source.branch=${{ github.ref }} \
-t "ghcr.io/${{ github.repository_owner }}/${{ env.IMAGE_NAME }}:latest" \
-t "ghcr.io/${{ github.repository_owner }}/${{ env.IMAGE_NAME }}:${{ steps.gitversion.outputs.shortSha }}" \
-t "ghcr.io/${{ github.repository_owner }}/${{ env.IMAGE_NAME }}:${{ steps.gitversion.outputs.nuGetVersionV2 }}" \
-t "ghcr.io/${{ github.repository_owner }}/${{ env.IMAGE_NAME }}:${{ steps.gitversion.outputs.major}}.${{ steps.gitversion.outputs.minor }}" \
-t "ghcr.io/${{ github.repository_owner }}/${{ env.IMAGE_NAME }}:${{ steps.gitversion.outputs.major }}" \
--cache-from ghcr.io/${{ github.repository_owner }}/${{ env.IMAGE_NAME }}:latest \
--build-arg BUILDKIT_INLINE_CACHE=1 \
.
- name: Log into registry
if: ${{ (github.ref == 'refs/heads/main') || (!startsWith(github.ref, 'refs/pull')) || startsWith(github.ref, 'refs/tags') }}
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login https://ghcr.io -u ${{ github.actor }} --password-stdin
- name: Push image (latest, ShortSha)
if: ${{ (github.ref == 'refs/heads/main') || startsWith(github.ref, 'refs/tags') }}
run: |
docker push "ghcr.io/${{ github.repository_owner }}/${{ env.IMAGE_NAME }}:latest"
docker push "ghcr.io/${{ github.repository_owner }}/${{ env.IMAGE_NAME }}:${{ steps.gitversion.outputs.shortSha}}"
- name: Push image (NuGetVersionV2)
if: ${{ !startsWith(github.ref, 'refs/pull') }}
run: docker push "ghcr.io/${{ github.repository_owner }}/${{ env.IMAGE_NAME }}:${{ steps.gitversion.outputs.nuGetVersionV2 }}"
- name: Push image (major, minor)
if: startsWith(github.ref, 'refs/tags')
run: |
docker push "ghcr.io/${{ github.repository_owner }}/${{ env.IMAGE_NAME }}:${{ steps.gitversion.outputs.major }}.${{ steps.gitversion.outputs.minor }}"
docker push "ghcr.io/${{ github.repository_owner }}/${{ env.IMAGE_NAME }}:${{ steps.gitversion.outputs.major }}"
- name: Upload Release
if: startsWith(github.ref, 'refs/tags/')
uses: ncipollo/release-action@v1
with:
artifacts: >
${{ github.workspace }}/drop/main.bicep,
${{ github.workspace }}/drop/main.json
token: ${{ secrets.GITHUB_TOKEN }}
draft: true
allowUpdates: true
Deploy:
runs-on: ubuntu-latest
needs: Build
if: ${{ github.actor != 'dependabot[bot]' && ((github.ref == 'refs/heads/main') || startsWith(github.ref, 'refs/tags')) }}
concurrency:
group: ${{ github.workflow }}
steps:
- name: Download Artifact
uses: actions/download-artifact@v4
with:
name: deploy
path: ${{ github.workspace }}/deploy
- name: Azure Login
uses: azure/login@v2
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
- name: Deploy
uses: azure/arm-deploy@v2
with:
subscriptionId: ${{ env.AZURE_SUBSCRIPTION_ID }}
resourceGroupName: ${{ env.AZURE_RESOURCE_GROUP }}
template: '${{ github.workspace }}/deploy/main.bicep'
parameters: '${{ github.workspace }}/deploy/main.parameters.json'
scope: 'resourcegroup'
GitHubPackages:
runs-on: ubuntu-latest
name: Publish (GitHub Packages)
needs: Build
if: ${{ github.actor != 'dependabot[bot]' && ((github.ref == 'refs/heads/main') || startsWith(github.ref, 'refs/tags')) }}
steps:
- uses: actions/download-artifact@v4
with:
name: drop
path: ${{ github.workspace }}/drop
- name: Setup .NET SDK
uses: actions/setup-dotnet@v4
with:
dotnet-version: '8.x'
source-url: https://nuget.pkg.github.com/${{ github.repository_owner }}/index.json
env:
NUGET_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Publish to nuget.pkg.github.com
run: >
dotnet nuget push "${{ github.workspace }}/drop/*"
-k ${{ secrets.GITHUB_TOKEN }}
--skip-duplicate
NugetOrg:
runs-on: ubuntu-latest
name: Publish (Nuget.org)
needs: Build
if: ${{ github.actor != 'dependabot[bot]' && startsWith(github.ref, 'refs/tags') }}
steps:
- uses: actions/download-artifact@v4
with:
name: drop
path: ${{ github.workspace }}/drop
- name: Setup .NET SDK
uses: actions/setup-dotnet@v4
with:
dotnet-version: 8.x
source-url: https://api.nuget.org/v3/index.json
env:
NUGET_AUTH_TOKEN: ${{ secrets.NUGET_API_KEY }}
- name: Publish to NuGet.org
if: startsWith(github.ref, 'refs/tags/')
run: >
dotnet nuget push "${{ github.workspace }}/drop/*"
-k ${{ secrets.NUGET_API_KEY }}
--skip-duplicate