AI Hints
Home›CI/CD with GitHub Actions Tutorial›Writing Your Own Actions
CI/CD with GitHub Actions · Lesson 104 of 138

Writing a Script Action

Actions with code.

⏱ 2 min read▶ Video at 2:00:55🎓 Free
Watch this lesson in the CI/CD with GitHub Actions courseWatch on YouTube ↗Full CI/CD with GitHub Actions course

When shell steps get complicated, you can write an action in JavaScript. It runs on Node, reads its inputs, and can set outputs. Most actions in the marketplace are built this way.

Writing a Script Action
From the video: Writing a Script Action
  • Too much shell?
  • Write it in JavaScript
  • Inputs and outputs

The action

The action dot y m l declares a required input called who, and an output called greeting. Using node twenty means GitHub runs index dot JS with Node.

.github/actions/greet/action.ymlyaml
name: Greet
description: Says hello and sets an output
inputs:
  who:
    description: Who to greet
    required: true
outputs:
  greeting:
    description: The greeting
runs:
  using: node20
  main: index.js

index.js

GitHub passes each input as an environment variable, named input_and the name in capitals. The script builds the greeting, and logs it. Then it appends greeting equals, to the github output file, which sets the output. Real actions usually use the actions core package, which wraps these details.

.github/actions/greet/index.jsjavascript
const fs = require("fs");

const who = process.env.INPUT_WHO;
const greeting = `Hello, ${who}!`;
console.log(greeting);
fs.appendFileSync(process.env.GITHUB_OUTPUT, `greeting=${greeting}\n`);

Running it

The workflow passes who, and reads the output in the next step. Our helper runs the action with Node. The action logs its greeting, and the next step prints the same text from the output.

Terminal
run-steps .github/workflows/greet.yml | grep -vE "^Workflow"
Job: hello
  ✓ Greet the team
      Hello, the shop team!
  ✓ Run echo "The action said Hello, the shop team!"
      The action said Hello, the shop team!
  Job succeeded

Summary

Script actions read inputs from input variables, and write outputs to the github output file. Next, finding actions other people wrote.

Key takeaways
  • using: node20
  • INPUT_WHO
  • GITHUB_OUTPUT
Next up: Marketplace Actions — using what others built