-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathJenkinsfile
More file actions
108 lines (99 loc) · 4.66 KB
/
Jenkinsfile
File metadata and controls
108 lines (99 loc) · 4.66 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
pipeline {
agent {
node {
label 'pi'
}
}
environment {
DOCKER_CREDS = credentials('docker_hub')
}
stages {
stage('pull code') {
steps {
git(url: 'https://github.com/sam-hudson02/TwitchSpotifyBot', branch: 'main', credentialsId: 'personal_github')
}
}
stage('build docker image') {
steps {
script {
// increment the version number
def version = env.TSB_VERSION
version = version.toInteger() + 1
env.TSB_VERSION = version
def versionString = version.toString()
// add zero to start of version if needed
if (versionString.length() == 2) {
versionString = "0" + versionString
}
// add . between each number
def versionStringFormatted = versionString.split('').join('.')
// build docker image with version and latest tags
sh "docker build -t samhudson02/sbotify:${versionStringFormatted} -t samhudson02/sbotify:latest ."
// login to docker hub
sh 'docker login -u $DOCKER_CREDS_USR -p $DOCKER_CREDS_PSW'
// push docker image to docker hub
sh "docker push samhudson02/sbotify:${versionStringFormatted}"
def branch = sh(returnStdout: true, script: 'git rev-parse --abbrev-ref HEAD').trim()
if (branch == "main") {
sh "docker push samhudson02/sbotify:latest"
}
}
}
}
}
post {
success {
script {
def gitCommit = sh(returnStdout: true, script: 'git rev-parse HEAD').trim()
def gitAuthor = sh(returnStdout: true, script: 'git --no-pager show -s --format=%an').trim()
def dateTime = sh(returnStdout: true, script: 'date +%d-%m-%Y_%H:%M:%S').trim()
// remove the 'and counting' from the duration string
def buildTime = currentBuild.durationString.split('and')[0].trim()
def commitURL = "https://github.com/sam-hudson02/TwitchSpotifyBot/commit/" + gitCommit
def commitTitle = sh(returnStdout: true, script: 'git log -1 --pretty=%B').trim()
def payload = [
"build_id": env.BUILD_ID,
"build_name": "TwitchSpotifyBot",
"build_url": env.BUILD_URL,
"build_date": dateTime,
"commit_author": gitAuthor,
"commit_title": commitTitle,
"commit_url": commitURL,
"build_result": "SUCCESS",
"build_duration": buildTime
]
def payloadString = payload.collect{ k,v -> "\"${k}\":\"${v}\"" }.join(',')
def payloadJson = "{${payloadString}}"
sh "curl -d '${payloadJson}' -H 'Content-Type: application/json' http://192.168.3.101:5005/build-notify"
}
}
failure {
script {
def gitCommit = sh(returnStdout: true, script: 'git rev-parse HEAD').trim()
def gitAuthor = sh(returnStdout: true, script: 'git --no-pager show -s --format=%an').trim()
def dateTime = sh(returnStdout: true, script: 'date +%d-%m-%Y_%H:%M:%S').trim()
// remove the 'and counting' from the duration string
def buildTime = currentBuild.durationString.split('and')[0].trim()
def commitURL = "https://github.com/sam-hudson02/TwitchSpotifyBot/commit/" + gitCommit
def commitTitle = sh(returnStdout: true, script: 'git log -1 --pretty=%B').trim()
def payload = [
"build_id": env.BUILD_ID,
"build_name": "TwitchSpotifyBot",
"build_url": env.BUILD_URL,
"build_date": dateTime,
"commit_author": gitAuthor,
"commit_title": commitTitle,
"commit_url": commitURL,
"build_result": "FAILURE",
"build_duration": buildTime
]
def payloadString = payload.collect{ k,v -> "\"${k}\":\"${v}\"" }.join(',')
def payloadJson = "{${payloadString}}"
sh "curl -d '${payloadJson}' -H 'Content-Type: application/json' http://192.168.3.101:5005/build-notify"
}
}
}
tools {
nodejs 'nodejs'
}
}