AkhileshNS / heroku-deploy

A simple github action that dynamically deploys an app to heroku

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support passing JSON strings as Docker build arguments

jasonraimondi opened this issue · comments

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