Skip to main content

Exec

The built-in plugin called exec allows you to execute shell commands from within your HCL configuration.

Have a question?

Check the FAQ to see if there's already an answer.

Configuration

None

Actions

NameDescription
exec_commandExecute a shell command

exec_command

The exec_command action allows you to execute a shell command.

Arguments

ArgumentDescriptionTypeRequired
commandThe command to executestringYes
dot_env_filenameName of file for outputsstringNo

Outputs

OutputDescriptionType
exit_codeThe exit code of the commandnumber
dot_envOutputs parsed from dot_env_filename abovemap

Usage

plugin exec {}

stage "dev" {
task "hello_world" {
action "exec_command" {
alias = "hello_exec"
dot_env_filename = ".env"
command = <<EOF
echo "Hello, world!"
echo "example_key=example_value" >> .env
EOF
}
}
}

stage "stage-2" {
task "get_outputs" {
action "exec_command" {
command = <<EOF
echo "output value : ${action_outputs.stage.dev.task.hello_world.action.hello_exec.dot_env.example_key}"
EOF
}
}
}

In this example, the exec plugin is used to execute the echo "Hello, world!" command. The output of this command will be

  • the exit code of the command, which will be 0 if the command was successful.
  • a map of any key-value pairs (strings) written to file .env in the first stage.

Frequently Asked Questions (FAQ)

How would I reference the output of this exec action in another?

The full path to the exec action can be used to get its output. For example, if you wanted to reference the exit_code output from the exec action in the example above, you would use action_outputs.stage.dev.task.hello_world.action.exec_command.exit_code in another action.

How can I use a complex variable or output inside of an exec command?

Since the command argument only accepts a string type, you will need to convert the complex variable or output to a string before it can be used. The variable can be passed to the built-in jsonencode function to convert it to a string. For example, if you wanted to use the variable var.my_variable in an exec command, you would use jsonencode(var.my_variable) in the command argument.