Hello, is it possible to get `cwd` passed to the `...
# getting-started
g
Hello, is it possible to get
cwd
passed to the
pulumi up --cwd
somehow in the code? (not automation API)
i
Copy code
pulumi -C "$(dirname $0)" $@
?
Copy code
$ pulumi --help |grep cw
  -C, --cwd string                   Run pulumi as if it had been started in another directory
g
but how do you resolve this path in pulumi program? from the code
i
Copy code
Mac-Studio:tmp andiolsi$ cat foo.js
console.log(__dirname);
Mac-Studio:tmp andiolsi$ node foo.js
/Users/andiolsi/tmp
has nothing to do with pulumi and everything with the programming language i guess
you could also expose it as an env var and then just read it
Copy code
import * as pulumi from "@pulumi/pulumi";

const config = new pulumi.Config();
const cwd = process.env.CWD || config.require("CWD");

export { cwd };
Copy code
export CWD="moo"
pulumi -C "${CWD}" $@
I could come up with some more esotheric ones 😉
g
The problem is that
__dirname
resolves to absolute path and I have a local command, where
scriptPath= __dirname+"something.ts"
Copy code
const cmd = new command.local.Command(
      `${name}`,
      {
        environment: {
          AWS_ACCOUNT_ID: pulumi.interpolate`${args.accountId}`,
        },
        create: `date && node --require ts-node/register ${scriptPath}`,
        triggers: [md5],
      },
      { ...opts, protect: false }
    );
the problem is that
${scriptPath}
is different in every system and if I use relative path, it has to be relative to the pulumi execution, rendering the function portability useless
i
uuuh, i wonder if environment vars are just “passed” through
worst case: just pass it via env var to pulumi like i suggested, it will work just fine. i pass credentials to it like that
g
I still do not see how passing env var to to pulumi process would not change the state. the point is that the path to the script must be resolvable from anywhere without modifying the state and therefore causing an update of the local command
w
I had this same issue previously when using
CopyFile
but never came up with a good solution, though admittedly I didn't try very hard as we setup a cicd server to handle deployments and only did manual deployments in emergencies. For
Command
, I bypassed the problem by reading the content of the file and supplying that as the script explicitly
i
Copy code
import { local } from "@pulumi/command";
const testFoo = new local.Command("testFoo", {
  create: "echo $TEST_FOO >> $HOME/testFoo.txt",
});

export const output = testFoo.stdout;
Copy code
Mac-Studio:~ andiolsi$ TEST_FOO=käse ~/work/olsicloud4/pulumi/gitlab-olsitrack2/run.sh up
Previewing update (gitlab-olsitrack2):
     Type                      Name                      Plan
     pulumi:pulumi:Stack       gitlab-gitlab-olsitrack2
 +   └─ command:local:Command  testFoo                   create

Outputs:
  + output     : output<string>

Resources:
    + 1 to create
    42 unchanged

Do you want to perform this update? yes
Updating (gitlab-olsitrack2):
     Type                      Name                      Status
     pulumi:pulumi:Stack       gitlab-gitlab-olsitrack2
 +   └─ command:local:Command  testFoo                   created (0.01s)
...
Resources:
    + 1 created
    42 unchanged
Copy code
Mac-Studio:~ andiolsi$ TEST_FOO=wurst ~/work/olsicloud4/pulumi/gitlab-olsitrack2/run.sh up
Previewing update (gitlab-olsitrack2):
     Type                 Name                      Plan
     pulumi:pulumi:Stack  gitlab-gitlab-olsitrack2

Resources:
    43 unchanged

Do you want to perform this update? yes
Updating (gitlab-olsitrack2):
     Type                 Name                      Status
     pulumi:pulumi:Stack  gitlab-gitlab-olsitrack2
...
Resources:
    43 unchanged

Duration: 3s
Copy code
Mac-Studio:~ andiolsi$ cat testFoo.txt
wurst
käse
You can also ingore changes to the environemnt:
Copy code
const testFoo = new local.Command(
  "testFoo",
  {
    environment: {
      TEST_FOO: "danny",
    },
    create: "echo $TEST_FOO >> $HOME/testFoo.txt",
  },
  { ignoreChanges: ["environment"] },
);
If you are only passing one var in `environment`that may be feasible. If you have more and you don’t want to ignore thats not an option. But you can just take those as part of your template literal sting there.
example:
Copy code
const cmd = new command.local.Command(
      `${name}`,
      {
        environment: {
          SCRIPT_PATH: someVarThatContaintsTheScriptPath,
        },
        create: pulumi.interpolate`date && AWS_ACCOUNT_ID="${args.accountId}" node --require ts-node/register "$\{SCRIPT_PATH\}"`,
        triggers: [md5],
      },
      { ...opts, protect: false, ignoreChanges: ["environment"] }
    );
But as long as you are using
local
all env vars passed to pulumi are available to all Commands anyway, so you can even skip that whole variable thing for it altogehter.