AkhileshNS / heroku-deploy

A simple github action that dynamically deploys an app to heroku
MIT License
972 stars 254 forks source link

Support passing JSON strings as Docker build arguments #177

Open jasonraimondi opened 4 months ago

jasonraimondi commented 4 months ago

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

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;
  }
}