diff --git a/action.yml b/action.yml index cc7f1cd..e31ef60 100644 --- a/action.yml +++ b/action.yml @@ -67,8 +67,8 @@ inputs: description: "" default: "" - merge_registry_json: - description: "" + merge_existing_auth_json: + description: "if existing registry auth json in .docker/config.json should be merge into the final auth json" default: "true" squash_layers: diff --git a/dist/index.js b/dist/index.js index 152dc54..86361ff 100644 --- a/dist/index.js +++ b/dist/index.js @@ -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) { external_fs_.mkdirSync(external_path_.dirname(targetFile), {recursive: true}); const jsonContents = JSON.stringify(registryAuthJson, null, 2); @@ -62450,7 +62478,9 @@ try { console.log('determined .docker/config.json location: ', dockerConfigFile); } + const registryAuthJson = {auths: {}}; + mergeExistingDockerAuthJson(registryAuthJson); addCiRegistryAuth(ci_registry, registryAuthJson); mergeArgRegistryAuthJson(registryAuthJson); writeRegistryAuthJson(registryAuthJson, dockerConfigFile); diff --git a/src/action.js b/src/action.js index 5f73805..d47a2ae 100644 --- a/src/action.js +++ b/src/action.js @@ -8,6 +8,7 @@ import { executeDockerBuild, isTrueString, mergeArgRegistryAuthJson, + mergeExistingDockerAuthJson, prepareDestinations, prepareDockerArgs, processAdditionalRegistries, @@ -42,6 +43,7 @@ try { } const registryAuthJson = {auths: {}}; + mergeExistingDockerAuthJson(registryAuthJson); addCiRegistryAuth(ci_registry, registryAuthJson); mergeArgRegistryAuthJson(registryAuthJson); writeRegistryAuthJson(registryAuthJson, dockerConfigFile); diff --git a/src/lib.js b/src/lib.js index e5483a9..08df9b2 100644 --- a/src/lib.js +++ b/src/lib.js @@ -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) { fs.mkdirSync(path.dirname(targetFile), {recursive: true}); const jsonContents = JSON.stringify(registryAuthJson, null, 2);