From 6c5883876a129b74155f90b9665933deef56263b Mon Sep 17 00:00:00 2001 From: David Hiendl Date: Wed, 6 Dec 2023 12:15:14 +0100 Subject: [PATCH] fix registry auth string being suffixed with "/owner/repo:", dynamically determine .docker/config.json, renamed targetRegistries to targetRepos to better clarify content (list of "registry/owner/repo:prefix-" and not actual registries) --- action.yml | 4 ++++ dist/index.js | 37 ++++++++++++++++++++++++++++++------- src/action.js | 21 ++++++++++++++------- src/lib.js | 15 +++++++++++++++ 4 files changed, 63 insertions(+), 14 deletions(-) diff --git a/action.yml b/action.yml index 09e5526..d719d21 100644 --- a/action.yml +++ b/action.yml @@ -6,6 +6,10 @@ inputs: description: docker client image to use for building images default: "docker:latest" + docker_auth_json_file: + description: location of the docker auth json file, relative to home dir or absolute path + default: ".docker/config.json" + docker_args: description: "Extra arguments to pass to docker invocation" default: "" diff --git a/dist/index.js b/dist/index.js index dccc722..7eecdde 100644 --- a/dist/index.js +++ b/dist/index.js @@ -62097,6 +62097,8 @@ const gBase64 = { // and finally, +// EXTERNAL MODULE: external "os" +var external_os_ = __nccwpck_require__(2037); // EXTERNAL MODULE: external "path" var external_path_ = __nccwpck_require__(1017); ;// CONCATENATED MODULE: ./src/lib.js @@ -62107,6 +62109,7 @@ var external_path_ = __nccwpck_require__(1017); + function processAdditionalRegistries(targetRegistries) { const additionalRegistries = core.getInput('additional_registries'); if (additionalRegistries != null && additionalRegistries.length > 0) { @@ -62359,6 +62362,20 @@ function executeDockerBuild(dockerArgs, destinations) { } } +function determineDockerConfigFileLocation(path) { + if (path == null || !path.length) { + return external_os_.homedir + '/.docker/config.json'; + } + + // absolute path + if (path.startsWith('/')) { + return path; + } + + // relative path to home dir + return external_os_.homedir + '/' + path; +} + function lib_isTrueString(str) { return str === '1' || str === 'true' @@ -62382,27 +62399,33 @@ try { const debug = lib_isTrueString(process.env['ACTIONS_STEP_DEBUG']); - let targetRegistries = []; - const repoStr = github.context.repo.owner + '/' + github.context.repo.repo; + let targetRepos = []; + const repoStr = github.context.repo.owner + '/' + github.context.repo.repo; let ci_registry = false; if (core.getBooleanInput('add_ci_registry_target')) { - ci_registry = information.ci_hostname + '/' + repoStr + ':'; - targetRegistries.push(ci_registry); + const ci_registry_repo = information.ci_hostname + '/' + repoStr + ':'; + targetRepos.push(ci_registry_repo); + } + + processAdditionalRegistries(targetRepos); + + const dockerConfigFile = determineDockerConfigFileLocation(core.getInput('docker_auth_json_file')); + if (debug) { + console.log('determined .docker/config.json location: ', dockerConfigFile); } - processAdditionalRegistries(targetRegistries); const registryAuthJson = {auths: {}}; addCiRegistryAuth(ci_registry, registryAuthJson); mergeArgRegistryAuthJson(registryAuthJson); - writeRegistryAuthJson(registryAuthJson, '/home/runner/.docker/config.json'); + writeRegistryAuthJson(registryAuthJson, dockerConfigFile); const tags = collectTags(information); if (debug) { console.log('tags:', JSON.stringify(tags, null, 2)); } - const destinations = prepareDestinations(targetRegistries, tags); + const destinations = prepareDestinations(targetRepos, tags); if (debug || core.getBooleanInput('debug_log_destinations')) { console.log('destinations:', JSON.stringify(destinations, null, 2)); } diff --git a/src/action.js b/src/action.js index aa9ee10..8ebd69a 100644 --- a/src/action.js +++ b/src/action.js @@ -4,6 +4,7 @@ import * as action_information from 'information'; import { addCiRegistryAuth, collectTags, + determineDockerConfigFileLocation, executeDockerBuild, isTrueString, mergeArgRegistryAuthJson, @@ -23,27 +24,33 @@ try { const debug = isTrueString(process.env['ACTIONS_STEP_DEBUG']); - let targetRegistries = []; - const repoStr = github.context.repo.owner + '/' + github.context.repo.repo; + let targetRepos = []; + const repoStr = github.context.repo.owner + '/' + github.context.repo.repo; let ci_registry = false; if (core.getBooleanInput('add_ci_registry_target')) { - ci_registry = information.ci_hostname + '/' + repoStr + ':'; - targetRegistries.push(ci_registry); + const ci_registry_repo = information.ci_hostname + '/' + repoStr + ':'; + targetRepos.push(ci_registry_repo); + } + + processAdditionalRegistries(targetRepos); + + const dockerConfigFile = determineDockerConfigFileLocation(core.getInput('docker_auth_json_file')); + if (debug) { + console.log('determined .docker/config.json location: ', dockerConfigFile); } - processAdditionalRegistries(targetRegistries); const registryAuthJson = {auths: {}}; addCiRegistryAuth(ci_registry, registryAuthJson); mergeArgRegistryAuthJson(registryAuthJson); - writeRegistryAuthJson(registryAuthJson, '/home/runner/.docker/config.json'); + writeRegistryAuthJson(registryAuthJson, dockerConfigFile); const tags = collectTags(information); if (debug) { console.log('tags:', JSON.stringify(tags, null, 2)); } - const destinations = prepareDestinations(targetRegistries, tags); + const destinations = prepareDestinations(targetRepos, tags); if (debug || core.getBooleanInput('debug_log_destinations')) { console.log('destinations:', JSON.stringify(destinations, null, 2)); } diff --git a/src/lib.js b/src/lib.js index 9d61c41..06c2474 100644 --- a/src/lib.js +++ b/src/lib.js @@ -3,6 +3,7 @@ import * as github from '@actions/github'; import * as child_process from 'child_process'; import * as fs from 'fs'; import {Base64} from 'js-base64'; +import * as os from 'os'; import * as path from 'path'; export function processAdditionalRegistries(targetRegistries) { @@ -257,6 +258,20 @@ export function executeDockerBuild(dockerArgs, destinations) { } } +export function determineDockerConfigFileLocation(path) { + if (path == null || !path.length) { + return os.homedir + '/.docker/config.json'; + } + + // absolute path + if (path.startsWith('/')) { + return path; + } + + // relative path to home dir + return os.homedir + '/' + path; +} + export function isTrueString(str) { return str === '1' || str === 'true'