add logic and option to reuse existing auth json, enabled by default

This commit is contained in:
David Hiendl 2023-12-28 20:28:57 +01:00
parent 758cf72aff
commit 7650773bc5
4 changed files with 62 additions and 2 deletions

View File

@ -67,8 +67,8 @@ inputs:
description: "" description: ""
default: "" default: ""
merge_registry_json: merge_existing_auth_json:
description: "" description: "if existing registry auth json in .docker/config.json should be merge into the final auth json"
default: "true" default: "true"
squash_layers: squash_layers:

30
dist/index.js vendored
View File

@ -62167,6 +62167,34 @@ function mergeArgRegistryAuthJson(registryAuthJson) {
} }
} }
function mergeExistingDockerAuthJson(registryAuthJson, targetFile) {
if (!core.getBooleanInput('merge_existing_auth_json')) {
return;
}
if (!external_fs_.existsSync(targetFile)) {
return;
}
try {
const existingJsonStr = external_fs_.readFileSync(targetFile, {encoding: 'utf-8'});
const existingJson = JSON.parse(existingJsonStr);
if (existingJson.auths != null && typeof existingJson === 'object') {
for (const key in existingJson.auths) {
if (existingJson.auths.hasOwnProperty(key)) {
registryAuthJson.auths[key] = existingJson.auths[key];
}
}
}
}
catch (e) {
console.log(`Failed to parse existing docker auth json in file: ${targetFile}"`);
core.setFailed(`Failed to parse existing docker auth json in file: ${targetFile}"` + e.message);
process.exit(1);
}
}
function writeRegistryAuthJson(registryAuthJson, targetFile) { function writeRegistryAuthJson(registryAuthJson, targetFile) {
external_fs_.mkdirSync(external_path_.dirname(targetFile), {recursive: true}); external_fs_.mkdirSync(external_path_.dirname(targetFile), {recursive: true});
const jsonContents = JSON.stringify(registryAuthJson, null, 2); const jsonContents = JSON.stringify(registryAuthJson, null, 2);
@ -62450,7 +62478,9 @@ try {
console.log('determined .docker/config.json location: ', dockerConfigFile); console.log('determined .docker/config.json location: ', dockerConfigFile);
} }
const registryAuthJson = {auths: {}}; const registryAuthJson = {auths: {}};
mergeExistingDockerAuthJson(registryAuthJson);
addCiRegistryAuth(ci_registry, registryAuthJson); addCiRegistryAuth(ci_registry, registryAuthJson);
mergeArgRegistryAuthJson(registryAuthJson); mergeArgRegistryAuthJson(registryAuthJson);
writeRegistryAuthJson(registryAuthJson, dockerConfigFile); writeRegistryAuthJson(registryAuthJson, dockerConfigFile);

View File

@ -8,6 +8,7 @@ import {
executeDockerBuild, executeDockerBuild,
isTrueString, isTrueString,
mergeArgRegistryAuthJson, mergeArgRegistryAuthJson,
mergeExistingDockerAuthJson,
prepareDestinations, prepareDestinations,
prepareDockerArgs, prepareDockerArgs,
processAdditionalRegistries, processAdditionalRegistries,
@ -42,6 +43,7 @@ try {
} }
const registryAuthJson = {auths: {}}; const registryAuthJson = {auths: {}};
mergeExistingDockerAuthJson(registryAuthJson);
addCiRegistryAuth(ci_registry, registryAuthJson); addCiRegistryAuth(ci_registry, registryAuthJson);
mergeArgRegistryAuthJson(registryAuthJson); mergeArgRegistryAuthJson(registryAuthJson);
writeRegistryAuthJson(registryAuthJson, dockerConfigFile); writeRegistryAuthJson(registryAuthJson, dockerConfigFile);

View File

@ -60,6 +60,34 @@ export function mergeArgRegistryAuthJson(registryAuthJson) {
} }
} }
export function mergeExistingDockerAuthJson(registryAuthJson, targetFile) {
if (!core.getBooleanInput('merge_existing_auth_json')) {
return;
}
if (!fs.existsSync(targetFile)) {
return;
}
try {
const existingJsonStr = fs.readFileSync(targetFile, {encoding: 'utf-8'});
const existingJson = JSON.parse(existingJsonStr);
if (existingJson.auths != null && typeof existingJson === 'object') {
for (const key in existingJson.auths) {
if (existingJson.auths.hasOwnProperty(key)) {
registryAuthJson.auths[key] = existingJson.auths[key];
}
}
}
}
catch (e) {
console.log(`Failed to parse existing docker auth json in file: ${targetFile}"`);
core.setFailed(`Failed to parse existing docker auth json in file: ${targetFile}"` + e.message);
process.exit(1);
}
}
export function writeRegistryAuthJson(registryAuthJson, targetFile) { export function writeRegistryAuthJson(registryAuthJson, targetFile) {
fs.mkdirSync(path.dirname(targetFile), {recursive: true}); fs.mkdirSync(path.dirname(targetFile), {recursive: true});
const jsonContents = JSON.stringify(registryAuthJson, null, 2); const jsonContents = JSON.stringify(registryAuthJson, null, 2);