Newer
Older
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
297
298
299
300
301
302
303
304
305
306
307
308
# =========================================================================================
# Copyright (C) 2021 Orange
#
# This program is free software; you can redistribute it and/or modify it under the terms
# of the GNU Lesser General Public License as published by the Free Software Foundation;
# either version 3 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License along with this
# program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
# Floor, Boston, MA 02110-1301, USA.
# =========================================================================================
variables:
# Default ng workspace
NG_WORKSPACE_DIR: .
# Default Docker image for ANGULAR CLI (can be overriden)
NG_CLI_IMAGE: trion/ng-cli-karma:latest
# JUnit test report
NG_JUNIT_TEST_REPORT_PATH: "reports/junit_test_report.xml"
NG_E2E_REPORT_PATH: "reports/e2e"
# Angular lint
NG_LINT_ARGS: "lint"
# Angular test
NG_TEST_ARGS: >-
test
--code-coverage
--reporters progress,junit
NG_E2E_ARGS: >-
e2e
# Angular Build
NG_BUILD_ARGS: "build --prod"
# default production ref name (pattern)
PROD_REF: '/^master$/'
# default integration ref name (pattern)
INTEG_REF: '/^develop$/'
# ==================================================
# Variables for publication
# ==================================================
# NG_PUBLISH_ENABLED
# List of projects to publish, use space (" ") for separation
# ex: NG_PUBLISH_PROJECTS: "Project1 Project2 myLib"
# By default, NG_PUBLISH_PROJECTS is the value of angular.json, "defaultProject" property
# Set some args of `npm publish` command
# ex: NG_PUBLISH_ARGS: "--dry-run"
NG_PUBLISH_ARGS: '--verbose'
# ==================================================
# Stages definition
# ==================================================
stages:
- build
- test
- publish
###############################################################################################
# Script definition #
###############################################################################################
.ng-cli-scripts: &ng-cli-scripts |
# BEGSCRIPT
set -e
function log_debug() {
if [[ -n "$TRACE" ]]
then
echo -e "[\\e[1;36mTRACE\\e[0m] $*"
fi
}
function log_info() {
echo -e "[\\e[1;94mINFO\\e[0m] $*"
}
function log_warn() {
echo -e "[\\e[1;93mWARN\\e[0m] $*"
}
function log_error() {
echo -e "[\\e[1;91mERROR\\e[0m] $*"
}
function assert_defined() {
if [[ -z "$1" ]]
then
log_error "$2"
exit 1
fi
}
function sonar_lint_report() {
if [[ -n "$SONAR_URL" ]]
then
mkdir -p reports
# generate ts lint report in json for SONARqube
# shellcheck disable=SC2086
ng $NG_LINT_ARGS --format=json --force > reports/tslint-report.json
fi
}
function install_ca_certs() {
certs=$1
if [[ -z "$certs" ]]
then
return
fi
# import in system
if echo "$certs" >> /etc/ssl/certs/ca-certificates.crt
then
log_info "CA certificates imported in \\e[33;1m/etc/ssl/certs/ca-certificates.crt\\e[0m"
fi
if echo "$certs" >> /etc/ssl/cert.pem
then
log_info "CA certificates imported in \\e[33;1m/etc/ssl/cert.pem\\e[0m"
fi
# import in Java keystore (if keytool command found)
if command -v keytool > /dev/null
then
# shellcheck disable=SC2046
javahome=${JAVA_HOME:-$(dirname $(readlink -f $(command -v java)))/..}
# shellcheck disable=SC2086
keystore=${JAVA_KEYSTORE_PATH:-$(ls -1 $javahome/jre/lib/security/cacerts 2>/dev/null || ls -1 $javahome/lib/security/cacerts 2>/dev/null || echo "")}
if [[ -f "$keystore" ]]
then
storepass=${JAVA_KEYSTORE_PASSWORD:-changeit}
nb_certs=$(echo "$certs" | grep -c 'END CERTIFICATE')
log_info "importing $nb_certs certificates in Java keystore \\e[33;1m$keystore\\e[0m..."
for idx in $(seq 0 $((nb_certs - 1)))
do
# TODO: use keytool option -trustcacerts ?
if echo "$certs" | awk "n==$idx { print }; /END CERTIFICATE/ { n++ }" | keytool -noprompt -import -alias "imported CA Cert $idx" -keystore "$keystore" -storepass "$storepass"
then
log_info "... CA certificate [$idx] successfully imported"
else
log_warn "... Failed importing CA certificate [$idx]: abort"
return
fi
done
else
log_warn "Java keystore \\e[33;1m$keystore\\e[0m not found: could not import CA certificates"
fi
fi
}
function get_latest_template_version() {
tag_json=$(wget -T 5 -q -O - "$CI_API_V4_URL/projects/Orange-OpenSource%2Ftbc%2F$1/repository/tags?per_page=1" || echo "")
echo "$tag_json" | sed -rn 's/^.*"name":"([^"]*)".*$/\1/p'
}
function check_for_update() {
template="$1"
actual="$2"
latest=$(get_latest_template_version "$template")
if [[ -n "$latest" ]] && [[ "$latest" != "$actual" ]]
then
log_warn "\\e[1;93m=======================================================================================================\\e[0m"
log_warn "\\e[93mThe template \\e[32m$template\\e[93m:\\e[33m$actual\\e[93m you're using is not up-to-date: consider upgrading to version \\e[32m$latest\\e[0m"
log_warn "\\e[93m(set \$TEMPLATE_CHECK_UPDATE_DISABLED to disable this message)\\e[0m"
log_warn "\\e[1;93m=======================================================================================================\\e[0m"
fi
}
function read_default_project() {
# current directory should be ${NG_WORKSPACE_DIR}
angular_json=./angular.json
default_project=$(node -pe "require('${angular_json}').defaultProject")
default_project_type=$(node -pe "require('${angular_json}').projects['${default_project}'].projectType")
}
function run_ng_build() {
read_default_project
case ${default_project_type} in
library)
# shellcheck disable=SC2086
ng $NG_BUILD_ARGS
;;
*)
# shellcheck disable=SC2086
ng $NG_BUILD_ARGS --no-progress
;;
esac
}
function run_ng_build_libs() {
# current directory should be ${NG_WORKSPACE_DIR}
angular_json=./angular.json
libs=$(node -pe "Object.entries(require('${angular_json}').projects).filter(entry => entry[1].projectType === 'library').map(entry => entry[0]).join(' ')")
log_debug "parsed libs: ${libs}"
if [ -n "${libs}" ]; then
log_info "libs detected: ${libs}"
for lib in ${libs}; do
log_debug "building lib ${lib}"
ng build "${lib}"
done
fi
}
function configure_gitlab_instance_level_npm_registry_auth() {
npm config set "//${CI_SERVER_HOST}/api/v4/packages/npm/:_authToken" "${CI_JOB_TOKEN}"
}
function compute_gitlab_registry_url() {
gitlab_registry_url="https://${CI_SERVER_HOST}/api/v4/projects/${CI_PROJECT_ID}/packages/npm"
}
function configure_npm_publish_registry_auth() {
publish_registry=${NPM_PUBLISH_REGISTRY}
if [[ "${publish_registry}" ]]; then
log_info "configured publish registry: ${publish_registry}"
if [[ "$NPM_PUBLISH_TOKEN" ]]; then
shopt -s extglob
npm_publish_registry_host_and_path=${publish_registry/#http?(s):/}
shopt -u extglob
npm config set "${npm_publish_registry_host_and_path}:_authToken" "${NPM_PUBLISH_TOKEN}"
fi
else
compute_gitlab_registry_url
log_info "No defined publication registry, falling back to GitLab packages ${gitlab_registry_url}"
publish_registry=${gitlab_registry_url}
log_info "be sure to have your projects name starting with @${CI_PROJECT_ROOT_NAMESPACE}/"
npm config set "@${CI_PROJECT_ROOT_NAMESPACE}:registry" "https://${CI_SERVER_HOST}/api/v4/packages/npm/"
npm config set "//${CI_SERVER_HOST}/api/v4/projects/${CI_PROJECT_ID}/packages/npm/:_authToken" "${CI_JOB_TOKEN}"
fi
}
function npm_publish() {
projects_to_publish=${NG_PUBLISH_PROJECTS}
if [ -z "${projects_to_publish}" ]; then
read_default_project
log_info "No projects specified, falling back to default project: \\e[33;1m${default_project}\\e[0m."
projects_to_publish=${default_project}
fi
log_info "Publishing the following projects: ${projects_to_publish}..."
# current directory should be ${NG_WORKSPACE_DIR}
angular_json=./angular.json
package_json=./package.json
# first we check all specified projects exist
for project in ${projects_to_publish}; do
if ! projectRoot=$(node -pe "require('${angular_json}').projects['${project}'].root"); then
log_error "unknown project: ${project}";
exit 1;
fi
done
for project in ${projects_to_publish}; do
projectTypeSelector="projects['${project}'].projectType"
projectType=$(node -pe "require('${angular_json}').${projectTypeSelector}")
case ${projectType} in
library)
# output dir for library is defined in ng-package.json
projectRoot=$(node -pe "require('${angular_json}').projects['${project}'].root")
projectRelativeOutputDir=$(node -pe "require('./${projectRoot}/ng-package.json').dest")
projectOutputDir=${projectRoot}/${projectRelativeOutputDir}
;;
*)
# other output dir is defined in angular.json
projectOutputDir=$(node -pe "require('${angular_json}').projects['${project}'].architect.build.options.outputPath")
log_warn "It is not recommended to publish a project with '${projectType}' type as npm package."
log_warn "Please consider using another packaging solution as this support might be limited."
# WORKAROUND:
# as application type projects don't generate a package.json which is required for publishing
# we generate one with the global package.json version
if [ ! -f "${projectOutputDir}/package.json" ]; then
global_version=$(node -pe "require('${package_json}').version")
cat > "${projectOutputDir}"/package.json <<- EOF
{
"name": "${project}",
"version": "${global_version}"
}
EOF
fi
;;
esac
projectOutputDir=$(realpath "${projectOutputDir}")
if [ ! -d "${projectOutputDir}" ]; then
log_error "'${project}' project output folder '${projectOutputDir}' not found"
log_error "please check that module '${project}' has been built"
exit 1
fi
log_info "Publishing project '${project}' from ${projectOutputDir}"
npm publish "${projectOutputDir}" "${NG_PUBLISH_ARGS}"
done
}
if [[ -z "$TEMPLATE_CHECK_UPDATE_DISABLED" ]]; then check_for_update angular "1.0.1"; fi
# ENDSCRIPT
###############################################################################################
# Generic job definition #
###############################################################################################
.ng-cli-base:
image: $NG_CLI_IMAGE
services:
- name: "$CI_REGISTRY/orange-opensource/tbc/tools/tracking:master"
command: ["--service", "angular", "1.0.1"]
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# cache configuration
cache:
key: "$CI_COMMIT_REF_SLUG-angular"
paths:
- ${NG_WORKSPACE_DIR}/.npm/
before_script:
- *ng-cli-scripts
- cd ${NG_WORKSPACE_DIR}
- install_ca_certs "${CUSTOM_CA_CERTS:-$DEFAULT_CA_CERTS}"
# NPM_CONFIG_REGISTRY is not supported by old npm versions: force with cli
- if [[ "$NPM_CONFIG_REGISTRY" ]]; then npm config set registry $NPM_CONFIG_REGISTRY; fi
- configure_gitlab_instance_level_npm_registry_auth
- npm ci --cache .npm --prefer-offline
###############################################################################################
# check stage: #
# - ng-lint #
###############################################################################################
ng-lint:
extends: .ng-cli-base
stage: build
script:
# generate lint report for sonar
- sonar_lint_report
# display ng lint result for job console
- ng $NG_LINT_ARGS
artifacts:
when: always # save artifact even if test failed
name: "$CI_JOB_NAME artifacts from $CI_PROJECT_NAME on $CI_COMMIT_REF_SLUG"
paths:
- $NG_WORKSPACE_DIR/reports
expire_in: 1 day
rules:
# exclude merge requests
- if: $CI_MERGE_REQUEST_ID
when: never
# on production or integration branches: auto
- if: '$CI_COMMIT_REF_NAME =~ $PROD_REF || $CI_COMMIT_REF_NAME =~ $INTEG_REF'
# else (development branches): auto & non-blocking
- allow_failure: true
###############################################################################################
# build stage: #
# - ng-build #
###############################################################################################
ng-build:
extends: .ng-cli-base
stage: build
script:
# build libs so they are available for other projects tests
- run_ng_build_libs
# launch unit test and code coverage
- ng $NG_TEST_ARGS --watch=false --no-progress
# build production artifact
- run_ng_build
coverage: '/^Statements\s*:\s*([^%]+)/'
artifacts:
reports:
junit: $NG_WORKSPACE_DIR/$NG_JUNIT_TEST_REPORT_PATH
when: always # save artifact even if test failed
name: "$CI_JOB_NAME artifacts from $CI_PROJECT_NAME on $CI_COMMIT_REF_SLUG"
paths:
- $NG_WORKSPACE_DIR/coverage
- $NG_WORKSPACE_DIR/dist
- $NG_WORKSPACE_DIR/reports
expire_in: 1 day
rules:
# exclude merge requests
- if: '$CI_MERGE_REQUEST_ID == null'
###############################################################################################
# test stage: #
# - ng-e2e #
###############################################################################################
ng-e2e:
extends: .ng-cli-base
stage: test
script:
- ng $NG_E2E_ARGS
artifacts:
reports:
junit: $NG_WORKSPACE_DIR/$NG_E2E_REPORT_PATH/junit*.xml
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
when: always # save artifact even if test failed
name: "$CI_JOB_NAME artifacts from $CI_PROJECT_NAME on $CI_COMMIT_REF_SLUG"
paths:
- $NG_WORKSPACE_DIR/$NG_E2E_REPORT_PATH
expire_in: 1 day
rules:
# exclude merge requests
- if: $CI_MERGE_REQUEST_ID
when: never
# only run if feature is enabled
- if: '$NG_E2E_ENABLED'
###############################################################################################
# publish stage: #
# - npm-publish #
###############################################################################################
npm-publish:
extends: .ng-cli-base
stage: publish
before_script:
- *ng-cli-scripts
- cd ${NG_WORKSPACE_DIR}
- install_ca_certs "${CUSTOM_CA_CERTS:-$DEFAULT_CA_CERTS}"
- configure_gitlab_instance_level_npm_registry_auth
- configure_npm_publish_registry_auth
- npm ci --cache .npm --prefer-offline
script:
- npm_publish
rules:
# exclude merge requests
- if: $CI_MERGE_REQUEST_ID
when: never
# on production branche: manual
- if: '$NG_PUBLISH_ENABLED && $CI_COMMIT_REF_NAME =~ $PROD_REF'
when: manual