-
Notifications
You must be signed in to change notification settings - Fork 6
/
.gitlab-ci.yml
216 lines (198 loc) · 8.46 KB
/
.gitlab-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
include:
- project: kount/third_party/tpa-ci-shared
file:
- base/sq-scan.yml
- core/rules.yml
- version.yml
ref: 2.6.1
stages:
- version
- build
- test
- sonarqube scan
- deploy
.dotnet:
image: mcr.microsoft.com/dotnet/sdk:5.0
build:
stage: build
extends: .dotnet
script:
- dotnet clean
- dotnet build
test:
stage: test
extends: .dotnet
script:
- dotnet test "KountRisTest"
test config:
stage: test
extends: .dotnet
script:
- dotnet test "KountRisConfigTest"
sonarqube scan:
extends:
- .sq-scan
- .dotnet
stage: sonarqube scan
variables:
SRC_INCLUSIONS: "**/*.cs"
SRC_EXCLUSIONS: "**/bin/**,**/obj/**,**/DS_Store/**,**/.git/**,**/*.vs,**/*_site,**/*articles,**/*.gitignore,**/*.key,**/*.yml"
SONAR_COVERAGE_REPORT_PATHS: "coverage/SonarQube.xml"
PROJECT_KEY: "third_party:$PROJECT_NAME"
before_script:
- |
install_dependencies () {
echo "install dotnet sonar scanner"
# install certs
mkdir /usr/local/share/ca-certificates/kount
chmod 755 /usr/local/share/ca-certificates/kount/
cd /usr/local/share/ca-certificates/kount
cp ${KOUNT_INTERMEDIATE_CRT} intermediate.crt
chmod 644 intermediate.crt
echo -e ${KOUNT_MASTER_CRT} > master.crt
awk 'BEGIN {c=0;} /BEGIN CERT/{c++} { print > "master." c ".pem"}' < master.crt
chmod 644 master.crt
cp ${CI_SERVER_TLS_CA_FILE} server.crt
awk 'BEGIN {c=0;} /BEGIN CERT/{c++} { print > "cert." c ".pem"}' < server.crt
chmod 644 *.pem
cd -
update-ca-certificates
# install JRE
apt-get update && apt-get install -y openjdk-11-jre jq
# install dotnet sonarscanner and reportgenerator
dotnet tool install --global dotnet-sonarscanner --version 5.5.3
dotnet tool install --global dotnet-reportgenerator-globaltool --version 5.1.4
PATH="${PATH}:/root/.dotnet/tools"
}
# Run the Sonarqube scan on code in the repo under test
- |
run_sonarqube_scan () {
echo "Running code scan..."
dotnet-sonarscanner begin \
/key:"$PROJECT_KEY" \
/name:"$PROJECT_NAME" \
/d:sonar.host.url="${SONAR_HOST}" \
/d:sonar.login="$SONAR_TOKEN" \
/d:sonar.coverageReportPaths="$SONAR_COVERAGE_REPORT_PATHS"
dotnet build
dotnet test "KountRisTest" --collect:"XPlat Code Coverage" --results-directory ./coverage
dotnet test "KountRisConfigTest" --collect:"XPlat Code Coverage" --results-directory ./coverage
reportgenerator "-reports:./coverage/*/coverage.*.xml" "-targetdir:coverage" "-reporttypes:SonarQube"
dotnet-sonarscanner end /d:sonar.login="$SONAR_TOKEN"
}
# Wait for sonar-scanner to get the quality-gate result from the scan.
# Time out with error after TIMEOUT_IN_SECONDS if no result yet.
- |
get_quality_gate_result () { echo "Getting quality-gate results...";
ELAPSED_TIME=0;
while [ $TIMEOUT_IN_SECONDS -ge $ELAPSED_TIME ]; do
QG_STATUS=$(curl -sku "${SONAR_TOKEN}:" -X GET "${SONAR_HOST}/api/qualitygates/project_status?projectKey=${PROJECT_KEY}" | jq '.projectStatus.status' | tr -d '"');
echo "QG_STATUS at ${ELAPSED_TIME} seconds = ${QG_STATUS}";
if [[ -z "$QG_STATUS" || "$QG_STATUS" = "null" || "$QG_STATUS" = "NONE" ]]; then
sleep 5 && ELAPSED_TIME=$(($ELAPSED_TIME+5));
echo "ELAPSED_TIME = ${ELAPSED_TIME}";
echo "TIMEOUT_IN_SECONDS = ${TIMEOUT_IN_SECONDS}";
echo "still waiting..."; else
break;
fi;
done;
if [ $TIMEOUT_IN_SECONDS -lt $ELAPSED_TIME ]; then
echo "TIMEOUT ERROR. Sonarqube scan incomplete after ${TIMEOUT_IN_SECONDS} seconds.";
exit 1;
fi; }
# Trim "new_" from a scan condition name if it's there. We want to better
# describe these condition as applying specifically to new code added by
# the current commit.
- trim_key_prefix () { echo "${KEY:$KEY_PREFIX_LENGTH:$KEY_LENGTH}"; }
# Rename the "sqale_rating" condition since it won't mean much to users
# unless they read Sonarqube docs. This condition is a maintainability rating.
- translate_sqale_rating_for_readability () { echo "maintainability_rating"; }
# Translate the generic condition keys to a more informative format
- |
describe_key () { KEY=$1;
KEY_LENGTH=${#KEY};
KEY_STARTS_WITH="${KEY:0:4}";
if [ "${KEY_STARTS_WITH}" = "new_" ]; then \
IS_NEW_CODE_CONDITION="true";
KEY_PREFIX_LENGTH=4; else \
IS_NEW_CODE_CONDITION="false";
KEY_PREFIX_LENGTH=0;
fi;
KEY=$(trim_key_prefix);
if [ "${KEY}" = "sqale_rating" ]; then \
KEY=$(translate_sqale_rating_for_readability);
fi;
if [ "${IS_NEW_CODE_CONDITION}" = "true" ]; then \
echo "${KEY} for this commit's new code"; else \
echo "${KEY} overall";
fi; }
# Translate the scan result for each condition to an unambiguous pass/fail binary
- |
evaluate_scan_result () { CONDITION_STATUS=$1;
if [ "${CONDITION_STATUS}" = "OK" ]; then \
echo "PASSED"; else \
echo "FAILED";
fi; }
# Extract and translate the scan result from REST-response JSON
- |
report_scan_condition_result () { CONDITION=$1;
KEY=$(echo $CONDITION | jq -r '.metricKey');
KEY_DESCRIPTION=$(describe_key $KEY);
STATUS=$(echo $CONDITION | jq -r '.status');
STATUS_DESCRIPTION=$(evaluate_scan_result $STATUS);
echo " * ${KEY_DESCRIPTION}: ${STATUS_DESCRIPTION}"; }
# Print a human-readable summary of the sonar-scanner result
- |
print_quality_gate_result () { echo "Printing quality-gate results...";
QG_RESULT_CONDITIONS=$(curl -sku "${SONAR_TOKEN}:" \
-X GET "${SONAR_HOST}/api/qualitygates/project_status?projectKey=${PROJECT_KEY}" | jq -c '.projectStatus.conditions[]');
printf "\n******************************************************************************************************";
printf "\n******************************************************************************************************\n";
echo "SONARQUBE QUALITY GATE ANALYSIS";
printf "\nOverall scan result = ${QG_STATUS}\n";
printf "\nIndividual conditions in quality gate analysis:\n";
for CONDITION in $QG_RESULT_CONDITIONS; do
report_scan_condition_result $CONDITION;
done;
printf "\n"
echo "To drill further into the scan results, go to"
echo "https://sonarqube.private.kount.com/dashboard?id=third_party%3A${CI_PROJECT_NAME}";
printf "\n******************************************************************************************************";
printf "\n******************************************************************************************************\n"; }
# Make the GitLab job fail if the Sonar scan failed
- |
fail_test_on_quality_gate_failure () { echo "Checking quality-gate results...";
if [ "$QG_STATUS" != "OK" ]; then exit 1; fi; }
script:
- install_dependencies
- run_sonarqube_scan
- get_quality_gate_result
- print_quality_gate_result
- fail_test_on_quality_gate_failure
nuget deploy:
stage: deploy
extends: .dotnet
only:
- tags
script:
- apt-get update -qq
# BUILD_VERSION is cut to 17 characters so SDK_VERSION will not exceed 32 characters in total
- SDK_VERSION=`echo $BUILD_VERSION | cut -c-17`
- echo ${SDK_VERSION}
- |
if [ ${SDK_VERSION} ]
then
echo "sed -i \"s/0.0.0/${SDK_VERSION}/g\" ${CI_PROJECT_DIR}/SDK/Kount/Ris/Config.cs"
sed -i "s/0.0.0/${SDK_VERSION}/g" ${CI_PROJECT_DIR}/SDK/Kount/Ris/Config.cs
echo "sed -i \"s/0.0.0/${SDK_VERSION}/g\" ${CI_PROJECT_DIR}/SDK/KountRisSdk.csproj"
sed -i "s/0.0.0/${SDK_VERSION}/g" ${CI_PROJECT_DIR}/SDK/KountRisSdk.csproj
echo "sed -i \"s/0.0.0/${SDK_VERSION}/g\" ${CI_PROJECT_DIR}/README.md"
sed -i "s/0.0.0/${SDK_VERSION}/g" ${CI_PROJECT_DIR}/README.md
cat ${CI_PROJECT_DIR}/SDK/Kount/Ris/Config.cs
cat ${CI_PROJECT_DIR}/SDK/KountRisSdk.csproj
cat ${CI_PROJECT_DIR}/README.md
fi
- dotnet build
- PKGPATH=$(find /builds/kount/third_party/kount-ris-dotnet-sdk/SDK/bin/Debug/*.nupkg)
- echo $PKGPATH
- dotnet nuget push $PKGPATH --source https://www.nuget.org/ -k $NUGET_API_KEY