Consul KV substitution in artifact stanza

Is it possible to use a consul KV as part of variable substitution in the artifact Stanza?

I’ve tried using {{ key “name” }} in the source field, as well as pairing with template Stanza and using an environment variable in the source field as ${NAME}, but neither work, with the job failing on the artifact download (the variable substitution isn’t happening.

My intended use case would be to use a version for my artifact configured in Consul KV, to which i could auto trigger a re-deployment and app restart (via template -> check_mode = restart). I guess the artifact Stanza is evaluated before the template Stanza.

One hacky solution is to use raw_exec and curl to download using env variable substitution:

task "app" {
  driver = "raw_exec"

  config {
    command = "/bin/bash"
    args = [
      "curl -s${APP_VERSION}/app-${APP_VERSION}-boot.jar -o local/app.jar && /usr/bin/java -Xms1g -Xmx1g -jar local/app.jar,consul-config"

Hi @chriswhite199! Nomad only uses Consul interpolation in the template stanza – it happens within the context of the allocation. So your workaround using env var interpolation should work. If you’re using consul kv for the name of an artifact another approach you might want to try is pulling the value from consul kv and interpolating it into the jobspec before submitting it.

@tgross, so the whole value would need to be in consul? Is there an example of this somewhere or are you saying you need to consul-template the whole jobspec file before submitting to nomad?

or are you saying you need to consul-template the whole jobspec file before submitting to nomad?

Right. I don’t have an open source example of this unfortunately but here’s a workflow for the sort of thing you might have in mind.

Suppose you’re building the Jars images in your CI/CD system, and the result is an app Jar tagged with the SHA of the source repo. ex. abc123d. That app version gets written to Consul at myapp/app_version. You want the CI/CD system to deploy a Nomad job using that Jar.

The jobspec has a consul-template template string embedded in the artifact stanza:

artifact {
   source = "{{ key \"myapp/app_version\" }}/app-{{ key \"myapp/app_version\" }}-boot.jar"

The CI/CD system takes the Nomad jobspec as a template and does a first-pass consul-template interpolation on it to replace {{ key "myapp/app_version" }} with the value from Consul. You then nomad job run the interpolated jobspec.

Note that the only reason you’d need consul-template in this workflow at all is if you want to store the app version in Consul. If you were doing a continuous deployment from a CI/CD system, you’d probably have the app version in-memory already and could do something as simple as interpolation with sed.