-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.ts
174 lines (140 loc) · 4.33 KB
/
gulpfile.ts
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
import webpackConfig from './webpack.config'
import webpack from 'webpack'
import rimraf from 'rimraf'
import { series } from 'gulp'
import { spawn } from 'child_process'
import { forEach } from 'lodash'
import { version, author, repository } from './package.json'
import fs from 'fs/promises'
export const buildWebpack = async () => {
return new Promise<void>((resolve, reject) => {
webpack(webpackConfig, (err, stats) => {
if (err) {
console.error(err.stack || err)
reject(err)
return
}
if (stats === undefined) {
reject(new Error('stats is undefined'))
return
}
const out = stats.toString({
colors: true,
})
if (stats.hasErrors()) {
console.error(out)
reject(new Error())
return
} else if (stats.hasWarnings()) {
console.warn(out)
} else {
console.log(out)
}
resolve()
})
})
}
buildWebpack.displayName = 'build-webpack'
type GitInfo = {
hash: string
branch?: string | undefined
}
const getGitInfo = async (): Promise<GitInfo> => {
const rev = (await fs.readFile('.git/HEAD')).toString().trim()
if (rev.indexOf(':') === -1) {
return {
hash: rev,
}
} else {
const hash = (await fs.readFile('.git/' + rev.substring(5))).toString().trim()
const branch = rev.substring(16)
return {
hash,
branch,
}
}
}
type Labels = 'created' | 'version' | 'authors' | 'url' | 'source' | 'revision' | 'title' | 'description'
const getLabels = async (): Promise<Record<Labels, string>> => {
const gitInfo = await getGitInfo()
const title = `autoenv-nginx/${version}/${gitInfo.hash}`
return {
version,
created: new Date().toISOString(),
authors: author,
url: repository,
source: repository,
revision: gitInfo.hash,
title,
description: title,
}
}
const registry = 'ghcr.io/d-tw/autoenv-nginx'
const run = async (command: string, args: string[], env?: Record<string, string>): Promise<void> => {
return new Promise<void>((resolve, reject) => {
const proc = spawn(command, args, { env: { ...process.env, ...env } })
proc.stdout.on('data', data => console.log(data.toString()))
proc.stderr.on('data', data => console.error(data.toString()))
proc.on('close', code => {
if (code !== 0) {
reject(code)
} else {
resolve()
}
})
})
}
const buildx = async (push = false) => {
const args: string[] = ['buildx', 'build']
const labels = await getLabels()
const gitInfo = await getGitInfo()
forEach(labels, (value, label) => {
args.push('--label', `org.opencontainers.image.${label}=${value}`)
})
const shaTag = `sha-${gitInfo.hash.substring(0, 8)}`
const tags = [version, shaTag]
switch (gitInfo.branch) {
case 'main':
case 'master':
tags.push('latest')
}
for (const tag of tags) {
args.push('--tag', `${registry}:${tag}`)
}
if (push) {
args.push('--push')
}
args.push('-f', 'Dockerfile')
args.push('dist')
return run('docker', args)
}
export const buildContainer = async () => {
return buildx()
}
buildContainer.displayName = 'build-container'
export const pushContainer = async () => {
return buildx(true)
}
pushContainer.displayName = 'push-container'
export const clean = async () => {
return new Promise<void>((resolve, reject) => {
rimraf('./dist', err => {
if (err === undefined || err === null) {
resolve()
} else {
reject(err)
}
})
})
}
export const build = series(clean, buildWebpack)
export const test = async () => {
const args: string[] = []
args.push('-f', 'test/docker-compose.yml')
args.push('up')
args.push('--force-recreate', '--remove-orphans')
args.push('--exit-code-from', 'test_runner')
args.push('--abort-on-container-exit')
const image = `${registry}:${version}`
return run('docker-compose', args, { AUTOENV_IMAGE: image })
}