-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJenkinsfile.groovy
More file actions
137 lines (116 loc) · 4.65 KB
/
Jenkinsfile.groovy
File metadata and controls
137 lines (116 loc) · 4.65 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
137
class Build{
String description
}
//def getRepoURL() {
// sh "git config --get remote.origin.url > .git/remote-url"
// return readFile(".git/remote-url").trim()
//}
//
//def getCommitSha() {
// sh "git rev-parse HEAD > .git/current-commit"
// return readFile(".git/current-commit").trim()
//}
//
//def updateGithubCommitStatus(build) {
// // workaround https://issues.jenkins-ci.org/browse/JENKINS-38674
// repoURL = getRepoURL()
// commitSha = getCommitSha()
//
// step([
// $class: 'GitHubCommitStatusSetter',
// reposSource: [$class: "ManuallyEnteredRepositorySource", url: repoURL],
//
// commitShaSource: [$class: "ManuallyEnteredShaSource", sha: commitSha],
// errorHandlers: [[$class: 'ShallowAnyErrorHandler']],
// statusResultSource: [
// $class: 'ConditionalStatusResultSource',
// results: [
//// [$class: 'AnyBuildResult', result: 'SUCCESS', state: 'SUCCESS', message: build.description]
// [$class: 'BetterThanOrEqualBuildResult', result: 'SUCCESS', state: 'SUCCESS', message: build.description],
// [$class: 'BetterThanOrEqualBuildResult', result: 'FAILURE', state: 'FAILURE', message: build.description],
// [$class: 'AnyBuildResult', state: 'FAILURE', message: 'Loophole']
// ]
// ]
// ])
//}
//
//void setBuildStatus(String message, String state) {
// step([
// $class: "GitHubCommitStatusSetter",
// reposSource: [$class: "ManuallyEnteredRepositorySource", url: "git@github.com:FinhackerTech/SmarketServer.git"],
// contextSource: [$class: "ManuallyEnteredCommitContextSource", context: "ci/jenkins/build-status"],
// errorHandlers: [[$class: "ChangingBuildStatusErrorHandler", result: "UNSTABLE"]],
// statusResultSource: [ $class: "ConditionalStatusResultSource", results: [[$class: "AnyBuildResult", message: message, state: state]] ]
// ]);
//}
node("yxw") {
def workspace = pwd()
def git_branch = 'main'
def git_repository ='git@github.com:FinhackerTech/SmarketServer.git'
def vm_ip = '60.205.183.238'
def vm_port = '22'
def vm_user = 'lyk'
def IMAGE_NAME = 'smarket_server'
def IMAGE_NAME_WITH_TAG = 'smarket_server:latest'
def IMAGE_TO_RUN = 'lyklove/smarket_server:latest'
def CONTAINER_NAME = 'smarket_server'
stage('clone from gitlab into slave\'s workspace') {
echo "workspace: ${workspace}"
git branch: "${git_branch}", url: "${git_repository}"
}
stage('cd to build context') {
echo "the context now is:"
sh "ls -al"
sh "cd ${workspace}"
echo "cd to build context, now the context is:"
sh "ls -al"
}
stage('build jar on slave machine, jacoco file generated') {
echo 'jdk should be 11'
sh 'java --version'
sh 'mvn --version'
sh "mvn clean package jacoco:report -Dmaven.test.failure.ignore=true"
echo "build finish on ${vm_ip}"
}
stage( 'testing, using jacoco' ) {
jacoco (
execPattern: '**/target/jacoco.exec',
classPattern: '**/classes',
sourcePattern: '**/src/main/java',
exclusionPattern: '**/src/test*',
// inclusionPattern: '**/com/hel/auto/service/*.class,**/com/hel/auto/controller/*.class',
)
}
stage("build docker image"){
sh "docker build -t ${IMAGE_NAME} ."
}
stage("login to dockerhub"){
withCredentials([usernamePassword(credentialsId: 'DOCKERHUB_KEY', passwordVariable: 'password', usernameVariable: 'username')]) {
sh 'docker login -u $username -p $password'
}
}
stage("push to dockerhub"){
echo "begin push to dockerhub"
sh "docker image tag ${IMAGE_NAME_WITH_TAG} lyklove/${IMAGE_NAME_WITH_TAG}"
sh "docker image push lyklove/${IMAGE_NAME_WITH_TAG}"
}
stage("clean previous image and container"){
sh "docker container rm -f ${CONTAINER_NAME}"
sh "docker image rm ${IMAGE_NAME_WITH_TAG}"
sh "docker image rm ${IMAGE_TO_RUN}"
}
stage( "pull image" ){
sh "docker pull lyklove/${IMAGE_NAME_WITH_TAG}"
}
stage("run container") {
sh "docker image ls"
sh "docker container run --name ${CONTAINER_NAME} --net=host -d ${IMAGE_TO_RUN}"
}
// stage("signal github: deployed"){
// def build = new Build(description: "build success!!!")
//
//// updateGithubCommitStatus(build)
//
// setBuildStatus("Build complete", "SUCCESS");
// }
}