diff --git a/CHANGELOG.md b/CHANGELOG.md index 72b40f3d7c8..1d783cd3172 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ IMPROVEMENTS: * client: Enforce shared allocation directory disk usage [GH-1580] * client: Introduce a `secrets/` directory to tasks where sensitive data can be written [GH-1681] + * driver: Export `NOMAD_JOB_NAME` environment variable [GH-1804] * driver/docker: Support Docker volumes [GH-1767] * driver/docker: Allow Docker logging to be configured [GH-1767] diff --git a/client/driver/driver.go b/client/driver/driver.go index ab31c5f8aeb..49a2cb89f12 100644 --- a/client/driver/driver.go +++ b/client/driver/driver.go @@ -142,6 +142,7 @@ func GetTaskEnv(allocDir *allocdir.AllocDir, node *structs.Node, SetTaskMeta(task.Meta). SetTaskGroupMeta(tg.Meta). SetJobMeta(alloc.Job.Meta). + SetJobName(alloc.Job.Name). SetEnvvars(task.Env). SetTaskName(task.Name) diff --git a/client/driver/driver_test.go b/client/driver/driver_test.go index d62d4afb47e..371860b64fd 100644 --- a/client/driver/driver_test.go +++ b/client/driver/driver_test.go @@ -153,6 +153,7 @@ func TestDriver_GetTaskEnv(t *testing.T) { "NOMAD_ALLOC_ID": alloc.ID, "NOMAD_ALLOC_NAME": alloc.Name, "NOMAD_TASK_NAME": task.Name, + "NOMAD_JOB_NAME": alloc.Job.Name, } act := env.EnvMap() diff --git a/client/driver/env/env.go b/client/driver/env/env.go index bd74e79fc25..2614a036a03 100644 --- a/client/driver/env/env.go +++ b/client/driver/env/env.go @@ -40,6 +40,9 @@ const ( // TaskName is the environment variable for passing the task name. TaskName = "NOMAD_TASK_NAME" + // JobName is the environment variable for passing the job name. + JobName = "NOMAD_JOB_NAME" + // AllocIndex is the environment variable for passing the allocation index. AllocIndex = "NOMAD_ALLOC_INDEX" @@ -98,6 +101,7 @@ type TaskEnvironment struct { PortMap map[string]int VaultToken string InjectVaultToken bool + JobName string // taskEnv is the variables that will be set in the tasks environment TaskEnv map[string]string @@ -188,6 +192,9 @@ func (t *TaskEnvironment) Build() *TaskEnvironment { if t.TaskName != "" { t.TaskEnv[TaskName] = t.TaskName } + if t.JobName != "" { + t.TaskEnv[JobName] = t.JobName + } // Build the node if t.Node != nil { @@ -452,11 +459,21 @@ func (t *TaskEnvironment) SetTaskName(name string) *TaskEnvironment { return t } +func (t *TaskEnvironment) SetJobName(name string) *TaskEnvironment { + t.JobName = name + return t +} + func (t *TaskEnvironment) ClearTaskName() *TaskEnvironment { t.TaskName = "" return t } +func (t *TaskEnvironment) ClearJobName() *TaskEnvironment { + t.JobName = "" + return t +} + func (t *TaskEnvironment) SetVaultToken(token string, inject bool) *TaskEnvironment { t.VaultToken = token t.InjectVaultToken = inject diff --git a/website/source/docs/jobspec/environment.html.md b/website/source/docs/jobspec/environment.html.md index 3b264d1df4b..537763e4cc2 100644 --- a/website/source/docs/jobspec/environment.html.md +++ b/website/source/docs/jobspec/environment.html.md @@ -52,6 +52,10 @@ environment variables.