Creating a GitHub Action
GitHub Actions allow you to automate tasks directly in your repository. Here’s a detailed guide to creating a custom GitHub Action.
Step 1: Set Up Your Repository
Ensure you have a GitHub repository where your action will be stored. You can create a new repository or use an existing one.
Step 2: Create the .github/actions
Directory
Actions are stored within the .github/actions/
directory inside your repository. Create the necessary directory structure:
mkdir -p .github/actions/my-action
Step 3: Define the Action in action.yml
The action.yml
file is the main configuration file for your GitHub Action. Inside .github/actions/my-action/
, create a file named action.yml
and add the following content:
name: "My Custom Action"
description: "A simple GitHub Action example"
author: "Your Name"
inputs:
message:
description: "Message to display"
required: true
default: "Hello, GitHub Actions!"
runs:
using: "node20"
main: "index.js"
Step 4: Implement the Action Logic
The logic of the action is defined in an executable script. If you're using JavaScript, create an index.js
file in the same directory:
const core = require("@actions/core");
try {
const message = core.getInput("message");
console.log(`Message: ${message}`);
} catch (error) {
core.setFailed(error.message);
}
Step 5: Commit and Push the Action
After creating the required files, commit and push them to GitHub:
git add .
git commit -m "Add custom GitHub Action"
git push origin main
Your GitHub Action is now ready to be used in workflows.