Skip to content

Commit

Permalink
feat(container-registry): ported heroku/heroku-container-registry#75
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Jun 19, 2018
1 parent 1bdc4af commit 13f2616
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
10 changes: 8 additions & 2 deletions packages/container-registry-v5/commands/push.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ module.exports = function (topic) {
`${cli.color.cmd('heroku container:push worker')} # Pushes Dockerfile to worker process type`,
`${cli.color.cmd('heroku container:push web worker --recursive')} # Pushes Dockerfile.web and Dockerfile.worker`,
`${cli.color.cmd('heroku container:push --recursive')} # Pushes Dockerfile.*`,
`${cli.color.cmd('heroku container:push web --arg ENV=live,HTTPS=on')} # Build-time variables`
`${cli.color.cmd('heroku container:push web --arg ENV=live,HTTPS=on')} # Build-time variables`,
`${cli.color.cmd('heroku container:push --recursive --context-path .')} # Pushes Dockerfile.* using current dir as build context`
],
flags: [
{
Expand All @@ -33,6 +34,11 @@ module.exports = function (topic) {
name: 'arg',
hasValue: true,
description: 'set build-time variables'
},
{
name: 'context-path',
hasValue: true,
description: 'path to use as build context (defaults to Dockerfile dir)'
}
],
run: cli.command(push)
Expand Down Expand Up @@ -82,7 +88,7 @@ let push = async function (context, heroku) {
} else {
cli.styledHeader(`Building ${job.name} (${job.dockerfile})`)
}
await Sanbashi.buildImage(job.dockerfile, job.resource, buildArg)
await Sanbashi.buildImage(job.dockerfile, job.resource, buildArg, context.flags['context-path'])
}
} catch (err) {
cli.error(`Error: docker build exited with ${err}`, 1)
Expand Down
4 changes: 2 additions & 2 deletions packages/container-registry-v5/lib/sanbashi.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ Sanbashi.filterByProcessType = function (jobs, procs) {
return filteredJobs
}

Sanbashi.buildImage = function (dockerfile, resource, buildArg) {
let cwd = Path.dirname(dockerfile)
Sanbashi.buildImage = function (dockerfile, resource, buildArg, path) {
let cwd = path || Path.dirname(dockerfile)
let args = ['build', '-f', dockerfile, '-t', resource]

for (let i = 0; i < buildArg.length; i++) {
Expand Down
22 changes: 22 additions & 0 deletions packages/container-registry-v5/test/commands/push.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,28 @@ describe('container push', () => {
.then(() => api.done())
})

it('builds with custom context path and pushes to the docker registry', () => {
let api = nock('https://api.heroku.com:443')
.get('/apps/testapp')
.reply(200, {name: 'testapp'})

let dockerfiles = sandbox.stub(Sanbashi, 'getDockerfiles')
.returns(['/path/to/Dockerfile'])
let build = sandbox.stub(Sanbashi, 'buildImage')
.withArgs('/path/to/Dockerfile', 'registry.heroku.com/testapp/web', [], '/custom/context/path')
let push = sandbox.stub(Sanbashi, 'pushImage')
.withArgs('registry.heroku.com/testapp/web')

return cmd.run({app: 'testapp', args: ['web'], flags: {'context-path': '/custom/context/path'}})
// .then(() => expect(cli.stderr, 'to be empty'))
.then(() => expect(cli.stdout, 'to contain', 'Building web (/path/to/Dockerfile)'))
.then(() => expect(cli.stdout, 'to contain', 'Pushing web (/path/to/Dockerfile)'))
.then(() => sandbox.assert.calledOnce(dockerfiles))
.then(() => sandbox.assert.calledOnce(build))
.then(() => sandbox.assert.calledOnce(push))
.then(() => api.done())
})

it('does not find an image to push', () => {
let api = nock('https://api.heroku.com:443')
.get('/apps/testapp')
Expand Down
9 changes: 9 additions & 0 deletions packages/container-registry-v5/test/sanbashi.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,15 @@ describe('Sanbashi', () => {
Sinon.assert.calledWith(cmd, 'docker', dockerArg)
})

it('set build path', () => {
let buildArg = ['']
let buildPath = 'build/context'
let cmd = Sinon.stub(Sanbashi, 'cmd')
Sanbashi.buildImage(dockerfile, resource, buildArg, buildPath)
let dockerArg = ['build', '-f', dockerfile, '-t', 'web', buildPath]
Sinon.assert.calledWith(cmd, 'docker', dockerArg)
})

afterEach(() => {
Sanbashi.cmd.restore() // Unwraps the spy
})
Expand Down

0 comments on commit 13f2616

Please sign in to comment.