Files
nomad/website/content/docs/job-specification/action.mdx
Aimee Ukasick 986f3c727a Docs: SEO job spec section (#25612)
* action page

* change all page_title fields

* update title

* constraint through migrate pages

* update page title and heading to use sentence case

* fix front matter description

* Apply suggestions from code review

Co-authored-by: Jeff Boruszak <104028618+boruszak@users.noreply.github.com>

---------

Co-authored-by: Jeff Boruszak <104028618+boruszak@users.noreply.github.com>
2025-05-19 09:02:07 -05:00

133 lines
3.7 KiB
Plaintext

---
layout: docs
page_title: action block in the job specification
description: |-
Configure custom commands in the `action` block of the Nomad job specification. Operators may execute these commands on a running allocation as a controlled way to interact with tasks. Review examples that use actions with arguments and templates.
---
# `action` block in the job specification
<Placement groups={['job', 'group', 'task', 'action']} />
The `action` block allows job authors to define custom commands. These commands
can be executed by operators with the necessary permissions on a running
allocation, offering a controlled way to interact with tasks.
The name of the action can contain alphanumeric characters and dashes. This
name must be unique within its task and must not exceed 128 characters.
## `action` Parameters
- `command` `(string: <required>)` - Specifies the command to be executed.
- `args` `(array<string>: [])` - Provides a list of arguments to pass to the command.
```hcl
job "my-job" {
group "my-group" {
task "my-task" {
action "get-changelog" {
command = "/usr/bin/curl"
args = [
"-s",
"https://raw.githubusercontent.com/hashicorp/nomad/main/CHANGELOG.md"
]
}
# ...
}
}
}
```
## `action` Examples
### Basic Action
This example demonstrates a simple action that prints the current date and time:
```hcl
job "example" {
# ...
group "demo" {
# ...
task "show-date" {
# ...
action "current-date" {
command = "/bin/date"
}
}
}
}
```
### Action with Arguments
Here, an action uses arguments to perform a specific task:
```hcl
job "example" {
# ...
group "demo" {
# ...
task "list-files" {
# ...
action "list-tmp" {
command = "/bin/ls"
args = ["-l", "/tmp"]
}
}
}
}
```
### Action with Template
This advanced example demonstrates an action that fetches and formats the latest
changelog from the Nomad GitHub repository using a shell script. It showcases
the use of templating with embedded environment variables and a multi-line script.
```hcl
action "fetch-latest-nomad-changelog" {
command = "/bin/sh"
args = ["-c",
<<EOT
curl -s https://raw.githubusercontent.com/hashicorp/nomad/main/CHANGELOG.md |
awk 'BEGIN{
# Setting record and field separators
RS="## "; FS="\n";
section=""; count=0
}
{
# Processing only the first 3 sections after the header
if (count < 3 && NR > 1){
# Splitting the version line into components
split($1, versionInfo, /[()]/);
version=versionInfo[1];
gsub(" ", "", version); # Removing spaces from version
releaseDate=versionInfo[2];
# Formatting URL components
urlVersion=version; gsub("\\.", "", urlVersion); # Remove dots from version
urlDate=releaseDate; gsub(" ", "-", urlDate); gsub(",", "", urlDate); # Replace spaces with hyphens and remove comma
# Counting items under each section
for(i=1; i<=NF; i++){
if($i ~ /^[A-Z ]+:$/){
gsub(":", "", $i);
section=$i;
itemCount[section]=0;
}
if(section && $i ~ /^\*/){
itemCount[section]++;
}
}
# Printing the extracted information
printf "Version: %s\nRelease Date: %s\n", version, releaseDate;
for (s in itemCount) {
printf "%d %s, ", itemCount[s], s;
}
printf "\nLink: https://github.com/hashicorp/nomad/blob/main/CHANGELOG.md#%s-%s\n\n", urlVersion, tolower(urlDate);
delete itemCount; # Clear the itemCount array for the next version
count++;
}
}'
EOT
]
}
```