-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
151 lines (141 loc) · 4.98 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
pipeline {
agent any
tools {nodejs "nodejs-16"}
environment {
REGISTRY = 'registry.ekbana.net'
HARBOR_NAMESPACE = "tcmv2"
HARBOR_CREDENTIAL = credentials('harbor-login-cred')
APP_NAME = "tcm-api"
DIR_NAME = getDirName(env.BRANCH_NAME)
scannerHome = tool 'SonarMSBuild';
}
stages {
stage('get_commit_msg') {
steps {
script {
notifyStarted()
passedBuilds = []
lastSuccessfulBuild(passedBuilds, currentBuild);
env.changeLog = getChangeLog(passedBuilds)
echo "changeLog \n${env.changeLog}"
}
}
}
stage("Checkout code") {
steps {
checkout scm
}
}
stage('Analysis & Deploy') {
parallel{
stage('SonarQube Analysis') {
steps {
withSonarQubeEnv(installationName: 'SonarQubePro') {
sh "dotnet ${scannerHome}/SonarScanner.MSBuild.dll begin /k:\"TCMSystem_TestCaseManagementV2_AX2PL0DKAug45V0lOzvv\""
sh "dotnet build TCM.sln"
sh "dotnet ${scannerHome}/SonarScanner.MSBuild.dll end"
}
}
}
stage('Build & Deploy'){
stages{
stage("Build image") {
steps {
echo 'test'
sh 'docker build -t $REGISTRY/$HARBOR_NAMESPACE/$APP_NAME:SNAPSHOT-$BRANCH_NAME-$BUILD_NUMBER .'
}
}
stage("Harbor login & Push image") {
steps {
sh '''echo $HARBOR_CREDENTIAL_PSW | docker login $REGISTRY -u 'harbor-user' --password-stdin'''
sh 'docker push $REGISTRY/$HARBOR_NAMESPACE/$APP_NAME:SNAPSHOT-$BRANCH_NAME-$BUILD_NUMBER'
sh 'docker rmi $REGISTRY/$HARBOR_NAMESPACE/$APP_NAME:SNAPSHOT-$BRANCH_NAME-$BUILD_NUMBER'
}
}
stage('Deploy to server') {
steps{
script {
withKubeConfig([credentialsId: 'tcm-k8s-login', serverUrl: 'https://36841342-00f9-4c7c-8ae4-0683ebc281f6.k8s.ondigitalocean.com']) {
//sh 'kubectl config set current-context do-sgp1-k8s-tcm-stag'
//sh 'kubectl config get-contexts'
sh 'kubectl apply -f $DIR_NAME/api/'
sh 'kubectl set image deployment/$BRANCH_NAME-tcm-api-deployment $BRANCH_NAME-tcm-api=registry.ekbana.net/tcmv2/tcm-api:SNAPSHOT-$BRANCH_NAME-$BUILD_NUMBER'
}
}
}
}
}
}
}
}
}
post{
success{
//script {
//if (env.BRANCH_NAME == 'dev' || env.BRANCH_NAME == 'QA' || env.BRANCH_NAME == 'UAT' )
notifySuccessful()
//}
}
failure{
notifyFailed()
}
}
}
def notifyStarted() {
mattermostSend (
color: "#2A42EE",
channel: 'tcm-jenkins',
endpoint: 'https://ekbana.letsperk.com/hooks/kfjzs4qxhjyizdam9f5feigpre',
message: "Build STARTED: ${env.JOB_NAME} #${env.BUILD_NUMBER} (<${env.BUILD_URL}|Link to build>)"
)
}
def notifySuccessful() {
mattermostSend (
color: "#00f514",
channel: 'tcm-jenkins',
endpoint: 'https://ekbana.letsperk.com/hooks/kfjzs4qxhjyizdam9f5feigpre',
message: "Build SUCCESS: ${env.JOB_NAME} #${env.BUILD_NUMBER} (<${env.BUILD_URL}|Link to build>):\n${changeLog}"
)
}
def notifyFailed() {
mattermostSend (
color: "#e00707",
channel: 'tcm-jenkins',
endpoint: 'https://ekbana.letsperk.com/hooks/kfjzs4qxhjyizdam9f5feigpre',
message: "Build FAILED: ${env.JOB_NAME} #${env.BUILD_NUMBER} (<${env.BUILD_URL}|Link to build>)"
)
}
def lastSuccessfulBuild(passedBuilds, build) {
if ((build != null) && (build.result != 'SUCCESS')) {
passedBuilds.add(build)
lastSuccessfulBuild(passedBuilds, build.getPreviousBuild())
}
}
@NonCPS
def getChangeLog(passedBuilds) {
def log = ""
for (int x = 0; x < passedBuilds.size(); x++) {
def currentBuild = passedBuilds[x];
def changeLogSets = currentBuild.changeSets
for (int i = 0; i < changeLogSets.size(); i++) {
def entries = changeLogSets[i].items
for (int j = 0; j < entries.length; j++) {
def entry = entries[j]
log += "* ${entry.msg} by ${entry.author} \n"
}
}
}
return log;
}
//Getting branch name for respective branches
def getDirName(branchName) {
if("dev".equals(branchName)) {
return "/var/lib/jenkins/workspace/tcm-k8s/dev";
} else if ("qa".equals(branchName)) {
return "/var/lib/jenkins/workspace/tcm-k8s/qa";
} else if ("uat".equals(branchName)) {
return "/var/lib/jenkins/workspace/tcm-k8s/uat";
} else {
return "/var/lib/jenkins/workspace/tcm-k8s/master";
}
}