forked from hulquest/ez-rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
146 lines (140 loc) · 4.46 KB
/
Jenkinsfile
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
void setBuildStatus(String message, String state) {
step([
$class: "GitHubCommitStatusSetter",
reposSource: [$class: "ManuallyEnteredRepositorySource", url: "https://github.com/NetApp/ez-rancher"],
contextSource: [$class: "ManuallyEnteredCommitContextSource", context: "ci/jenkins/build-status"],
errorHandlers: [[$class: "ChangingBuildStatusErrorHandler", result: "UNSTABLE"]],
statusResultSource: [ $class: "ConditionalStatusResultSource", results: [[$class: "AnyBuildResult", message: message, state: state]] ]
]);
}
pipeline {
triggers {
cron(env.BRANCH_NAME == 'main' ? '0 0 * * 0' : '')
}
agent {
kubernetes {
label 'ez-rancher'
defaultContainer 'jnlp'
yaml """
apiVersion: v1
kind: Pod
metadata:
labels:
app: jenkins
component: builder
spec:
serviceAccountName: jenkins
containers:
- name: tf
image: hashicorp/terraform:0.14.0
tty: true
command: ["cat"]
- name: dind
image: docker:dind
args:
- --dns-opt='options single-request'
securityContext:
privileged: true
volumeMounts:
- name: dind-storage
mountPath: /var/lib/docker
volumes:
- name: dind-storage
emptyDir: {}
"""
}
}
environment {
COMMIT_SLUG = "${GIT_COMMIT}".substring(0,8)
}
stages {
stage ('Prepare') {
steps {
container('jnlp') {
script {
def myRepo = checkout scm
def gitCommit = "${GIT_COMMIT}"
def gitBranch = "${GIT_BRANCH}"
def previousGitCommit = sh(script: "git rev-parse ${gitCommit}~", returnStdout: true)
}
} }
}
stage ('Validate') {
steps {
setBuildStatus("Build succeeded", "PENDING");
container('tf') {
sh """
apk add --no-cache curl
terraform fmt -check -recursive -diff .
"""
}
container('tf') {
sh """
cd terraform/vsphere-rancher
terraform init
"""
}
container('tf') {
sh """
terraform validate
"""
}
}
}
stage ('Build') {
steps {
container('dind') {
sh """
docker build --network host --no-cache -t ez-rancher:${COMMIT_SLUG} .
"""
}
}
}
stage ('Deploy') {
steps {
container('dind') {
withCredentials([file(credentialsId: 'ez-rancher-olab', variable: 'TFVARS')]) {
sh """
cd terraform/vsphere-rancher
apk add --no-cache bash make
cat ${TFVARS} > env.list
echo "TF_VAR_vm_name=ezr-${COMMIT_SLUG}" >> env.list
mkdir deliverables
EZR_IMAGE_NAME=ez-rancher
EZR_IMAGE_TAG=latest
DELIVERABLES=deliverables
docker run --rm --env-file env.list \
-v `pwd`/deliverables:/terraform/vsphere-rancher/deliverables \
--network host \
ez-rancher:${COMMIT_SLUG} apply -auto-approve -input=false
"""
}
}
}
}
}
post {
always {
container('dind') {
sh """
cd terraform/vsphere-rancher
docker run --rm --env-file env.list \
-v `pwd`/deliverables:/terraform/vsphere-rancher/deliverables \
--network host \
ez-rancher:${COMMIT_SLUG} destroy -auto-approve -input=false
"""
}
container('dind') {
sh """
docker rmi ez-rancher:${COMMIT_SLUG}
"""
}
}
success {
setBuildStatus("Build succeeded", "SUCCESS");
}
failure {
setBuildStatus("Build failed", "FAILURE");
}
}
}