https://pulumi.com logo
Title
r

rapid-raincoat-36492

09/30/2021, 1:45 PM
How would I run a bash command like
git rev-parse --abbrev-ref HEAD
or a webpack command in a pulumi file? Is there a way to run arbitrary shell commands synchronously?
c

crooked-pillow-11944

09/30/2021, 2:06 PM
You'd probably need to use Automation API if you need to inject some external inputs during a stack build. https://www.pulumi.com/docs/guides/automation-api/
r

rapid-raincoat-36492

09/30/2021, 2:17 PM
Hmm, I don't want to do that in this case and lose out on the CLI tools. I was hoping to convert over some terraform/serverless files to pulumi, but might wait until running shell commands is better supported. I appreciate your help
c

crooked-pillow-11944

09/30/2021, 2:26 PM
You could just wrap it in a shell script
p

prehistoric-activity-61023

09/30/2021, 3:21 PM
I’ve never done it but I guess you should be able to run shell commands in pulumi script without any issues. Not sure if it’s a recommended approach though.
c

crooked-pillow-11944

09/30/2021, 3:26 PM
More like:
#!/usr/bin/env bash

git rev-parse --abbrev-ref HEAD
pulumi config ...
pulumi up
ls -altr
p

prehistoric-activity-61023

09/30/2021, 3:28 PM
that seems like a better option - modify the config before running
pulumi up
so the change is actually reflected in the source code and/or in config values
another option is to directly invoke the command in pulumi code itself like:
import pulumi_gcp as gcp
import subprocess

shell_result = subprocess.check_output(["uname", "-r"], text=True).strip()
bucket = gcp.storage.Bucket(
    "test",
    name=shell_result,
)
But I guess @crooked-pillow-11944 approach is better. The above will be non deterministic from my point of view. It can give different outputs every time it’s being run and it leaves no trace in source code/config values. Anyway, if this is something you used to do with terraform, here you go. I wouldn’t say pulumi has worse support for shell commands. It’s exactly the opposite 😄 You can do everything python/javascript/C#/go can do.
r

rapid-raincoat-36492

09/30/2021, 4:33 PM
I like the idea of passing in shell command output via config files, that should be a nice solution I think