-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
168 lines (164 loc) · 6.3 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
pipeline {
agent{
docker {
image env.DOCKER_FARM_IMAGE_NODEJS
label env.DOCKER_FARM_LABEL
args env.DOCKER_FARM_ARGS
}
}
environment{
DIR_PATH='/opt/unicloud/pco/sales'
ANSIBLE_PATH='/opt/unicloud/ansible'
}
options {
gitLabConnection(env.gitlabConnection)
gitlabBuilds(builds: ['checkout', 'clean', 'compile'])
timestamps()
}
post {
always { step([$class: 'WsCleanup']) }
success {
addGitLabMRComment comment: ':white_check_mark: Jenkins Build SUCCESS\n\n' +
"Results available at: [Jenkins [${env.JOB_BASE_NAME} ${env.BUILD_DISPLAY_NAME}]](${env.BUILD_URL})"
}
failure {
addGitLabMRComment comment: ':negative_squared_cross_mark: Jenkins Build Failure\n\n' +
"Results available at: [Jenkins [${env.JOB_BASE_NAME} ${env.BUILD_DISPLAY_NAME}]](${env.BUILD_URL})"
}
}
stages {
stage('checkout') {
post {
success { updateGitlabCommitStatus name: 'checkout', state: 'success' }
failure { updateGitlabCommitStatus name: 'checkout', state: 'failed' }
}
steps {
script{
checkoutDependOnEnv env
}
}
}
stage('clean') {
post {
success { updateGitlabCommitStatus name: 'clean', state: 'success' }
failure { updateGitlabCommitStatus name: 'clean', state: 'failed' }
}
steps {
sh """
git clean -fxd
"""
}
}
stage('download-module') {
post {
success { updateGitlabCommitStatus name: 'download-module', state: 'success' }
failure { updateGitlabCommitStatus name: 'download-module', state: 'failed' }
}
steps {
script{
def server = Artifactory.server(env.ARTIFACTORY_SERVER_ID)
def downloadSpec = """{
"files": [
{
"pattern": " mavens/h3cloud-tools/node_modules/verify-record-20201218/*",
"target": "./node_modules/",
"flat": "true",
"explode": "true"
}]
}"""
server.download(downloadSpec)
sh "chmod -R +x node_modules/.bin"
}
}
}
stage('build'){
post {
success { updateGitlabCommitStatus name: 'build', state: 'success' }
failure { updateGitlabCommitStatus name: 'build', state: 'failed' }
}
steps{
sh """
npm run build
"""
}
}
stage('package')
{
steps
{
sh """
envsubst < ansible.yaml.template > ansible.yaml
envsubst < install.sh.template > install.sh
cd dist/
tar -czvf ../${JOB_NAME}-${BUILD_VERSION}.tar.gz `ls`
cd ../
tar -czvf ${JOB_NAME}.tar.gz ansible.yaml install.sh ${JOB_NAME}-${BUILD_VERSION}.tar.gz
rm -rf ${JOB_NAME}-${BUILD_VERSION}.tar.gz
mv ${JOB_NAME}.tar.gz ${JOB_NAME}-${BUILD_VERSION}.tar.gz
"""
}
}
stage('publish') {
post {
success { updateGitlabCommitStatus name: 'publish', state: 'success' }
failure { updateGitlabCommitStatus name: 'publish', state: 'failed' }
}
steps {
script {
def uploadSpec = """{
"files": [
{
"pattern": "${JOB_NAME}-${BUILD_VERSION}.tar.gz",
"target": "${env.COMPONENTS_REPO}/unicloud/pco/${JOB_NAME}/${BUILD_VERSION}/"
}
]
}"""
def server = Artifactory.server(env.ARTIFACTORY_SERVER_ID)
def buildInfo = Artifactory.newBuildInfo()
server.upload(uploadSpec, buildInfo)
//buildInfo.env.capture = true
//buildInfo.env.collect()
//server.publishBuildInfo(buildInfo)
}
}
}
stage('sonar-check') {
//触发条件,正则匹配
when { expression { env.BUILD_VERSION ==~ '.*test.*'} }
post {
success {
updateGitlabCommitStatus name: 'sonar', state: 'success'
}
failure {
updateGitlabCommitStatus name: 'sonar', state: 'failed'
}
}
steps {
sh """
/home/sonar-scanner/bin/sonar-scanner
"""
}
}
stage('deploy')
{
post {
success { updateGitlabCommitStatus name: 'deploy', state: 'success' }
failure { updateGitlabCommitStatus name: 'deploy', state: 'failed' }
}
steps {
script {
if ( master == 'NULL' ) {
sh 'echo no master'
} else {
sh """
sshpass -p yunying123@ scp -oStrictHostKeyChecking=no ${JOB_NAME}-${BUILD_VERSION}.tar.gz root@${master}:/root/${JOB_NAME}-${BUILD_VERSION}.tar.gz
sshpass -p yunying123@ ssh -oStrictHostKeyChecking=no root@${master} "cd /root && rm -rf ${JOB_NAME}/${BUILD_VERSION} && mkdir -p ${JOB_NAME}/${BUILD_VERSION} "
sshpass -p yunying123@ ssh -oStrictHostKeyChecking=no root@${master} "tar -zxvf ${JOB_NAME}-${BUILD_VERSION}.tar.gz -C ${JOB_NAME}/${BUILD_VERSION}"
sshpass -p yunying123@ ssh -oStrictHostKeyChecking=no root@${master} "cd ${JOB_NAME}/${BUILD_VERSION} && bash install.sh"
"""
}
}
}
}
}
}