1 | const cli = require('heroku-cli-util')
|
2 | const Sanbashi = require('../lib/sanbashi')
|
3 |
|
4 | let usage = `
|
5 | ${cli.color.bold.underline.magenta('Usage:')}
|
6 | ${ cli.color.cmd('heroku container:push web')} # Pushes Dockerfile to web process type
|
7 | ${ cli.color.cmd('heroku container:push web worker --recursive')} # Pushes Dockerfile.web and Dockerfile.worker
|
8 | ${ cli.color.cmd('heroku container:push --recursive')} # Pushes Dockerfile.*`
|
9 |
|
10 | module.exports = function (topic) {
|
11 | return {
|
12 | topic: topic,
|
13 | command: 'push',
|
14 | description: 'builds, then pushes Docker images to deploy your Heroku app',
|
15 | needsApp: true,
|
16 | needsAuth: true,
|
17 | variableArgs: true,
|
18 | help: usage,
|
19 | flags: [
|
20 | {
|
21 | name: 'verbose',
|
22 | char: 'v',
|
23 | hasValue: false
|
24 | },
|
25 | {
|
26 | name: 'recursive',
|
27 | char: 'R',
|
28 | hasValue: false,
|
29 | description: 'pushes Dockerfile.<process> found in current and subdirectories'
|
30 | }
|
31 | ],
|
32 | run: cli.command(push)
|
33 | }
|
34 | }
|
35 |
|
36 | let push = async function (context, heroku) {
|
37 | const recurse = !!context.flags.recursive
|
38 | if (context.args.length === 0 && !recurse) {
|
39 | cli.error(`Error: Requires either --recursive or one or more process types\n ${usage} `)
|
40 | process.exit(1)
|
41 | }
|
42 | if (context.args.length > 1 && !recurse) {
|
43 | cli.error(`Error: Please specify one target process type\n ${usage} `)
|
44 | process.exit(1)
|
45 | }
|
46 | let herokuHost = process.env.HEROKU_HOST || 'heroku.com'
|
47 | let registry = `registry.${ herokuHost }`
|
48 | let dockerfiles = Sanbashi.getDockerfiles(process.cwd(), recurse)
|
49 |
|
50 | let possibleJobs = Sanbashi.getJobs(`${ registry }/${ context.app }`, dockerfiles)
|
51 | let jobs = []
|
52 | if (recurse) {
|
53 | if (context.args.length) {
|
54 | possibleJobs = Sanbashi.filterByProcessType(possibleJobs, context.args)
|
55 | }
|
56 | jobs = await Sanbashi.chooseJobs(possibleJobs)
|
57 | } else if (possibleJobs.standard) {
|
58 | possibleJobs.standard.forEach((pj) => { pj.resource = pj.resource.replace(/standard$/, context.args[0])})
|
59 | jobs = possibleJobs.standard || []
|
60 | }
|
61 | if (!jobs.length) {
|
62 | cli.warn('No images to push')
|
63 | process.exit(1)
|
64 | }
|
65 | try {
|
66 | for (let job of jobs) {
|
67 | if (job.name === 'standard') {
|
68 | cli.styledHeader(`Building for ${context.args} (${job.dockerfile })`)
|
69 | } else {
|
70 | cli.styledHeader(`Building ${job.name} (${job.dockerfile})`)
|
71 | }
|
72 | await Sanbashi.buildImage(job.dockerfile, job.resource, context.flags.verbose)
|
73 | }
|
74 | }
|
75 | catch (err) {
|
76 | cli.error(`Error: docker build exited with ${ err }`)
|
77 | cli.hush(err.stack || err)
|
78 | process.exit(1)
|
79 | }
|
80 |
|
81 | try {
|
82 | for (let job of jobs) {
|
83 | if (job.name === 'standard') {
|
84 | cli.styledHeader(`Pushing for ${context.args} (${job.dockerfile })`)
|
85 | } else {
|
86 | cli.styledHeader(`Pushing ${job.name} (${job.dockerfile })`)
|
87 | }
|
88 | await Sanbashi.pushImage(job.resource, context.flags.verbose)
|
89 | }
|
90 | }
|
91 | catch (err) {
|
92 | cli.error(`Error: docker push exited with ${ err }`)
|
93 | cli.hush(err.stack || err)
|
94 | process.exit(1)
|
95 | }
|
96 | }
|