How to pass environment variable created by template block

Hi @CamilaBetim :wave:

So the problem is that environment variables are not available at the time artifact (and other variables) are interpolated. Environment variables are only set inside the task, while artifact are retrieved by the client agent.

What you could do is maybe use a prestart task to manually clone the repo into the alloc dir? If you have git installed you can use the exec driver directly, or a lightweight Docker image that has the git command.

Here’s an example:

job "private-git-clone" {
  datacenters = ["dc1"]

  group "repo" {
    task "clone" {
      driver = "exec"

      lifecycle {
        hook = "prestart"
      }

      config {
        command = "git"
        args    = ["clone", "https://${USER}:${SENHA}@bitbucket.org/${USER}/nomad.git", "${NOMAD_ALLOC_DIR}/repo"]
      }

      template {
        data        = <<EOF
USER="{{ ... }}"
SENHA="{{ ... }}"
EOF
        destination = "${NOMAD_SECREST_DIR}/env"
        env         = true
      }
    }

    task "repo" {
      driver = "docker"

      config {
        image   = "ubuntu:20.04"
        command = "/bin/bash"
        args    = ["local/script.sh"]
        
        # Mount a path in the allocation directory into the Docker container.
        mount {
          type   = "bind"
          source = "..${NOMAD_ALLOC_DIR}/repo"
          target = "/repo"
        }
      }

      template {
        data        = <<EOF
cat /repo/README.md
while true
do
  sleep 10
done
EOF
        destination = "local/script.sh"
      }
    }
  }
}

See if this works for you, and let us know how it goes :slightly_smiling_face: