Sub topic 1
Choosing Between JavaScript, Container, and Composite Actions
Prerequisites
Assume you know basic workflow syntax, JavaScript and npm, Docker concepts, Git, and shell commands.
Choosing an Action Type
A custom action packages reusable automation for a workflow. Choose the implementation that matches the task:
| Type | Best fit | Main trade-off |
|---|---|---|
| JavaScript | Fast, portable logic using Node.js | Dependencies must be bundled |
| Docker container | Tools needing a specific Linux environment | Runs only on Linux runners and starts more slowly |
| Composite | A sequence of existing workflow steps | Cannot provide the same programmatic control as JavaScript |
Use a composite action when you mainly combine commands and other actions. Use JavaScript for API calls, structured input handling, or cross-platform behavior. Use a container when the action depends on system packages or a fixed command-line environment.
Describing an Action with `action.yml`
Every action has an action.yml or action.yaml metadata file at its root. It declares the action's name, inputs, outputs, and implementation:
name: "Hello World"
description: "Greets a person"
inputs:
who-to-greet:
description: "Name to greet"
required: true
default: "World"
outputs:
time:
description: "Time of the greeting"
runs:
using: "node20"
main: "dist/index.js"The runs section selects the action type. A JavaScript action uses a Node runtime and points to its bundled entry file. A Docker action instead uses using: docker and names its Dockerfile. A composite action uses using: composite and defines steps.
Declaring Inputs and Outputs
Inputs arrive as strings. Mark an input required when the action cannot work without it. Outputs let later workflow steps consume results. The workflow assigns the action step an id, then reads an output through that identifier.
Writing a JavaScript Action
The Actions toolkit provides helpers for reading inputs and setting outputs:
const core = require("@actions/core");
try {
const person = core.getInput("who-to-greet");
console.log(`Hello ${person}!`);
core.setOutput("time", new Date().toISOString());
} catch (error) {
core.setFailed(error instanceof Error ? error.message : String(error));
}Install @actions/core with npm and bundle the dependency into the file named by main. The runner does not automatically install your package dependencies when it executes a published JavaScript action.
Exercise
Which action type should you choose for a task that needs a pinned Linux toolchain?
Show answer
Choose a Docker container action, because its image can define the required operating system tools and versions.