Skip to content

action

The action resource reference. This resource allows Codify to perform scripting actions on the system. The action is executed in the user’s current interactive login shell. An optional condition parameter is also available to control when the action should be run. Actions appear in the plan and are only applied during the apply step.

Actions are powerful resources that are not confined to predetermined logic like other resources. Actions can be used to perform arbitrary tasks such as npm install, mkdir tempFolder, and brew update. They can be made stateful using the condition parameter, ensuring a specific action is only executed once. See the examples below for more ideas on how to use actions.

Parameters:

  • action: (string, required) The action to execute. The action is run during the apply phase in an interactive login shell.

  • condition: (string) An optional condition parameter. The condition determines if the action is included or not included in the plan. The condition is executed in an interactive login shell during the plan phase. Return exit code 1 to include the action in the plan (indicating something doesn’t exist on the system) and exit code 0 for no action. Leaving the condition undefined means the action resource always shows up in the plan.

  • cwd: (string) An optional parameter to specify the current working directory for both the action and condition commands.

Example usage:

Running npm install

codify.json
[
{
"type": "nvm",
"nodeVersions": ["20"],
"global": ["20"]
},
{
"type": "action",
"condition": "[ -d node_modules ]",
"action": "npm install",
"cwd": "~/projects/my-repo",
"dependsOn": ["nvm"]
}
]

This action will run npm install on a repository if npm install hasn’t been run yet. It checks the project files for the presence of the node_modules folder which gets generated by npm install. If it can’t detect the folder then the action (npm install) will show up in the plan and be applied.

Creating a file if it doesn’t exist

codify.json
[
{
"type": "action",
"condition": "[ -e .env ]",
"action": "touch .env",
"cwd": "~/projects/my-repo"
}
]

This action creates a .env file if one cannot be found in the project files.

Create a directory if not found

codify.json
[
{
"type": "action",
"condition": "[ -d testing ]",
"action": "mkdir testing"
}
]

This action creates a testing folder if one cannot be found.

Runs brew update

codify.json
[
{ "type": "homebrew" },
{
"type": "action",
"action": "brew update",
"dependsOn": ["homebrew"]
}
]

This actions keeps homebrew up to date by running brew update automatically everytime Codify is applied.