-
Notifications
You must be signed in to change notification settings - Fork 2
/
Jenkinsfile
152 lines (134 loc) · 5.76 KB
/
Jenkinsfile
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
pipeline {
agent any
tools {nodejs "NodeJS 16.13"}
options {
ansiColor('xterm')
}
environment {
DEMO_SERVER = '147.172.178.30'
DEMO_SERVER_PORT = '3000'
API_FILE = 'api-json'
API_URL = "http://${env.DEMO_SERVER}:${env.DEMO_SERVER_PORT}/${env.API_FILE}"
}
stages {
stage('Git') {
steps {
cleanWs()
git 'https://github.com/Student-Management-System/StudentMgmt-Backend.git'
}
}
stage('Install Dependencies') {
steps {
sh 'npm install'
}
}
stage('Test') {
environment {
POSTGRES_DB = 'StudentMgmtDb'
POSTGRES_USER = 'postgres'
POSTGRES_PASSWORD = 'admin'
PORT = '5432'
}
steps {
script {
// Sidecar Pattern: https://www.jenkins.io/doc/book/pipeline/docker/#running-sidecar-containers
docker.image('postgres:14.1-alpine').withRun("-e POSTGRES_USER=${env.POSTGRES_USER} -e POSTGRES_PASSWORD=${env.POSTGRES_PASSWORD} -e POSTGRES_DB=${env.POSTGRES_DB} -p ${env.PORT}:${env.PORT}") { c ->
sh 'npm run test:jenkins'
}
}
step([
$class: 'CloverPublisher',
cloverReportDir: 'output/test/coverage/',
cloverReportFileName: 'clover.xml',
healthyTarget: [methodCoverage: 70, conditionalCoverage: 80, statementCoverage: 80], // optional, default is: method=70, conditional=80, statement=80
unhealthyTarget: [methodCoverage: 50, conditionalCoverage: 50, statementCoverage: 50], // optional, default is none
failingTarget: [methodCoverage: 0, conditionalCoverage: 0, statementCoverage: 0] // optional, default is none
])
}
post {
always {
junit 'output/**/junit*.xml'
}
}
}
stage('Build') {
steps {
sh 'npm run build'
sh 'rm -f Backend.tar.gz'
sh 'tar czf Backend.tar.gz dist src test config package.json package-lock.json ormconfig.ts tsconfig.json'
}
}
stage('Build Docker') {
steps {
// Use build Dockerfile instead of Test-DB Dockerfile to build image
sh 'cp -f docker/Dockerfile Dockerfile'
script {
// Based on:
// - https://e.printstacktrace.blog/jenkins-pipeline-environment-variables-the-definitive-guide/
// - https://stackoverflow.com/a/16817748
// - https://stackoverflow.com/a/51991389
env.API_VERSION = sh(returnStdout: true, script: 'grep -Po \'(?<=export const VERSION = ")[^";]+\' src/version.ts').trim()
echo "API: ${env.API_VERSION}"
dockerImage = docker.build 'e-learning-by-sse/qualityplus-student-management-service'
docker.withRegistry('https://ghcr.io', 'github-ssejenkins') {
dockerImage.push("${env.API_VERSION}")
dockerImage.push('latest')
}
}
}
}
// Based on: https://medium.com/@mosheezderman/c51581cc783c
stage('Deploy') {
steps {
sshagent(credentials: ['Stu-Mgmt_Demo-System']) {
sh """
# [ -d ~/.ssh ] || mkdir ~/.ssh && chmod 0700 ~/.ssh
# ssh-keyscan -t rsa,dsa example.com >> ~/.ssh/known_hosts
ssh -i ~/.ssh/id_rsa_student_mgmt_backend elscha@${env.DEMO_SERVER} <<EOF
cd /staging/qualityplus-student-management-system
./recreate.sh
exit
EOF"""
}
//findText(textFinders: [textFinder(regexp: '(- error TS\\*)|(Cannot find module.*or its corresponding type declarations\\.)', alsoCheckConsoleOutput: true, buildResult: 'FAILURE')])
}
}
stage('Lint') {
steps {
sh 'npm run lint:ci'
}
}
stage('Publish Results') {
steps {
archiveArtifacts artifacts: '*.tar.gz'
sleep(time:40, unit:"SECONDS")
sh "wget ${env.API_URL}"
archiveArtifacts artifacts: "${env.API_FILE}"
}
}
stage("Trigger Downstream Projects") {
steps {
build job: 'Teaching_StuMgmtDocker', wait: false
build job: 'Teaching_StudentMgmt-Backend-API-Gen', wait: false
}
}
stage('Trigger API Client') {
// Execute this step only if Version number was changed
// Based on: https://stackoverflow.com/a/57823724
when { changeset "src/version.ts"}
steps {
build job: 'Teaching_StudentMgmt-API-Client', parameters: [string(name: 'API', value:'STU-MGMT')], wait: false
}
}
}
post {
always {
// Send e-mails if build becomes unstable/fails or returns stable
// Based on: https://stackoverflow.com/a/39178479
load "$JENKINS_HOME/.envvars/emails.groovy"
step([$class: 'Mailer', recipients: "${env.elsharkawy}, ${env.klingebiel}", notifyEveryUnstableBuild: true, sendToIndividuals: false])
// Report static analyses
recordIssues enabledForFailure: false, tool: checkStyle(pattern: 'output/eslint/eslint.xml')
}
}
}