Skip to content

Commit

Permalink
fix(publish): fix push docker images
Browse files Browse the repository at this point in the history
  • Loading branch information
lgaticaq committed Feb 4, 2020
1 parent 3e8a92e commit 704712c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
13 changes: 12 additions & 1 deletion src/getAuth.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,38 @@ const getError = require('./get-error')
* @param {string} registry -
* @param {string} imageName -
* @param {Context} ctx -
* @returns {void} -
* @returns {Registry} -
* @example
* verifyConditions(pluginConfig, ctx)
*/
const getAuth = (user, password, registry, imageName, ctx) => {
const errors = []
/** @type {import('./types').Registry} */
const auth = {}
if (!user || !ctx.env[user]) {
errors.push(getError('ENODOCKERUSER', ctx))
} else {
auth.user = ctx.env[user]
}
if (!password || !ctx.env[password]) {
errors.push(getError('ENODOCKERPASSWORD', ctx))
} else {
auth.password = ctx.env[password]
}
if (!registry) {
errors.push(getError('ENODOCKERREGISTRY', ctx))
} else {
auth.url = registry
}
if (!imageName) {
errors.push(getError('ENOIMAGENAME', ctx))
} else {
auth.imageName = imageName
}
if (errors.length > 0) {
throw new AggregateError(errors)
}
return auth
}

module.exports = getAuth
Expand Down
29 changes: 26 additions & 3 deletions src/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const AggregateError = require('aggregate-error')
const Dockerode = require('dockerode')

const getError = require('./get-error')
const getAuth = require('./getAuth')

/** @typedef {import('stream').Readable} ReadableStream */
/**
Expand All @@ -12,8 +13,23 @@ const getError = require('./get-error')
*/
const pushImage = response => {
return new Promise((resolve, reject) => {
response.on('end', () => resolve())
response.on('error', error => reject(error))
let error
response.on('data', chunk => {
const data = JSON.parse(chunk.toString())
if (data.error) {
error = new Error(data.error)
}
})
response.on('end', () => {
if (error) {
reject(error)
} else {
resolve()
}
})
response.on('error', error => {
reject(error)
})
})
}

Expand All @@ -35,7 +51,14 @@ module.exports = async (pluginConfig, ctx) => {
if (pluginConfig.additionalTags && pluginConfig.additionalTags.length > 0) {
tags.push(...pluginConfig.additionalTags)
}
for (const { user, password, url, imageName } of pluginConfig.registries) {
for (const registry of pluginConfig.registries) {
const { user, password, url, imageName } = getAuth(
registry.user,
registry.password,
registry.url,
registry.imageName,
ctx
)
const image = docker.getImage(imageName)
const options = {
password: password,
Expand Down

0 comments on commit 704712c

Please sign in to comment.