-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
185 lines (157 loc) · 7.4 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
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
177
178
179
180
181
182
183
184
185
def changedFiles = []
pipeline {
agent { label 'jenkins-bc-did' }
options {
disableConcurrentBuilds()
}
environment {
IMAGE_TAG = "latest"
CONTAINER_NAME = "probe-reader"
// The name of of the nodjs application that will read data coming of the controller board.
NODEJS_DOWNLOAD = "https://nodejs.org/dist/latest-v13.x/node-v13.14.0-linux-armv7l.tar.xz"
NODEJS_DOWNLOAD_FILENAME = "node-v13.14.0-linux-armv7l.tar.xz"
NODEJS_DOWNLOAD_DIRECTORY = "node-v13.14.0-linux-armv7l"
INO_VALVE_CHANGED = "yes"
}
stages {
stage("See what files have changed") {
steps {
script {
for (changeLogSet in currentBuild.changeSets) {
for (entry in changeLogSet.getItems()) { // for each commit in the detected changes
for (file in entry.getAffectedFiles()) {
changedFiles.add(file.getPath()) // add changed file to list
}
}
}
changedFiles.unique()
for (fileName in changedFiles) {
println(fileName)
if (fileName.contains("arduino") && fileName.contains("valve-controller")) {
INO_VALVE_CHANGED = "yes"
}
}
}
}
}
// stage("Compile and deploy valve sketch") {
// when {
// expression {
// INO_VALVE_CHANGED == "yes"
// }
// }
// steps {
// script {
//
//
// env.INO = "valve-controller.ino"
//
// withCredentials([dockerCert(credentialsId: 'DOCKER_AUTH_CERTS', variable: 'LOCAL_DOCKER_CERT_PATH'),
// usernamePassword(credentialsId: 'GITHUBUSER_TOKENPASS', usernameVariable: 'USER', passwordVariable: 'PASS')]) {
//
// FLAGS = "--insecure --cert ${LOCAL_DOCKER_CERT_PATH}/cert.pem --key ${LOCAL_DOCKER_CERT_PATH}/key.pem --cacert ${LOCAL_DOCKER_CERT_PATH}/ca.pem"
//
// sh """
//
//
//
// cd src/main/arduino/valve-controller
//
// cat ${INO} | mo > ${INO}.tmp
// mv -f ${INO}.tmp ${INO}
//
// cat Dockerfile | mo > Dockerfile.tmp
// cat Dockerfile.tmp | mo > Dockerfile
//
//
// cp ${LOCAL_DOCKER_CERT_PATH}/* ~/.docker
// export DOCKER_HOST=tcp://${IOT_AQUAPONIC_IP_AND_DOCKER_PORT}
// export DOCKER_TLS_VERIFY=1
//
// docker login docker.pkg.github.com -u ${USER} -p ${PASS}
//
//
//
// docker rm ino || true
// docker build . -t ino:latest
//
// curl -X POST -H 'Content-Type: application/json' --data-binary '@deploy-container.json' https://${IOT_AQUAPONIC_IP_AND_DOCKER_PORT}/containers/create?name=ino ${FLAGS}
//
//
// docker start ino
//
// """
//
// waitUntil {
// script {
//
// CONTANER_STATUS = sh(
// script: "curl -X GET -H 'Content-Type: application/json' https://${IOT_AQUAPONIC_IP_AND_DOCKER_PORT}/containers/ino/json ${FLAGS}",
// returnStdout: true
// ).trim()
//
// def containerStatusJson = readJSON text: "${CONTANER_STATUS}"
// println containerStatusJson.State.Status
// if (containerStatusJson.State.Status == "exited") {
// return true
// } else {
// return false
// }
// }
// }
//
// }
//
//
//
// sleep(10)
// }
// }
// }
stage('Deploy the nodejs controller application') {
steps {
script {
withCredentials([string(credentialsId: 'TWILIO_SID', variable: 'TWILIO_SID'),
string(credentialsId: 'TWILIO_AUTH_TOKEN', variable: 'TWILIO_AUTH_TOKEN'),
string(credentialsId: 'TWILIO_TO', variable: 'TWILIO_TO'),
string(credentialsId: 'TWILIO_FROM', variable: 'TWILIO_FROM'),
string(credentialsId: 'SPLUNK_URL', variable: 'SPLUNK_URL'),
string(credentialsId: 'SPLUNK_TOKEN', variable: 'SPLUNK_TOKEN'),
string(credentialsId: 'HUE_API_TOKEN', variable: 'HUE_API_TOKEN'),
dockerCert(credentialsId: 'DOCKER_AUTH_CERTS', variable: 'LOCAL_DOCKER_CERT_PATH')]) {
dir("src/main/javascript/probe-reader") {
if (fileExists(NODEJS_DOWNLOAD_FILENAME)) {
println "Node js already downloaded."
} else {
sh "wget ${NODEJS_DOWNLOAD}"
}
FLAGS = "--insecure --cert ${LOCAL_DOCKER_CERT_PATH}/cert.pem --key ${LOCAL_DOCKER_CERT_PATH}/key.pem --cacert ${LOCAL_DOCKER_CERT_PATH}/ca.pem"
sh """
ls -ltra
cat Dockerfile | mo > Dockerfile.tmp
mv Dockerfile.tmp Dockerfile
cat deploy-container.json | mo > deploy-container.json.tmp
mv deploy-container.json.tmp deploy-container.json
unset DOCKER_HOST
unset DOCKER_TLS_VERIFY
docker build . -t ${REGISTRY_IP_AND_PORT}/${CONTAINER_NAME}:${IMAGE_TAG}
docker push ${REGISTRY_IP_AND_PORT}/${CONTAINER_NAME}:${IMAGE_TAG}
cp ${LOCAL_DOCKER_CERT_PATH}/* ~/.docker
export DOCKER_HOST=tcp://${IOT_AQUAPONIC_IP_AND_DOCKER_PORT}
export DOCKER_TLS_VERIFY=1
docker ps
docker stop ${CONTAINER_NAME} || true
docker rm ${CONTAINER_NAME} || true
docker pull ${REGISTRY_IP_AND_PORT}/${CONTAINER_NAME}:${IMAGE_TAG}
# It's just easier to push the docker configuration this way. I'm sure a compose file would be cleaner.
curl -X POST -H 'Content-Type: application/json' --data-binary '@deploy-container.json' https://${IOT_AQUAPONIC_IP_AND_DOCKER_PORT}/containers/create?name=${CONTAINER_NAME} ${FLAGS}
docker start ${CONTAINER_NAME}
docker system prune -f
"""
}
}
}
}
}
}
}