Jenkinsfile
5.67 KB
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
pipeline {
agent any
//环境定义
environment{
//服务名称
SVN_FOLD = "fe-swzl-asset-temp"
//部署远程服务器
//192.168.4.4服务器
//SSH_PATH = "csbr4.4"
//192.168.5.4
SSH_PATH = "csbr5.2"
//139.9.190.186
//SSH_PATH = "csbr190.186"
//SVN_TYPE = "master"
//镜像版本号
image_tag = "1.0.0"
//docker私服ip
ip = "192.168.5.4:82"
//前端端口号
port = "8911"
//映射端口号
vport = "80"
}
//定时任务
//triggers { pollSCM('H 4/* 0 0 1-5') }
//triggers {
//每天凌晨2点自动构建
// cron('H 2 * * *')
//}
options {
// 设置保留的最大历史构建数为6
buildDiscarder(logRotator(numToKeepStr: '2'))
}
//全局定义工具
tools {
//工具名称必须在Jenkins 管理Jenkins → 全局工具配置中预配置。
nodejs 'node 16.20.2'
}
stages {
//清空
stage('Clean') {
steps {
sh 'rm -rf dist/*'
}
}
//构建
stage('Node Build') {
steps {
nodejs('node 16.20.2'){}
//配置私有npm仓库
sh 'npm config set registry http://192.168.5.4:8001/repository/csbr-npm/'
sh 'yarn config set registry http://192.168.5.4:8001/repository/csbr-npm/'
// 配置后可通过下面方式来验证是否成功
sh 'npm config get registry'
sh 'npm install -g pnpm'
sh 'pnpm config set registry http://registry.npm.taobao.org'
sh 'pnpm install'
sh 'pnpm build:product'
}
}
stage('Docker Build') {
steps {
//分分支构建
script{
if(env.BRANCH_NAME=='master-asset'){
//master-asset分支环境
echo 'start to deploy ${SVN_FOLD} on master-asset ...'
sh '''
#docker rmi -f $(docker images | grep "none" | awk '{print $3}')
CID=$(docker ps -a | grep "${SVN_FOLD}" | awk '{print $1}')
#IID=$(docker images | grep "${SVN_FOLD}" | awk '{print $3}')
IID=$(docker images | grep "none" | awk '{print $3}')
if [ -n "$IID" ]; then
echo "存在'${SVN_FOLD}'镜像,IID='$IID'"
cd "$WORKSPACE"/
##构建镜像到远程仓库
docker login "${ip}" -u admin -p E6w611g864wQ2
#docker tag "${SVN_FOLD}":"${image_tag}" "${ip}"/csbr/"${SVN_FOLD}":"${image_tag}"
docker build -t "${ip}"/csbr/"${SVN_FOLD}":"${image_tag}" .
docker push "${ip}"/csbr/"${SVN_FOLD}":"${image_tag}"
else
echo "不存在'${SVN_FOLD}'镜像,开始构建镜像"
##构建镜像到远程仓库
docker login "${ip}" -u admin -p E6w611g864wQ2
#docker tag "${SVN_FOLD}":"${image_tag}" "${ip}"/csbr/"${SVN_FOLD}":"${image_tag}"
docker build -t "${ip}"/csbr/"${SVN_FOLD}":"${image_tag}" .
docker push "${ip}"/csbr/"${SVN_FOLD}":"${image_tag}"
fi
'''
echo 'Depoly ${SVN_FOLD} success ...'
}
}
}
}
//测试(暂时不用)
//stage('Test') {
//steps {
//sh './gradlew check'
//sh 'mvn -f ${SVN_FOLD}/pom.xml test'
//sh 'mvn -f ${SVN_FOLD}/pom.xml test'
//}
//}
//部署
stage('Deploy') {
steps {
echo 'Deploying'
//分分支部署
script{
if(env.BRANCH_NAME=='master-asset'){
//master-asset分支环境
echo 'start to deploy ${SVN_FOLD} on master-asset ...'
//调用Publish Over SSH插件,上传docker-compose.yaml文件并且执行deploy脚本
sshPublisher(publishers: [sshPublisherDesc(configName: "csbr5.3", transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: """
#使用k8s构建
kubectl delete -f /mnt/k8s/production/fe-swzl-asset.yaml
kubectl apply -f /mnt/k8s/production/fe-swzl-asset.yaml
""", execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '/mnt/csbr/data', remoteDirectorySDF: false, removePrefix: '', sourceFiles: 'output/*.*')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
echo 'Depoly ${SVN_FOLD} success ...'
}
}
}
}
}
//归档
post {
always {
echo 'Archive artifacts'
archiveArtifacts artifacts: "dist/**", excludes: "dist"
}
}
}