-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile-sh.groovy
109 lines (100 loc) · 3.15 KB
/
Jenkinsfile-sh.groovy
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
properties([
parameters([
// Container parameters
string(defaultValue: '192.168.48.2:5002/jenkins/jnlp-image-with-tools:latest', description: '', name: 'TASK_IMAGE', trim: false),
string(defaultValue: 'pvc-ws', description: '', name: 'TASK_PVC', trim: false),
string(defaultValue: '8Gi', description: '', name: 'TSK_MEM_LIMIT', trim: false),
string(defaultValue: '4000m', description: '', name: 'TSK_CPU_LIMIT', trim: false),
// Genenric parameters
string(defaultValue: '30', description: '', name: 'TASK_TIMEOUT', trim: false),
string(defaultValue: 'regcred', description: '', name: 'IMAGE_PULL_SECRETS', trim: false),
// Task parameters
string(defaultValue: '.', description: '', name: 'SROUCE', trim: false),
string(defaultValue: 'bHMgLWFsCg==', description: 'shell script encoded by base64', name: 'SCRIPT',trim: false)
])
])
// Task parameters
def source = params.SROUCE
byte[] decoded = params.SCRIPT.decodeBase64()
def script = new String(decoded)
// Container parameters
def taskPVC = params.TASK_PVC
def taskTmage = params.TASK_IMAGE
def taskMemoryLimit = params.TSK_MEM_LIMIT
def taskCpuLimit = params.TSK_CPU_LIMIT
// Generic parameters for task
def taskTimeout = params.TASK_TIMEOUT
def imagePullSecrets = params.IMAGE_PULL_SECRETS
// Generic parameters for Pipeline
def mDefaultContainer = "main"
def mCustomWorkspace = '/home/jenkins/agent/workspace/ws'
def mYaml = """\
apiVersion: v1
kind: Pod
metadata:
name: jenkins-slave
spec:
containers:
- name: "jnlp"
image: "192.168.48.2:5002/jenkins/jnlp-slave:4.7-1-jdk11"
imagePullPolicy: "Always"
tty: true
resources:
requests:
memory: "256Mi"
cpu: "100m"
- name: $mDefaultContainer
image: $taskTmage
imagePullPolicy: "Always"
tty: true
command:
- cat
securityContext:
privileged: true
volumeMounts:
- mountPath: "/var/run/docker.sock"
name: "docker-sock"
readOnly: false
resources:
requests:
memory: "256Mi"
cpu: "100m"
limits:
memory: $taskMemoryLimit
cpu: $taskCpuLimit
imagePullSecrets:
- name: $imagePullSecrets
nodeSelector:
kubernetes.io/hostname: "node13"
restartPolicy: Never
volumes:
- hostPath:
path: "/var/run/docker.sock"
name: "docker-sock"
""".stripIndent()
// Uses Declarative syntax to run commands inside a container.
pipeline {
agent {
kubernetes {
yaml mYaml
defaultContainer mDefaultContainer
workspaceVolume persistentVolumeClaimWorkspaceVolume(claimName: taskPVC, readOnly: false)
customWorkspace mCustomWorkspace
}
}
options { timestamps () }
stages {
stage('Task') {
options {
timeout(time: taskTimeout, unit: 'MINUTES')
}
steps {
echo "Task starts =============================================================="
dir(source) {
sh script
}
echo "Task ends =============================================================="
}
}
}
}