@@ -2,59 +2,21 @@ import * as fs from "fs";
2
2
import * as gulp from "gulp" ;
3
3
import * as shell from "gulp-shell" ;
4
4
5
- const contents = fs . readFileSync ( "./package.json" ) . toString ( ) ;
6
-
7
- const npmPackage = JSON . parse ( contents ) ;
8
-
9
- const version = npmPackage . version ;
10
- const [ versionMajor , versionMajMin ] = versionMajorMinor ( version ) ;
11
- const repo = npmPackage . dockerRepository ;
12
- const imageName = npmPackage . dockerImageName || npmPackage . name ;
13
-
14
- const dockerRepoImage = `${ repo } /${ imageName } ` ;
15
-
16
5
///
17
- // The objective here is to build and tag the actual version, mark it as latest, and also tag it with just the major
18
- // version and major.minor as the "latest" within those scopes. Iterations of deploy-services should generally use
19
- // major.minor in the Compose file rather that just latest. This facilitates side-by-side deployments of current and
20
- // next version systems where pulling "latest" doesn't affect the older system on subsequent up commands.
6
+ // Build and tag the actual version, mark as latest, and also tag it with just the major and minor versions.
21
7
///
8
+ const [ buildTask , tagTask , pushTask ] = createShellTasks ( "./package.json" ) ;
22
9
23
- const imageWithVersion = `${ dockerRepoImage } :${ version } ` ;
24
- const imageWithVersionMajor = versionMajor ? `${ dockerRepoImage } :${ versionMajor } ` : null ;
25
- const imageWithVersionMajMin = versionMajMin ? `${ dockerRepoImage } :${ versionMajMin } ` : null ;
26
- const imageAsLatest = `${ dockerRepoImage } :latest` ;
10
+ gulp . task ( "default" , [ "docker-build" ] ) ;
27
11
28
- const buildCommand = `docker build --tag ${ imageWithVersion } .` ;
29
- const tagMajorCommand = imageWithVersionMajor ? `docker tag ${ imageWithVersion } ${ imageWithVersionMajor } ` : `echo "could not tag with major version"` ;
30
- const tagMajMinCommand = imageWithVersionMajMin ? `docker tag ${ imageWithVersion } ${ imageWithVersionMajMin } ` : `echo "could not tag with major.minor version"` ;
31
- const tagLatestCommand = `docker tag ${ imageWithVersion } ${ imageAsLatest } ` ;
12
+ gulp . task ( "build" , buildTask ) ;
32
13
33
- const pushCommand = `docker push ${ imageWithVersion } ` ;
34
- const pushMajorCommand = imageWithVersionMajor ? `docker push ${ imageWithVersionMajor } ` : `echo "could not push major version"` ;
35
- const pushMajMinCommand = imageWithVersionMajMin ? `docker push ${ imageWithVersionMajMin } ` : `echo "could not push major.minor version"` ;
36
- const pushLatestCommand = `docker push ${ imageAsLatest } ` ;
14
+ gulp . task ( "docker-build" , [ "build" ] , tagTask ) ;
37
15
38
- gulp . task ( "default " , [ "docker-build" ] ) ;
16
+ gulp . task ( "docker-push " , [ "docker-build" ] , pushTask ) ;
39
17
40
18
gulp . task ( "release" , [ "docker-push" ] ) ;
41
19
42
- gulp . task ( "docker-build" , shell . task ( [
43
- buildCommand ,
44
- tagMajorCommand ,
45
- tagMajMinCommand ,
46
- tagLatestCommand
47
- ] )
48
- ) ;
49
-
50
- gulp . task ( "docker-push" , [ "docker-build" ] , shell . task ( [
51
- pushCommand ,
52
- pushMajorCommand ,
53
- pushMajMinCommand ,
54
- pushLatestCommand
55
- ] )
56
- ) ;
57
-
58
20
function versionMajorMinor ( version : string ) {
59
21
const parts = version . split ( "." ) ;
60
22
@@ -64,3 +26,62 @@ function versionMajorMinor(version: string) {
64
26
65
27
return [ null , null ] ;
66
28
}
29
+
30
+ function createShellTasks ( sourceFile : string ) {
31
+ // Clean and build
32
+ const cleanCommand = `rm -rf dist` ;
33
+
34
+ const compileTypescript = `tsc -p tsconfig.prod.json` ;
35
+
36
+ const moveFiles = `cp ./{package.json,yarn.lock,LICENSE,docker-entry.sh} dist` ;
37
+ const webpack = `webpack` ;
38
+
39
+ const contents = fs . readFileSync ( sourceFile ) . toString ( ) ;
40
+
41
+ const npmPackage = JSON . parse ( contents ) ;
42
+
43
+ const version = npmPackage . version ;
44
+ const [ versionMajor , versionMajMin ] = versionMajorMinor ( version ) ;
45
+ const repo = npmPackage . dockerRepository ;
46
+ const imageName = npmPackage . dockerImageName || npmPackage . name ;
47
+
48
+ const dockerRepoImage = `${ repo } /${ imageName } ` ;
49
+
50
+ const imageWithVersion = `${ dockerRepoImage } :${ version } ` ;
51
+ const imageWithVersionMajor = versionMajor ? `${ dockerRepoImage } :${ versionMajor } ` : null ;
52
+ const imageWithVersionMajMin = versionMajMin ? `${ dockerRepoImage } :${ versionMajMin } ` : null ;
53
+ const imageAsLatest = `${ dockerRepoImage } :latest` ;
54
+
55
+ // Docker build/tag
56
+ const buildCommand = `docker build --tag ${ imageWithVersion } .` ;
57
+ const tagMajorCommand = imageWithVersionMajor ? `docker tag ${ imageWithVersion } ${ imageWithVersionMajor } ` : `echo "could not tag with major version"` ;
58
+ const tagMajMinCommand = imageWithVersionMajMin ? `docker tag ${ imageWithVersion } ${ imageWithVersionMajMin } ` : `echo "could not tag with major.minor version"` ;
59
+ const tagLatestCommand = `docker tag ${ imageWithVersion } ${ imageAsLatest } ` ;
60
+
61
+ // Docker push
62
+ const pushCommand = `docker push ${ imageWithVersion } ` ;
63
+ const pushMajorCommand = imageWithVersionMajor ? `docker push ${ imageWithVersionMajor } ` : `echo "could not push major version"` ;
64
+ const pushMajMinCommand = imageWithVersionMajMin ? `docker push ${ imageWithVersionMajMin } ` : `echo "could not push major.minor version"` ;
65
+ const pushLatestCommand = `docker push ${ imageAsLatest } ` ;
66
+
67
+ return [
68
+ shell . task ( [
69
+ cleanCommand ,
70
+ compileTypescript ,
71
+ moveFiles ,
72
+ webpack
73
+ ] ) ,
74
+ shell . task ( [
75
+ buildCommand ,
76
+ tagMajorCommand ,
77
+ tagMajMinCommand ,
78
+ tagLatestCommand
79
+ ] ) ,
80
+ shell . task ( [
81
+ pushCommand ,
82
+ pushMajorCommand ,
83
+ pushMajMinCommand ,
84
+ pushLatestCommand
85
+ ] )
86
+ ] ;
87
+ }
0 commit comments