Merge pull request #937 from drudge/fix-custom-registry

fix(docker): fix for custom registry login
This commit is contained in:
Mauricio Siu
2024-12-19 02:14:34 -06:00
committed by GitHub
3 changed files with 22 additions and 17 deletions

View File

@@ -211,21 +211,21 @@ const getImageName = (application: ApplicationNested) => {
} }
if (registry) { if (registry) {
return join(registry.imagePrefix || "", appName); return join(registry.registryUrl, registry.imagePrefix || "", appName);
} }
return `${appName}:latest`; return `${appName}:latest`;
}; };
const getAuthConfig = (application: ApplicationNested) => { const getAuthConfig = (application: ApplicationNested) => {
const { registry, username, password, sourceType } = application; const { registry, username, password, sourceType, registryUrl } = application;
if (sourceType === "docker") { if (sourceType === "docker") {
if (username && password) { if (username && password) {
return { return {
password, password,
username, username,
serveraddress: "https://index.docker.io/v1/", serveraddress: registryUrl || "",
}; };
} }
} else if (registry) { } else if (registry) {

View File

@@ -1,5 +1,5 @@
import type { WriteStream } from "node:fs"; import type { WriteStream } from "node:fs";
import { join } from "node:path"; import path, { join } from "node:path";
import type { ApplicationNested } from "../builders"; import type { ApplicationNested } from "../builders";
import { spawnAsync } from "../process/spawnAsync"; import { spawnAsync } from "../process/spawnAsync";
@@ -13,27 +13,32 @@ export const uploadImage = async (
throw new Error("Registry not found"); throw new Error("Registry not found");
} }
const { registryUrl, imagePrefix, registryType } = registry; const { registryUrl, imagePrefix } = registry;
const { appName } = application; const { appName } = application;
const imageName = `${appName}:latest`; const imageName = `${appName}:latest`;
const finalURL = registryUrl; const finalURL = registryUrl;
const registryTag = join(imagePrefix || "", imageName); const registryTag = path
.join(registryUrl, join(imagePrefix || "", imageName))
.replace(/\/+/g, "/");
try { try {
writeStream.write( writeStream.write(
`📦 [Enabled Registry] Uploading image to ${registry.registryType} | ${registryTag} | ${finalURL}\n`, `📦 [Enabled Registry] Uploading image to ${registry.registryType} | ${imageName} | ${finalURL}\n`,
); );
await spawnAsync( const loginCommand = spawnAsync(
"docker", "docker",
["login", finalURL, "-u", registry.username, "-p", registry.password], ["login", finalURL, "-u", registry.username, "--password-stdin"],
(data) => { (data) => {
if (writeStream.writable) { if (writeStream.writable) {
writeStream.write(data); writeStream.write(data);
} }
}, },
); );
loginCommand.child?.stdin?.write(registry.password);
loginCommand.child?.stdin?.end();
await loginCommand;
await spawnAsync("docker", ["tag", imageName, registryTag], (data) => { await spawnAsync("docker", ["tag", imageName, registryTag], (data) => {
if (writeStream.writable) { if (writeStream.writable) {
@@ -68,22 +73,23 @@ export const uploadImageRemoteCommand = (
const finalURL = registryUrl; const finalURL = registryUrl;
const registryTag = join(imagePrefix || "", imageName); const registryTag = path
.join(registryUrl, join(imagePrefix || "", imageName))
.replace(/\/+/g, "/");
try { try {
const command = ` const command = `
echo "📦 [Enabled Registry] Uploading image to '${registry.registryType}' | '${registryTag}'" >> ${logPath}; echo "📦 [Enabled Registry] Uploading image to '${registry.registryType}' | '${registryTag}'" >> ${logPath};
docker login ${finalURL} -u ${registry.username} -p ${registry.password} >> ${logPath} 2>> ${logPath} || { echo "${registry.password}" | docker login ${finalURL} -u ${registry.username} --password-stdin >> ${logPath} 2>> ${logPath} || {
echo "❌ DockerHub Failed" >> ${logPath}; echo "❌ DockerHub Failed" >> ${logPath};
exit 1; exit 1;
} }
echo "✅ DockerHub Login Success" >> ${logPath}; echo "✅ Registry Login Success" >> ${logPath};
docker tag ${imageName} ${registryTag} >> ${logPath} 2>> ${logPath} || { docker tag ${imageName} ${registryTag} >> ${logPath} 2>> ${logPath} || {
echo "❌ Error tagging image" >> ${logPath}; echo "❌ Error tagging image" >> ${logPath};
exit 1; exit 1;
} }
echo "✅ Image Tagged" >> ${logPath}; echo "✅ Image Tagged" >> ${logPath};
docker push ${registryTag} 2>> ${logPath} || { docker push ${registryTag} 2>> ${logPath} || {
echo "❌ Error pushing image" >> ${logPath}; echo "❌ Error pushing image" >> ${logPath};
exit 1; exit 1;
@@ -92,7 +98,6 @@ export const uploadImageRemoteCommand = (
`; `;
return command; return command;
} catch (error) { } catch (error) {
console.log(error);
throw error; throw error;
} }
}; };

View File

@@ -53,7 +53,7 @@ export const buildRemoteDocker = async (
application: ApplicationNested, application: ApplicationNested,
logPath: string, logPath: string,
) => { ) => {
const { sourceType, dockerImage, username, password } = application; const { registryUrl, dockerImage, username, password } = application;
try { try {
if (!dockerImage) { if (!dockerImage) {
@@ -65,7 +65,7 @@ echo "Pulling ${dockerImage}" >> ${logPath};
if (username && password) { if (username && password) {
command += ` command += `
if ! docker login --username ${username} --password ${password} https://index.docker.io/v1/ >> ${logPath} 2>&1; then if ! echo "${password}" | docker login --username "${username}" --password-stdin "${registryUrl || ""}" >> ${logPath} 2>&1; then
echo "❌ Login failed" >> ${logPath}; echo "❌ Login failed" >> ${logPath};
exit 1; exit 1;
fi fi