-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathJenkinsfile
148 lines (142 loc) · 6.28 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
pipeline {
agent { label "slave" }
environment{
sonarqubeURL="http://sonarqube.lightnet-nonprod.com:9000"
branchName = sh(
script: "printf \$(git rev-parse --abbrev-ref HEAD | sed -e \"s|/|-|g\")",
returnStdout: true
)
GIT_COMMIT_SHORT = sh(
script: "printf \$(git rev-parse --short=8 ${GIT_COMMIT})",
returnStdout: true
)
newVersion="${env.BUILD_NUMBER}-${env.GIT_COMMIT_SHORT}"
dockerTag="${env.branchName}-${env.newVersion}"
dockerImage="${env.CONTAINER_IMAGE}:${env.dockerTag}"
appName="cen"
repoName="DRSv1"
githubUsername="velo-protocol"
CONTAINER_IMAGE="registry.gitlab.com/velo-labs/${appName}"
status_failure="{\"state\": \"failure\",\"context\": \"continuous-integration/jenkins\", \"description\": \"Jenkins\", \"target_url\": \"${BUILD_URL}\"}"
status_success="{\"state\": \"success\",\"context\": \"continuous-integration/jenkins\", \"description\": \"Jenkins\", \"target_url\": \"${BUILD_URL}\"}"
}
stages {
stage('Build Image Test') {
steps {
withCredentials([usernamePassword(credentialsId: '6a0ef684-a441-4262-937f-d9a7a0602b56', passwordVariable: 'gitlabPassword', usernameVariable: 'gitlabUsername')]) {
sh '''
echo "Build Image"
docker login -u ${gitlabUsername} -p ${gitlabPassword} registry.gitlab.com
docker build --pull --target development -t ${CONTAINER_IMAGE}:${dockerTag}-test -f node/Dockerfile .
'''
}
}
}
stage('Unit Test') {
steps {
sh '''
echo "Run unit test -> ${CONTAINER_IMAGE}:${dockerTag}-test"
mkdir -p $(pwd)/reports
docker run --rm -v $(pwd)/reports:/reports ${CONTAINER_IMAGE}:${dockerTag}-test sh -c "go test --tags='unit' ./... -v -coverprofile .coverage.txt | go-junit-report > /reports/coverage-tasks.xml"
docker run --rm -v $(pwd)/reports:/reports ${CONTAINER_IMAGE}:${dockerTag}-test sh -c "go test --tags='unit' ./... -v -coverprofile .coverage.txt; gocov convert .coverage.txt | gocov-xml > /reports/coverage.xml && cp .coverage.txt /reports/"
sudo chown -R jenkins:jenkins $(pwd)/reports
'''
}
}
stage('SonarQube Code Analysis') {
steps {
script {
echo "SonarQube Code Analysis"
withSonarQubeEnv('sonarqube') {
sh '''
sed -i s~#SONARQUBE_URL#~${sonarqubeURL}~g Makefile
sed -i s~#APP_VERSION#~${dockerTag}~g Makefile
make ci_sonarqube
'''
}
}
}
}
stage('SonarQube Quality Gate') {
steps {
sleep(10)
script {
echo "SonarQube Quality Gate"
timeout(time: 1, unit: 'MINUTES') {
def qg = waitForQualityGate()
if (qg.status != 'OK') {
error "Pipeline aborted due to quality gate failure: ${qg.status}"
}
}
}
}
}
stage('Build and Push to Registry') {
when {
anyOf {
branch 'develop';
branch 'release/*';
branch 'master'
}
}
steps {
withCredentials([usernamePassword(credentialsId: '6a0ef684-a441-4262-937f-d9a7a0602b56', passwordVariable: 'gitlabPassword', usernameVariable: 'gitlabUsername')]) {
sh '''
echo "Build and Push to Registry"
docker login -u ${gitlabUsername} -p ${gitlabPassword} registry.gitlab.com
docker build --pull -t ${CONTAINER_IMAGE}:${dockerTag} -f node/Dockerfile .
docker push ${CONTAINER_IMAGE}:${dockerTag}
docker tag ${CONTAINER_IMAGE}:${dockerTag} ${CONTAINER_IMAGE}:${branchName}
docker push ${CONTAINER_IMAGE}:${branchName}
'''
}
}
}
stage('Trigger to Deployment job') {
parallel {
stage ('Deploy to Develop Environment') {
when {
branch 'develop'
}
steps {
build job: 'DRSv1-deploy', parameters: [string(name: 'dockerVersion', value: env.dockerTag),string(name: 'environment', value: 'develop')]
}
}
stage ('Deploy to Staging Environment') {
when {
branch 'master'
}
steps {
build job: 'DRSv1-deploy', parameters: [string(name: 'dockerVersion', value: env.dockerTag),string(name: 'environment', value: 'staging')]
}
}
}
}
}
post {
failure {
withCredentials([string(credentialsId: 'velo-github-token', variable: 'githubToken')]) {
sh '''
curl \"https://api.github.com/repos/${githubUsername}/${repoName}/statuses/${GIT_COMMIT}?access_token=${githubToken}\" \
-H \"Content-Type: application/json\" \
-X POST \
-d "${status_failure}"
'''
}
}
success {
withCredentials([string(credentialsId: 'velo-github-token', variable: 'githubToken')]) {
sh '''
curl \"https://api.github.com/repos/${githubUsername}/${repoName}/statuses/${GIT_COMMIT}?access_token=${githubToken}\" \
-H \"Content-Type: application/json\" \
-X POST \
-d "${status_success}"
'''
}
}
always {
junit "reports/coverage-tasks.xml"
cobertura coberturaReportFile: "reports/coverage.xml"
}
}
}