forked from kubesphere-sigs/ks
/
jenkinsfile_template.go
176 lines (163 loc) · 4.4 KB
/
jenkinsfile_template.go
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
169
170
171
172
173
174
175
176
package pipeline
var jenkinsfileTemplateForSimple = `
pipeline {
agent any
parameters {
string defaultValue: 'rick', description: 'just for testing', name: 'name', trim: true
booleanParam defaultValue: false, description: 'You can use this flag to debug your Pipeline', name: 'debug'
choice choices: ['v1.18.8', 'v1.18.9'], description: 'Please choose the target Kubernetes version', name: 'kubernetesVersion'
}
environment {
APP_NAME = "this is a sample app"
}
stages{
stage('simple'){
steps{
echo "My name is ${params.name}."
echo "Target Kubernetes version is " + params.kubernetesVersion
echo "env " + env.APP_NAME
script {
if ("${params.debug}" == "true") {
echo "You can put some debug info at here."
echo "Another way to use param: " + params.name
}
}
}
}
stage('debug stage') {
when {
equals expected: true, actual: params.debug
}
steps {
echo "It's joke, there're debug info here."
script {
result = input message: 'Please input your name!', ok: 'Confirm',
parameters: [string(defaultValue: 'rick',
description: 'This should not be your real name.', name: 'name', trim: true)]
echo result
}
}
}
stage('parallel'){
parallel {
stage('channel-1'){
steps{
input message: 'Please input your age!', ok: 'Confirm',
parameters: [string(defaultValue: '18',
description: 'Just a joke.', name: 'age', trim: true)]
}
}
stage('channel-2'){
steps{
input message: 'Please input your weight!', ok: 'Confirm',
parameters: [string(defaultValue: '50',
description: 'Just a joke.', name: 'weight', trim: true)]
}
}
}
}
}
}`
var jenkinsfileTemplateForJava = `
pipeline {
agent {
node {
label 'maven'
}
}
stages {
stage('Clone') {
steps {
git(url: 'https://github.com/kubesphere-sigs/demo-java', changelog: true, poll: false)
}
}
stage('Build & Test') {
steps {
container('maven') {
sh 'mvn package test'
}
}
}
stage('Code Scan') {
steps {
withSonarQubeEnv('sonar') {
container('maven') {
sh '''mvn --version
mvn sonar:sonar \\
-Dsonar.projectKey=test \\
-Dsonar.host.url=http://139.198.9.130:30687/ \\
-Dsonar.login=b3e146cdb76ecef5ffb12743779cd78e69a4b5c5'''
}
}
waitForQualityGate 'false'
}
}
stage('Build Image') {
steps {
container('maven') {
withCredentials([usernamePassword(credentialsId : 'docker' ,passwordVariable : 'PASS' ,usernameVariable : 'USER' ,)]) {
sh '''docker login -u $USER -p $PASS
cat <<EOM >Dockerfile
FROM kubesphere/java-8-centos7:v2.1.0
COPY target/demo-java-1.0.0.jar demo.jar
COPY target/lib demo-lib
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "demo.jar"]
EOM
docker build . -t surenpi/java-demo
docker push surenpi/java-demo'''
}
}
}
}
}
}
`
var jenkinsfileTemplateForGo = `
pipeline {
agent {
node {
label 'go'
}
}
stages {
stage('Code Clone') {
steps {
git(url: 'https://github.com/kubesphere-sigs/demo-go-http', changelog: true, poll: false)
}
}
stage('Test & Code Scan') {
steps {
container('go') {
sh 'go test ./... -coverprofile=covprofile'
withCredentials([string(credentialsId : 'sonar-token' ,variable : 'TOKEN' ,)]) {
withSonarQubeEnv('sonar') {
sh 'sonar-scanner -Dsonar.login=$TOKEN'
}
}
}
waitForQualityGate 'false'
}
}
stage('Build Image & Push') {
steps {
container('go') {
sh ''' CGO_ENABLED=0 GOARCH=amd64 go build -o bin/go-server -ldflags "-w"
chmod u+x bin/go-server'''
withCredentials([usernamePassword(credentialsId : 'rick-docker-hub' ,passwordVariable : 'PASS' ,usernameVariable : 'USER' ,)]) {
sh 'echo "$PASS" | docker login -u "$USER" --password-stdin'
sh '''cat <<EOM >Dockerfile
FROM alpine
COPY bin/go-server go-server
EXPOSE 80
ENTRYPOINT ["go-server"]
EOM
docker build . -t surenpi/go-demo
docker push surenpi/go-demo'''
}
}
}
}
}
}
`