-
-
Notifications
You must be signed in to change notification settings - Fork 0
201 lines (179 loc) · 6.81 KB
/
dotnet-action-sonar-container-workflow.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
name: 'build'
on:
push:
branches:
- main
paths-ignore:
- '*.md'
- '*.png'
tags:
- 'v?[0-9]+.[0-9]+.[0-9]+'
pull_request:
branches:
- main
paths-ignore:
- '*.md'
- '*.png'
types: [opened, synchronize, reopened] # https://docs.github.com/en/free-pro-team@latest/actions/reference/events-that-trigger-workflows#pull_request
workflow_dispatch:
inputs:
reason:
description: 'The reason for running the workflow.'
required: true
default: 'Manual run'
jobs:
build:
name: Build
runs-on: ubuntu-latest
outputs:
mode: ${{ steps.release.outputs.mode }}
steps:
- uses: actions/checkout@v4
with:
# Disabling shallow clone is recommended for improving relevancy.
fetch-depth: 0
# Already installed on build agent.
# - name: Setup .NET
# uses: actions/setup-dotnet@v4
# with:
# dotnet-version: 8.x
# Step needed to avoid issues with sonarscanner and preinstalled Java 11.
- name: Install Temurin OpenJDK
uses: actions/setup-java@v4
with:
java-version: 17
distribution: 'temurin'
architecture: x64
- name: Cache NuGet packages
uses: actions/cache@v4
with:
path: ~/.nuget/packages
key: ${{ runner.os }}-nuget-${{ hashFiles('**/*.csproj') }}
restore-keys: ${{ runner.os }}-nuget
- name: Cache SonarCloud packages
uses: actions/cache@v4
with:
path: ~/.sonar/cache
key: ${{ runner.os }}-sonar
restore-keys: ${{ runner.os }}-sonar
- name: Cache SonarCloud scanner
id: cache-sonar-scanner
uses: actions/cache@v4
with:
path: ~/.sonar/scanner
key: ${{ runner.os }}-sonar-scanner
restore-keys: ${{ runner.os }}-sonar-scanner
- name: Install SonarCloud scanner
if: steps.cache-sonar-scanner.outputs.cache-hit != 'true'
shell: bash
run: |
mkdir -p ~/.sonar/scanner
dotnet tool update dotnet-sonarscanner --tool-path ~/.sonar/scanner
- name: Restore dependencies
run: dotnet restore
- name: Build, Test, and Analyze
# env:
# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any.
# SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
run: |
# ~/.sonar/scanner/dotnet-sonarscanner begin /k:StevenJDH_maven-version-checker /o:stevenjdh /d:sonar.token=$SONAR_TOKEN /d:sonar.host.url="https://sonarcloud.io" \
# /d:sonar.cs.opencover.reportsPaths="**/TestResults/*/coverage.opencover.xml" /d:sonar.cs.vstest.reportsPaths="**/TestResults/*.trx" /d:sonar.language=cs \
# /d:sonar.scanner.scanAll=false
dotnet build --configuration Debug --no-restore
dotnet test --configuration Debug --no-build --verbosity normal --collect:"XPlat Code Coverage" --logger trx -- DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Format=opencover
# ~/.sonar/scanner/dotnet-sonarscanner end /d:sonar.token=$SONAR_TOKEN
- name: Set Release Mode
id: release
run: |
if [[ "${{ !contains(github.event_name, 'pull_request') }}" == true && ${{ startsWith(github.ref, 'refs/tags/') }} == true ]]; then
MODE=release
else
MODE=test
fi
echo "mode=$MODE" >> "$GITHUB_OUTPUT"
echo "### Release mode set to: ${MODE^^} :rocket:" >> "$GITHUB_STEP_SUMMARY"
- name: Upload Artifact
uses: actions/upload-artifact@v4
with:
name: repo-src
path: |
${{ github.workspace }}
!.git/
!.github/
!*/bin/
!*/obj/
!*/Properties/
!*.md
!*/TestResults/
retention-days: 1
deploy_action:
name: Deploy Action for ${{ needs.build.outputs.mode == 'test' && 'Testing' || 'Release' }}
runs-on: ubuntu-latest
permissions:
packages: write
contents: read
environment: ${{ needs.build.outputs.mode }}
needs: build
steps:
- name: Download Artifact
uses: actions/download-artifact@v4
with:
name: repo-src
- name: Generate Docker Metadata
id: meta
uses: docker/metadata-action@v5
with:
# List of container registry names to use for image tagging.
# Everything will be automatically set to lowercase.
images: |
ghcr.io/${{ github.repository }},enable=true
# Generates Docker tags based on the following events/attributes.
# latest tag set to true instead of {{is_default_branch}} because push is a conditional.
tags: |
type=ref,event=branch,enable=false
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}
type=sha
type=raw,value=latest,enable=true
- name: Login to GHCR
if: ${{ needs.build.outputs.mode == 'release' }}
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build${{ needs.build.outputs.mode == 'test' && ' and Tag ' || ', Tag, and Push ' }}Image
uses: docker/build-push-action@v5
with:
context: .
file: MavenVersionChecker.Action/Dockerfile
push: ${{ needs.build.outputs.mode == 'release' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
- name: Test Action Locally
if: ${{ needs.build.outputs.mode == 'test' }}
id: maven-artifacts
uses: ./MavenVersionChecker.Action/
with:
location: './MavenVersionChecker.Action.Tests/Sample/Multi/pom.xml'
- name: Display Action Outputs
if: ${{ needs.build.outputs.mode == 'test' }}
run: |
echo "Action Outputs:"
echo "- [has_updates]: ${{ steps.maven-artifacts.outputs.has_updates }}"
echo "- [number_of_updates]: ${{ steps.maven-artifacts.outputs.number_of_updates }}"
echo "- [update_json]: ${{ steps.maven-artifacts.outputs.update_json }}"
echo ""
echo "Deserialized Update JSON:"
echo "- [parents][0]: ${{ fromJSON(steps.maven-artifacts.outputs.update_json).parents[0] }}"
echo "- [dependencies][0]: ${{ fromJSON(steps.maven-artifacts.outputs.update_json).dependencies[0] }}"
echo "- [plugins][0]: ${{ fromJSON(steps.maven-artifacts.outputs.update_json).plugins[0] }}"
echo ""
echo "One approach to processing an array type field using bash:"
for element in ${{ join(fromJSON(steps.maven-artifacts.outputs.update_json).plugins, ' ') }}; do
IFS=":" read -r groupId artifactId version <<< "$element"
echo "groupId: $groupId"
echo "artifactId: $artifactId"
echo -e "version: $version\n"
done