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
| pipeline {
agent any
parameters {
choice(name: 'MODULE', choices: ['all', 'auth', 'gateway', 'system'], description: '请选择部署模块!')
choice(name: 'PUBLISH_SERVER', choices: ['127.0.0.1'], description: '目标服务器')
choice(name: 'ACTIVE', choices:['prod', 'test', 'dev'], description: 'spring.profiles.active')
}
environment {
// SVN仓库地址
REPO = 'https://xxx/cloud'
// Jenkins凭据ID
REPO_CREDENTIALSID = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
// Publish Over SSH Remote Directory
REMOTE_DIR = '/app/cloud'
}
tools {
maven 'apache-maven-3.6.3'
}
stages {
// 检出代码
stage('Checkout') {
steps {
checkout([$class: 'SubversionSCM', additionalCredentials: [], excludedCommitMessages: '', excludedRegions: '', excludedRevprop: '', excludedUsers: '', filterChangelog: false, ignoreDirPropChanges: false, includedRegions: '', locations: [[cancelProcessOnExternalsFail: true, credentialsId: "${env.REPO_CREDENTIALSID}", depthOption: 'infinity', ignoreExternalsOption: true, local: '.', remote: "${env.REPO}"]], quietOperation: true, workspaceUpdater: [$class: 'UpdateUpdater']])
}
}
// 将deploy.sh传送到目标服务器
stage('Transfer Shell Script') {
steps {
sshPublisher(publishers: [sshPublisherDesc(configName: "${params.PUBLISH_SERVER}", transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: "chmod 777 ${REMOTE_DIR}/shell/deploy.sh", execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: 'shell', remoteDirectorySDF: false, removePrefix: '', sourceFiles: 'deploy.sh')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: true)])
}
}
// 使用Maven打包
stage('Maven Package') {
parallel {
// 打包所有项目
stage('Package All') {
when {
equals expected: "all", actual: "${params.MODULE}"
}
steps {
sh "mvn clean package -Dmaven.test.skip=true"
}
}
// 打包指定的项目
stage('Package Specific Module') {
when {
not {
equals expected: "all", actual: "${params.MODULE}"
}
}
steps {
sh "mvn clean package -pl com.github.l-qiang:${params.MODULE} -am -amd -Dmaven.test.skip=true"
}
}
}
}
// 部署: 传送jar包, 然后执行deploy.sh
stage('Deploy') {
parallel {
stage('auth') {
when {
anyOf {
equals expected: "auth", actual: "${params.MODULE}"
equals expected: "all", actual: "${params.MODULE}"
}
}
steps {
sshPublisher(publishers: [sshPublisherDesc(configName: "${params.PUBLISH_SERVER}", transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: "${REMOTE_DIR}/shell/deploy.sh restart auth ${params.ACTIVE}", execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: "auth/target", sourceFiles: "auth/target/*.jar")], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: true)])
}
}
stage('gateway') {
when {
anyOf {
equals expected: "gateway", actual: "${params.MODULE}"
equals expected: "all", actual: "${params.MODULE}"
}
}
steps {
sshPublisher(publishers: [sshPublisherDesc(configName: "${params.PUBLISH_SERVER}", transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: "${REMOTE_DIR}/shell/deploy.sh restart gateway ${params.ACTIVE}", execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: "gateway/target", sourceFiles: "gateway/target/*.jar")], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: true)])
}
}
stage('system') {
when {
anyOf {
equals expected: "system", actual: "${params.MODULE}"
equals expected: "all", actual: "${params.MODULE}"
}
}
steps {
sshPublisher(publishers: [sshPublisherDesc(configName: "${params.PUBLISH_SERVER}", transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: "${REMOTE_DIR}/shell/deploy.sh restart system ${params.ACTIVE}", execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: "service/system/target", sourceFiles: "service/system/target/*.jar")], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: true)])
}
}
}
}
}
options {
// 丢弃旧的构建, 保持构建的最大个数
buildDiscarder(logRotator(numToKeepStr: '5'))
}
}
|