Skip to main content
In this tutorial, you’ll learn how to build a custom GitHub Actions JavaScript action from scratch. We’ll cover:
  • Prerequisites for local development
  • Project scaffolding and configuration
  • Defining metadata in action.yml
  • Implementing core logic in index.js
  • Bundling with Vercel NCC
  • Publishing your action to GitHub Marketplace
The image shows a GitHub documentation page titled "Creating a JavaScript action," which provides a guide on building a JavaScript action using the actions toolkit. The page includes an introduction and a sidebar with related topics.

Prerequisites

Before you begin, ensure you have:
  • Node.js v20 or later installed
  • A GitHub account and repository
Keep your GitHub token and API keys secure. Never commit secrets directly to your repo.
The image shows a GitHub documentation page about creating a JavaScript action, including prerequisites like downloading Node.js and creating a GitHub repository.

1. Define Your Action Metadata

All GitHub Actions require an action.yml file to declare inputs, outputs, and execution details. Here’s a minimal example:
You can extend this file later to include more inputs or permissions.

2. Scaffold the Project

Create a new directory and initialize with npm:
Your generated package.json will look like:

3. Create and Update action.yml

We’ll start with a Docker-based example and then switch to Node.js.
Now update it for JavaScript:
The image shows a GitHub repository page for "docker-action-pr-giphy-comment," displaying files like Dockerfile, README.md, and action.yml, along with details about branches and commits.

4. Install Dependencies

In index.js, we’ll require the following modules:
Install them with npm:

Dependency Versions


5. Write the Action Logic

Create an index.js file and implement the core workflow:

6. Bundle with Vercel NCC

To reduce file size and avoid committing node_modules, bundle your code into a single file:
Update action.yml to reference the bundled script:
Add a .gitignore file to prevent tracking large files:

7. Publish Your Action

  1. Create a new repository on GitHub, e.g., js-action-pr-giphy-comment.
The image shows a GitHub interface for creating a new repository, with fields for the repository name, description, visibility options, and initialization settings.
  1. Push your code:
  2. Tag a release and publish on the GitHub Marketplace following Publishing actions in the GitHub Marketplace.
Your final repository should resemble:
The image shows a GitHub repository page titled "js-action-pr-giphy-comment," displaying files like .gitignore, README.md, and index.js. The repository is public with no stars or forks.
Congratulations! You now have a fully functional JavaScript-based GitHub Action that posts a Giphy GIF comment on new pull requests.

Watch Video