|
| 1 | +// Copyright 2015 Google Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +/** |
| 16 | + * @fileoverview Gulp tasks for deploying and releasing the application. |
| 17 | + */ |
| 18 | +import child from 'child_process'; |
| 19 | +import gulp from 'gulp'; |
| 20 | +import path from 'path'; |
| 21 | + |
| 22 | +import conf from './conf'; |
| 23 | + |
| 24 | + |
| 25 | +/** |
| 26 | + * @param {!Array<string>} args |
| 27 | + * @param {function(?Error=)} doneFn |
| 28 | + */ |
| 29 | +function spawnDockerProcess(args, doneFn) { |
| 30 | + let dockerTask = child.spawn('docker', args); |
| 31 | + |
| 32 | + // Call Gulp callback on task exit. This has to be done to make Gulp dependency management |
| 33 | + // work. |
| 34 | + dockerTask.on('exit', function(code) { |
| 35 | + if (code === 0) { |
| 36 | + doneFn(); |
| 37 | + } else { |
| 38 | + doneFn(new Error('Docker command error, code:' + code)); |
| 39 | + } |
| 40 | + }); |
| 41 | + |
| 42 | + dockerTask.stdout.on('data', function (data) { |
| 43 | + console.log('' + data); |
| 44 | + }); |
| 45 | + |
| 46 | + dockerTask.stderr.on('data', function (data) { |
| 47 | + console.error('' + data); |
| 48 | + }); |
| 49 | +} |
| 50 | + |
| 51 | + |
| 52 | +/** |
| 53 | + * Creates Docker image for the application. The image is tagged with the image name configuration |
| 54 | + * constant. |
| 55 | + * |
| 56 | + * In order to run the image on a Kubernates cluster, it has to be deployed to a registry. |
| 57 | + */ |
| 58 | +gulp.task('docker-image', ['docker-file'], function(doneFn) { |
| 59 | + spawnDockerProcess([ |
| 60 | + 'build', |
| 61 | + // Remove intermediate containers after a successful build. |
| 62 | + '--rm=true', |
| 63 | + '--tag', conf.deploy.imageName, |
| 64 | + conf.paths.dist, |
| 65 | + ], doneFn); |
| 66 | +}); |
| 67 | + |
| 68 | + |
| 69 | +/** |
| 70 | + * Processes the Docker file and places it in the dist folder for building. |
| 71 | + */ |
| 72 | +gulp.task('docker-file', function() { |
| 73 | + return gulp.src(path.join(conf.paths.deploySrc, 'Dockerfile')) |
| 74 | + .pipe(gulp.dest(conf.paths.dist)); |
| 75 | +}); |
0 commit comments