Setting the git rev-parse HEAD value as an environ...
# 🌱|help-and-getting-started
l
I've got 3 distinct repositories that make up my project as a whole. I want to have the ability to display the git hash on the dashboards for each of the 3 deployments, by running
git rev-parse HEAD
or equivalent and passing that on to the image being deployed. I'm not seeing a crystal clear path on doing that in just the
garden.yml
files, and wondering if anybody has a method of achieving that you wouldn't mind sharing.
b
@little-army-47606 You can use the template variable
${git.commitHash}
https://docs.garden.io/reference/template-strings/environments#usd-git.commithash Is this what you were looking for?
l
I tried setting an environment variable straight from this template variable and it doesn’t appear to work.
We've got one
project.garden.yml
file that builds out the required cluster specifications, and then 3 linked-repos that each have their own
garden.yml
file for Build and Deploy , specific to the requirements of each deployment's stack. We run the deploy action from the overall product directory, with each of the linked repos nested in
./services/project-name/
, each with their own individual commit hashes. It's those 3 commit hashes that I want to have environment variables for, each on their individual pods. So, for example,
api
has its own commit hash,
frontend
has its own commit hash, and
dashboard
has its own...
b
Hi @little-army-47606 The
git.commitHash
field is actually intentionally set to show the commit hash of the "parent" repo and changing that would be a breaking change. That being said, there is a workaround but it's a little hacky. But you can create an
exec
action (i.e. a simple action that just runs on the host) that gets the commit hash and then reference the output of that action. For explanation purposes, here's how you can create an exec action that gets the hash and another one that references it:
Copy code
kind: Run
type: exec
name: get-hash
spec:
  command: [git, rev-parse, HEAD]

---
kind: Run
type: exec
name: print-hash
dependencies: [run.get-hash]
spec:
  command: [echo, "hash is: ${actions.run.get-hash.outputs.log}"]
Now if you run
garden run print-hash
you should see the correct hash. For your specific use case it would looke something like:
Copy code
# In the API repo
kind: Run
type: exec
name: get-api-hash
spec:
  command: [git, rev-parse, HEAD]

---
kind: Deploy
type: container
name: api
dependencies: [run.get-api-hash]
spec:
  # ...
  env:
    COMMIT_HASH: ${actions.run.get-api-hash.outputs.log}
Note that each run action needs to have a unique name (i.e.
get-api-hash
,
get-dashboard-hash
etc) to prevent naming collision. Note also that this only works with remote sources, not remote actions (see [distinction here](https://docs.garden.io/advanced/using-remote-sources))
..it ain't always pretty but most things can be worked around using
exec
actions
l
Thanks so much for this! I’ll try it today.
I get the error
Copy code
key "env" is not allowed at path spec[env]. Available keys: files, kustomize, manifests, patchResources, namespace, portForwards, timeout, waitForJobs, defaultTarget, sync, localMode)
my
Deploy
types are all
kubernetes
, though, not
container
@brief-restaurant-63679 , I'm not sure if deploy type is an important distinction here.
b
Ah yes my bad. I thought you were using the
container
action. For the
kubernetes
action there's no env field and instead the environment varialbles are typically set in the manifest themselves but can be then overwritten via the
patchResources
field in the Garden spec. Here's an example of that: https://github.com/garden-io/garden/tree/main/examples/k8s-deploy-patch-resources
You can also specify the manifests "inline" in the Garden config but we typically recommend having dedicated manifest files so that they're portable and re-usable.
4 Views