Generate random variable value on the flight
# 🌱|help-and-getting-started
b
Hey y'all so I'm running some unit tests with Garden but at the moment I have a fixed namespace per service e.g ci-${PR_NUMBER}-${service_name} It is working pretty good, however I noticed an edge-case were the developer puts multiple commits at the same time, the CI will run builds in parallel, this breaks Garden as the namespace is the same, it will try to re-run against the previously existing namespace; Is there someway to dynamically add a string in Garden that I could use to randomly generate the namespace name?
Copy code
openssl rand -base64 12
gives me a random string that I could use
c
Hey @bright-policeman-43626 have you looked at the Template Helper Functions? https://docs.garden.io/reference/template-strings/functions I think you could use either the
uuidv4()
function, or perhaps
sha256()
the
PR_NUMBER
and
slice()
it? Let me know if that works.
b
Exactly what I needed, thanks!
Will this change everytime I run it?
I need to do 2 commands: garden run task tests # this should generate the ID garden delete environment # use the same ID
c
Well, the uuidv4 yes, it will change at every invocation. If you sha256 the PR_NUMBER it will stay the same as soon as the PR_NUMBER doesn't change
You could alternatively generate a value outside of the commands, and pass it through the
--var foo=bar
option, see https://docs.garden.io/using-garden/variables-and-templating#variable-files-varfiles.
b
Oh, man. You are a saint, I think the one that will help me a bit more is the PR_NUMBER sha256 one
well, actually I think I need to generate one PER WORKFLOW so (deploy) and delete So probably the best solution is to generate it as part of my CI script, and then pass that to the job so a single job will have a unique ID, this will allow me to do both delete + create
c
Awesome! Happy I could help! 🙂