Hands-on demo – Testing an Action in GitHub
Below is a step-by-step demo to test a custom GitHub Action. We'll create a local test workflow to validate the action's functionality.
Prerequisites
A GitHub repository with a custom action defined (e.g., JavaScript/TypeScript or Docker-based).
Basic knowledge of GitHub Actions YAML syntax.
Step 1: Define Your Action
Example:
action.yml
A simple JavaScript-based GitHub Action that echoes an input value.
xxxxxxxxxx91name"Echo Input Action"2description"A simple action to echo an input"3inputs4 message5 description"The message to echo"6 requiredtrue7runs8 using"node16"9 main"index.js"Example:
index.js
The Node.js script for the action.
xxxxxxxxxx71const core = require("@actions/core");2try {3 const message = core.getInput("message");4 console.log("Message: " . $message);5} catch (error) {6 core.setFailed(error.message);7}Step 2: Create a Test Workflow
Define a separate GitHub workflow to test the action locally in your repository.
File:
.github/workflows/test-action.yml
xxxxxxxxxx131nameTest Echo Action2on3 workflow_dispatch# Allows manual triggering for testing4jobs5 test-action6 runs-onubuntu-latest7 steps8nameCheckout Repository9 usesactions/checkout@v310nameRun the Action11 uses./ # Reference the action from the current repo12 with13 message"Hello from the test!"Step 3: Trigger the Test Workflow
Commit and push the workflow file:
xxxxxxxxxx31git add .2git commit -m "Add test workflow"3git push origin mainNavigate to the Actions tab in your GitHub repository.
Select the "Test Echo Action" workflow.
Click Run workflow (manual trigger via
workflow_dispatch).
Step 4: Analyze Console Output
After the workflow runs, click on the "test-action" job.
Look at the logs for the "Run the Action" step.
Example Output:
xxxxxxxxxx21Run ./2Message: Hello from the test!
Step 5: Add Debugging (Optional)
Enhance logging for more detailed output:
Enable
ACTIONS_STEP_DEBUG:xxxxxxxxxx21env2ACTIONS_STEP_DEBUGtrueAdd additional logs to your code:
xxxxxxxxxx11console.log("Debug: Starting action execution...");
Step 6: Automated Tests with Matrix Strategy
For comprehensive testing, include a matrix to validate across multiple configurations.
Example:
xxxxxxxxxx131jobs2 test-action3 runs-onubuntu-latest4 strategy5 matrix6 message"Test 1" "Test 2" "Test 3"7 steps8nameCheckout Repository9 usesactions/checkout@v310nameRun the Action11 uses./12 with13 message$ matrix.message Output:
xxxxxxxxxx31Message: Test 12Message: Test 23Message: Test 3Step 7: Testing Edge Cases
Update your test workflow to include invalid inputs:
xxxxxxxxxx41nameTest Missing Input2 uses./3 with4 message"" # Intentionally empty inputObserve whether the action fails gracefully with a clear error message.
Step 8: Local Testing with Act
For faster testing, use the Act tool:
Install Act:
xxxxxxxxxx11brew install actRun the workflow locally:
xxxxxxxxxx11act -j test-actionDebug and iterate on your action without triggering GitHub workflows.
Summary
This demo demonstrates how to define, test, and debug a GitHub Action effectively.






















Leave a Reply