Skip to content

Commit bafe927

Browse files
committed
Pass task group name as NOMAD_GROUP_NAME environment variable
1 parent 8ae8f61 commit bafe927

File tree

5 files changed

+19
-5
lines changed

5 files changed

+19
-5
lines changed

client/driver/driver_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ func setupTaskEnv(t *testing.T, driver string) (*allocdir.TaskDir, map[string]st
209209
"NOMAD_ALLOC_INDEX": "0",
210210
"NOMAD_ALLOC_NAME": alloc.Name,
211211
"NOMAD_TASK_NAME": task.Name,
212+
"NOMAD_GROUP_NAME": alloc.TaskGroup,
212213
"NOMAD_JOB_NAME": alloc.Job.Name,
213214
"NOMAD_DC": "dc1",
214215
"NOMAD_REGION": "global",

client/driver/env/env.go

+8
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ const (
4444
// TaskName is the environment variable for passing the task name.
4545
TaskName = "NOMAD_TASK_NAME"
4646

47+
// GroupName is the environment variable for passing the task group name.
48+
GroupName = "NOMAD_GROUP_NAME"
49+
4750
// JobName is the environment variable for passing the job name.
4851
JobName = "NOMAD_JOB_NAME"
4952

@@ -208,6 +211,7 @@ type Builder struct {
208211
region string
209212
allocId string
210213
allocName string
214+
groupName string
211215
vaultToken string
212216
injectVaultToken bool
213217
jobName string
@@ -277,6 +281,9 @@ func (b *Builder) Build() *TaskEnv {
277281
if b.allocName != "" {
278282
envMap[AllocName] = b.allocName
279283
}
284+
if b.groupName != "" {
285+
envMap[GroupName] = b.groupName
286+
}
280287
if b.allocIndex != -1 {
281288
envMap[AllocIndex] = strconv.Itoa(b.allocIndex)
282289
}
@@ -380,6 +387,7 @@ func (b *Builder) setTask(task *structs.Task) *Builder {
380387
func (b *Builder) setAlloc(alloc *structs.Allocation) *Builder {
381388
b.allocId = alloc.ID
382389
b.allocName = alloc.Name
390+
b.groupName = alloc.TaskGroup
383391
b.allocIndex = int(alloc.Index())
384392
b.jobName = alloc.Job.Name
385393

client/driver/env/env_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ func TestEnvironment_AsList(t *testing.T) {
181181
"NOMAD_HOST_PORT_http=80",
182182
"NOMAD_HOST_PORT_https=8080",
183183
"NOMAD_TASK_NAME=web",
184+
"NOMAD_GROUP_NAME=web",
184185
"NOMAD_ADDR_ssh_other=192.168.0.100:1234",
185186
"NOMAD_ADDR_ssh_ssh=192.168.0.100:22",
186187
"NOMAD_IP_ssh_other=192.168.0.100",

website/source/docs/runtime/_envvars.html.md.erb

+4
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@
5151
<td><tt>NOMAD_TASK_NAME</tt></td>
5252
<td>Task's name</td>
5353
</tr>
54+
<tr>
55+
<td><tt>NOMAD_GROUP_NAME</tt></td>
56+
<td>Group's name</td>
57+
</tr>
5458
<tr>
5559
<td><tt>NOMAD_JOB_NAME</tt></td>
5660
<td>Job's name</td>

website/source/docs/runtime/environment.html.md.erb

+5-5
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ environment variable names such as `NOMAD_ADDR_<task>_<label>`.
2323

2424
## Task Identifiers
2525

26-
Nomad will pass both the allocation ID and name as well as the task and job's
27-
names. These are given as `NOMAD_ALLOC_ID`, `NOMAD_ALLOC_NAME`,
28-
`NOMAD_ALLOC_INDEX`, `NOMAD_JOB_NAME`, and `NOMAD_TASK_NAME`. The allocation ID
29-
and index can be useful when the task being run needs a unique identifier or to
30-
know its instance count.
26+
Nomad will pass both the allocation ID and name as well as the task, group and
27+
job's names. These are given as `NOMAD_ALLOC_ID`, `NOMAD_ALLOC_NAME`,
28+
`NOMAD_ALLOC_INDEX`, `NOMAD_JOB_NAME`, `NOMAD_GROUP_NAME` and `NOMAD_TASK_NAME`.
29+
The allocation ID and index can be useful when the task being run needs a unique
30+
identifier or to know its instance count.
3131

3232
## Resources
3333

0 commit comments

Comments
 (0)