forked from caprover/caprover
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy-to-docker-hub.js
executable file
·154 lines (129 loc) · 4.09 KB
/
deploy-to-docker-hub.js
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
#!/usr/bin/env node
const execOriginal = require('child_process').exec
const requestOriginal = require('request')
function exec(command) {
return new Promise(function (resolve, reject) {
processToRun = execOriginal(command, function (err, stdout, stderr) {
if (stderr) {
console.log('stderr')
console.log(stderr)
}
if (err) {
reject(err)
return
}
resolve(stdout)
})
if (processToRun.stdout && processToRun.stdout.pipe) {
processToRun.stdout.pipe(process.stdout)
}
})
}
function request(url) {
return new Promise(function (resolve, reject) {
requestOriginal(url, function (error, response, body) {
if (body) {
body = JSON.parse(body)
}
if (error || !body) {
console.log('Error while fetching tags from Docker Hub!')
reject(error)
return
}
resolve(body)
})
})
}
const publishedNameOnDockerHub = 'caprover/caprover'
let version = ''
exec('npm run build')
.then(function (data) {
data = (data + '').trim()
console.log('----------')
console.log(data)
console.log('----------')
if (
!data.startsWith('> [email protected]') ||
!data.endsWith('rm -rf ./built && npx tsc')
) {
console.log('Unexpected output:')
throw new Error(data)
}
version = require('./built/utils/CaptainConstants').configs.version
return exec(`rm -rf ./built`)
})
.then(function () {
return exec('git status')
})
.then(function (data) {
var l1 = 'On branch master'
var l2 = "Your branch is up-to-date with 'origin/master'"
var l3 = 'nothing to commit, working tree clean'
if (
data.indexOf(l1) < 0 ||
data.indexOf(l2) < 0 ||
data.indexOf(l3) < 0
) {
console.log(
'Make sure you are on master branch, in sync with remote, and your working directory is clean'
)
throw new Error(data)
}
var URL =
'https://hub.docker.com/v2/repositories/' +
publishedNameOnDockerHub +
'/tags'
return request(URL)
})
.then(function (body) {
if (!body.results || !body.results.length) {
console.log('Error while fetching tags from Docker Hub!')
throw error
}
var highestTag = ''
var highestTagValue = 0
function getTagValue(tag) {
var sp = tag.split('.')
return (
Number(sp[0]) * 1000 * 1000 +
Number(sp[1]) * 1000 +
Number(sp[2])
)
}
for (var i = 0; i < body.results.length; i++) {
var t = body.results[i].name
var value = getTagValue(t)
if (value > highestTagValue) {
highestTagValue = value
highestTag = t
}
}
var isVersionValid = false
if (getTagValue(version) > highestTagValue) {
isVersionValid = true
}
if (!isVersionValid || !highestTagValue || !version) {
console.log(
`isVersionValid: ${isVersionValid} highestTagValue: ${highestTagValue}`
)
throw new Error(
'The version you are pushing is not valid! ' + version
)
}
console.log('Pushing ' + version)
var t1 = publishedNameOnDockerHub + ':' + 'latest'
var t2 = publishedNameOnDockerHub + ':' + version
return exec(
`sudo docker build -t ${t1} -t ${t2} -f dockerfile-captain.release . && docker push ${t1} && docker push ${t2}`
)
})
.then(function (stdout) {
if (stdout) {
console.log('stdout')
console.log(stdout)
}
})
.catch(function (err) {
console.error(err)
process.exit(1)
})