Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support passing JSON strings as Docker build arguments #177

Open
jasonraimondi opened this issue Apr 29, 2024 · 0 comments
Open

Support passing JSON strings as Docker build arguments #177

jasonraimondi opened this issue Apr 29, 2024 · 0 comments

Comments

@jasonraimondi
Copy link

Issue

Trying to use valid json string as environment variables/docker build args does not work as expected

Description

The current code for passing Docker build arguments incorrectly handles JSON string values by enclosing them in double quotes, resulting in improperly formatted arguments.

To address this, we need to modify the code to handle JSON strings separately.

Proposed solution

  • For non-JSON strings, enclose in double quotes as before.
  • For valid JSON strings, parse with JSON.parse(), stringify with JSON.stringify(), and enclose in single quotes.
if (heroku.dockerBuildArgs) {
  heroku.dockerBuildArgs = heroku.dockerBuildArgs
    .split("\\n")
    .map((arg) => {
      const argValue = process.env[arg];
      if (argValue && isValidJSON(argValue)) {
        return `${arg}='${JSON.stringify(JSON.parse(argValue))}'`;
      } else {
        return `${arg}="${argValue}"`;
      }
    })
    .join(",");

  heroku.dockerBuildArgs = heroku.dockerBuildArgs
    ? `--build-arg ${heroku.dockerBuildArgs}`
    : "";
}

function isValidJSON(str) {
  try {
    JSON.parse(str);
    return true;
  } catch (e) {
    return false;
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant