Action Directory Structure
Earn 25 points (50 with Pro) in two steps
- ① Read through the lesson — each section gets a ✓ as you scroll through it.
- ② When every section has a ✓, tap Complete lesson.
0 of 13 read · keep scrolling
✦ See fewer ads and earn double points — 50 a lesson instead of 25 — with Pro
Lesson: Mastering Action Directory Structure
Introduction: Why Directory Structure Matters
In the world of software development, particularly when working with automated workflows, CI/CD pipelines, or plugin-based architectures, the way you organize your code is just as important as the code itself. An "Action"—a modular, reusable unit of logic—is the building block of modern automation. Whether you are building custom actions for GitHub, internal orchestration tools, or containerized task runners, the directory structure serves as the blueprint for how your software behaves, how it is discovered, and how it is maintained.
When you start a new project, it is tempting to dump all your files into a single root folder. This might work for a "Hello World" script, but as your logic grows to include dependencies, documentation, testing suites, and metadata, that flat structure becomes a liability. A well-defined directory structure ensures that your actions are predictable. When a colleague—or your future self—opens your repository, they should be able to navigate to the source code, locate the configuration files, and identify the entry point within seconds.
This lesson explores the industry-standard conventions for structuring action directories. We will move beyond simple file placement to discuss how metadata files, manifest definitions, and isolated execution environments interact to create reliable, scalable automation. By the end of this module, you will understand not just where to put your files, but why specific arrangements reduce technical debt and simplify long-term maintenance.
Not read yet
The Anatomy of an Action Directory
At its core, an action is simply a directory containing a manifest file and the code required to perform a task. However, the complexity arises when you need to package that logic for others to consume. A professional action directory usually follows a hierarchical pattern that separates configuration from implementation.
The Root Level
The root of your directory should be clean and focused. It acts as the "front door" for the action. You should only see files that are essential for the action's discovery and immediate operation.
action.ymloraction.yaml: This is the mandatory manifest file. It describes the action, its inputs, outputs, and the runtime environment. Without this file, the system simply sees a folder of scripts rather than an actionable tool.README.md: Documentation is not optional. A well-structured action must explain how to use it, what the inputs are, and what the expected outcomes look like.LICENSE: If you intend to share your action, a license file defines how others can use, modify, and distribute your work..gitignore: This prevents temporary files, build artifacts, or secret keys from being committed to your version control system.
The Source Code Directory (/src or /lib)
Never place your primary execution logic directly in the root directory. Instead, group your source files into a dedicated subdirectory. This keeps the root clean and allows you to enforce a clear boundary between configuration and logic.
main.jsormain.py: This is your entry point. It should be the first file executed when the action runs.- Helper modules: If your logic is complex, break it down into smaller, testable files. For example, if you are writing a JavaScript action, you might have
input-parser.js,api-client.js, andlogger.jsinside yoursrcfolder.
Callout: Separation of Concerns The primary reason for separating the manifest (
action.yml) from the source code (/src) is to maintain a clear boundary between metadata and implementation. Metadata defines how the system interacts with the action, while the implementation defines what the action actually does. When you keep these separate, you can update your logic without needing to re-understand the manifest, and vice versa.
Not read yet
Defining the Manifest: The action.yml File
The action.yml file is the heart of your directory structure. It acts as the contract between your code and the environment. If your directory structure is the house, the manifest is the floor plan that tells the inspector where the plumbing and electrical systems are located.
Metadata Breakdown
Your manifest must provide enough information for the environment to run the action correctly. Key components include:
- Name and Description: A clear, concise identifier for the action.
- Inputs: Parameters that the user provides to the action. These should be clearly defined with descriptions and default values.
- Outputs: Data that the action produces, which can be passed to subsequent steps in a workflow.
- Runs: The instruction that tells the system how to execute the code (e.g., using a specific Node.js version or a Docker container).
Practical Example: Node.js Action Manifest
Consider an action designed to validate a configuration file. Your action.yml might look like this:
name: 'Validate Config'
description: 'Validates a JSON configuration file against a schema'
inputs:
config-path:
description: 'The path to the JSON file'
required: true
default: 'config.json'
outputs:
is-valid:
description: 'Boolean indicating if the config is valid'
runs:
using: 'node20'
main: 'dist/index.js'
Note: In the example above, the
mainfield points todist/index.js. This is a common industry standard for compiled languages like TypeScript. You write your code insrc/, compile it, and the resulting artifact is placed indist/. This ensures that your source code and your production-ready code remain distinct.
Not read yet
Organizing for Scalability: Advanced Directory Patterns
As your actions grow in scope, you will encounter the need for testing suites, local development scripts, and documentation assets. A flat structure will quickly collapse under the weight of these files. Adopting a modular directory structure is the solution.
Recommended Directory Layout
A mature, production-ready action directory often follows this structure:
/my-action
├── .github/workflows/ # CI/CD for the action itself
├── dist/ # Compiled/minified code
├── src/ # Source code
│ ├── index.ts # Entry point
│ ├── utils.ts # Shared helpers
│ └── validator.ts # Core logic
├── tests/ # Unit and integration tests
├── node_modules/ # Dependencies (often ignored in git)
├── .gitignore # Files to exclude from version control
├── action.yml # The manifest
├── package.json # Dependency management
├── README.md # Documentation
└── tsconfig.json # TypeScript configuration
Why this structure works:
- Testing Isolation: By placing tests in a
/testsfolder, you ensure that test files are not accidentally bundled into your distribution. - Dependency Management: Using
package.jsonat the root allows you to manage third-party libraries independently of the action logic. - CI/CD Integration: The
.github/workflowsfolder allows you to run tests automatically every time you push code, ensuring your action remains stable.
Callout: The "Dist" Folder Paradox You might wonder why we need a
distfolder if we have asrcfolder. Thedistfolder represents the artifact. By separating source from distribution, you can optimize your code (e.g., minification, bundling) for the runtime environment without destroying the readability of your original source files. This is a standard practice in professional software engineering.
Not read yet
Step-by-Step: Setting Up a New Action
Let’s walk through the process of setting up a new action from scratch. We will assume a Node.js environment, as it is the most common runtime for these types of tasks.
Step 1: Initialize the Directory
Create your main folder and initialize the dependency manager.
mkdir my-new-action
cd my-new-action
npm init -y
Step 2: Create the Core Structure
Create the folders for your source code and testing.
mkdir src
mkdir tests
Step 3: Define the Manifest
Create the action.yml file in the root directory. Use the structure discussed in the previous section, ensuring that the runs field points to the file that will eventually exist in the dist folder.
Step 4: Write Your Logic
Create your entry point inside src. For example, create src/index.ts. Keep this file focused on handling inputs and outputs, and delegate the heavy lifting to modules within the src folder.
Step 5: Configure Build Tools
If you are using TypeScript, create a tsconfig.json file. Configure the outDir to point to dist. This ensures that when you run your build command, the files are automatically placed in the correct location for the action to consume.
Step 6: Test and Verify
Before publishing or using your action, run your test suite. If you have followed the directory structure, your test runner should be able to look at the tests directory and execute the files found there without interfering with the src or dist directories.
Not read yet
Best Practices for Action Maintenance
Maintaining an action is a long-term commitment. Over time, your code will evolve, dependencies will need updating, and new requirements will emerge. The way you structure your directory has a direct impact on how easy these changes are to implement.
1. Keep the Manifest Minimal
Your action.yml should be a declaration, not a script. Avoid putting complex logic inside the manifest. If you find yourself trying to execute shell commands directly in the runs section, it is a sign that you should move that logic into a script file inside your src directory.
2. Versioning and Tagging
While this isn't strictly a directory structure issue, your directory organization should support versioning. If you need to make breaking changes, you might consider having a v1/ and v2/ directory structure, or more commonly, using Git tags to manage versions. Keeping your directory structure clean makes the tagging process much easier to manage.
3. Documentation as Code
Store your documentation inside the repository. A docs/ folder can hold detailed architectural diagrams, API references, or troubleshooting guides. This keeps your README.md clean and provides a home for more extensive information that might overwhelm a simple documentation file.
4. Dependency Pinning
Always use a lockfile (package-lock.json or yarn.lock). This ensures that the environment running your action always uses the exact same versions of your dependencies. Without this, your action might work perfectly today and break tomorrow because a dependency was updated in an unexpected way.
Warning: Never include your
node_modulesfolder in version control. It is bloated, platform-specific, and unnecessary. Use yourpackage.jsonand lockfile to ensure that the environment can rebuild the dependency tree during the deployment or setup phase.
Not read yet
Common Pitfalls and How to Avoid Them
Even experienced developers can fall into traps when structuring their actions. Here are some of the most frequent mistakes and how to steer clear of them.
Pitfall 1: The "Everything in Root" Approach
As mentioned earlier, putting index.js, action.yml, README.md, and various helper scripts all in the root directory leads to a "spaghetti" folder.
- The Fix: Adopt a strict directory structure immediately. If you have more than three files that aren't configuration-related, move them into a
src/folder.
Pitfall 2: Hardcoding Paths
When your code refers to files using absolute paths (e.g., /home/user/action/config.json), it will fail when running in a different environment, such as a CI/CD runner.
- The Fix: Always use path-relative logic. In Node.js, use the
pathmodule to construct paths relative to the current file or the root of the project.
Pitfall 3: Ignoring the dist Folder
Some developers commit their src folder but forget to commit the compiled dist folder. If your action requires a build step, the system running the action will not have the tools to build it.
- The Fix: Ensure your
distfolder is included in your version control if your runtime environment expects a pre-built artifact.
Pitfall 4: Over-complicating the Manifest
Some developers try to pack too much logic into the action.yml file, such as complex conditional inputs or environment variable mapping that should be handled in code.
- The Fix: Remember that
action.ymlis for interface definition. Any logic that requires decision-making should be in your source code.
Not read yet
Quick Reference: Directory Component Guide
| Directory/File | Purpose | Essential? |
|---|---|---|
action.yml |
Defines inputs, outputs, and runtime | Yes |
/src |
Contains all source code logic | Yes |
/dist |
Contains compiled/bundled production code | Yes (if using build tools) |
/tests |
Contains unit and integration tests | Highly Recommended |
README.md |
Explains usage and requirements | Yes |
.gitignore |
Prevents unnecessary files in Git | Yes |
package.json |
Manages project dependencies | Yes (for Node-based actions) |
Comparison: Flat vs. Modular Structure
When choosing a structure, consider the growth potential of your project.
Flat Structure:
- Pros: Simple to set up, easy to understand for very small projects.
- Cons: Becomes unmanageable as the project grows, hard to test, difficult to maintain.
- Best for: Proof-of-concept scripts, single-file utilities.
Modular Structure:
- Pros: Scalable, testable, clean separation of concerns, easier for teams to collaborate.
- Cons: Slightly higher overhead to set up initially.
- Best for: Production-ready actions, shared libraries, complex workflows.
Not read yet
Deep Dive: Handling Environment Variables and Secrets
When organizing your directory, you must consider how the action will handle sensitive data. Your directory structure should not contain hardcoded secrets. Instead, your code should be structured to accept secrets via input variables defined in the action.yml.
Example: Secure Input Handling
In your action.yml, define a secret input:
inputs:
api-key:
description: 'The API secret'
required: true
In your code (src/index.ts), access this input using the provided environment variable mapping. By keeping the logic for handling secrets inside your source code, you ensure that you can implement logging filters (to prevent the secret from being printed to the console) and validation logic in a single, controlled location.
The Role of Documentation in the Directory
A directory structure is incomplete without documentation. A README.md file should be placed at the root level of your action. This file should be structured with the following sections to ensure maximum clarity for users:
- Overview: What does this action do?
- Usage: A code snippet showing how to include the action in a workflow.
- Inputs: A table detailing each input, its purpose, and whether it is required.
- Outputs: A table detailing what data the action provides to subsequent steps.
- Example: A complete, copy-pasteable example of the action in a real-world scenario.
By placing this in the root, you ensure that any user browsing your repository can immediately understand the value and mechanics of your tool without needing to parse the source code.
Not read yet
Handling Dependencies: A Closer Look
Dependency management is often where action developers get into trouble. If you are using a language like JavaScript or Python, your directory structure must account for how dependencies are installed.
- Node.js: Use a
package.jsonfile to define dependencies. Usenpm installoryarn installto generate yournode_modules. Remember, do not commitnode_modules. - Python: Use a
requirements.txtfile or apyproject.toml. - Docker: If your action is container-based, your directory should include a
Dockerfile. This file lives in the root and defines the entire environment, including dependencies.
Dockerfile Integration
If your action uses Docker, your directory structure changes slightly:
/my-docker-action
├── Dockerfile
├── entrypoint.sh
├── action.yml
└── src/
└── ...
In this case, the action.yml will point to the Dockerfile instead of a script file. The Dockerfile handles the installation of dependencies and the execution environment, making your action highly portable and independent of the host machine's configuration.
Not read yet
Advanced Maintenance: Automated Testing
As your action grows, you should implement automated testing. This is where the /tests directory becomes vital. You should use a testing framework (such as Jest for JavaScript or PyTest for Python) to verify your logic.
A well-structured /tests directory mimics the structure of your /src directory. If you have src/api-client.ts, you should have tests/api-client.test.ts. This makes it trivial to find the corresponding test for any piece of logic.
Example Test File Structure
tests/
├── unit/
│ ├── input-parser.test.ts
│ └── api-client.test.ts
└── integration/
└── workflow.test.ts
Separating unit tests (which test individual functions) from integration tests (which test the action as a whole) is a hallmark of professional software development. It allows you to run fast, lightweight tests during development and more intensive tests before you release a new version.
Not read yet
Key Takeaways for Action Authors
- Standardize the Root: Always keep your root directory clean. Only essential configuration files like
action.yml,README.md, andLICENSEshould live here. - Separate Metadata from Logic: Use the
action.ymlto define the interface and a dedicated/srcfolder to house your implementation. This boundary is critical for maintainability. - Build Artifacts: If your language requires compilation (like TypeScript), use a
dist/directory to store the final, executable code. Never mix your source files with your build artifacts. - Documentation is First-Class: Include a comprehensive
README.mdat the root. It is the most important piece of documentation for your action and should explain clearly how to use it. - Test Early and Often: Dedicate a
/testsfolder to your testing suite. Organize it to mirror yoursrcdirectory so that finding relevant tests is intuitive. - Dependency Hygiene: Use lockfiles and avoid committing dependency folders (like
node_modules). Keep your environment definitions clear and reproducible. - Think About Scalability: Even if your action is small today, structure it as if it will grow. Adopting a modular, scalable directory structure from day one prevents the need for painful refactoring later.
By adhering to these principles, you ensure that your actions are not just functional, but also professional, maintainable, and easy for others to adopt. The directory structure is the foundation of your project—build it well, and the rest of your development process will be significantly smoother.
Not read yet
Common Questions (FAQ)
Q: Should I commit my dist folder to Git?
A: Yes, in most cases, you should commit your dist folder. Because actions often run in environments where they might not have access to build tools, providing the pre-compiled code ensures the action can run immediately.
Q: Can I use subdirectories for my inputs?
A: While you can organize your code into subdirectories, your action.yml must clearly define the input interface. Keep your input definitions in the root manifest, even if the logic they trigger is nested deep within your directory structure.
Q: How do I handle large assets (like images or data files)?
A: If your action requires large data files, create a /data or /assets directory. Keep them outside of your src folder to maintain a clear distinction between code and content.
Q: Is it okay to have multiple action.yml files?
A: Generally, no. An action repository is typically designed to house a single action. If you have multiple related actions, consider either splitting them into separate repositories or using a monorepo structure, though the latter is significantly more complex to manage.
Q: What if I am using a language that doesn't require a build step?
A: Even if you don't need a dist folder, you should still use a src folder to keep your root clean. A structure like /src and /tests is recommended for almost every project, regardless of the language used.
Not read yet
Each section gets a ✓ as you scroll through it. Tap the button to jump to the next one.
- Scheduled Event Triggers
- Scheduled Event Triggers Quiz5q
- Manual Workflow Dispatch
- Manual Workflow Dispatch Quiz5q
- Webhook Event Triggers
- Webhook Event Triggers Quiz5q
- Repository Events Configuration
- Repository Events Configuration Quiz5q
- Workflow Dispatch Inputs
- Workflow Dispatch Inputs Quiz5q
- Reusable Workflows with workflow_call
- Reusable Workflows with workflow_call Quiz5q
- Jobs and Steps Fundamentals
- Jobs and Steps Fundamentals Quiz5q
- Conditional Logic in Workflows
- Conditional Logic in Workflows Quiz5q
- Job Dependencies with needs
- Job Dependencies with needs Quiz5q
- Service Containers Configuration
- Service Containers Configuration Quiz5q
- Matrix Strategy Configuration
- Matrix Strategy Configuration Quiz5q
- YAML Anchors and Aliases
- YAML Anchors and Aliases Quiz5q
- Predefined Contexts Usage
- Predefined Contexts Usage Quiz5q
- Azure Integration Patterns
- Azure Integration Patterns Quiz5q
- AWS Integration Patterns
- AWS Integration Patterns Quiz5q
- Kubernetes Deployments
- Kubernetes Deployments Quiz5q
- Notification Integrations
- Notification Integrations Quiz5q
- Code Quality Integrations
- Code Quality Integrations Quiz5q
- Third-Party Service Integration
- Third-Party Service Integration Quiz5q
- Workflow Orchestration Patterns
- Workflow Orchestration Patterns Quiz5q
- Multi-Environment Workflows
- Multi-Environment Workflows Quiz5q
- Blue-Green and Canary Deployments
- Blue-Green and Canary Deployments Quiz5q
- GitHub-Hosted Runners Configuration
- GitHub-Hosted Runners Configuration Quiz5q
- Self-Hosted Runners Setup
- Self-Hosted Runners Setup Quiz5q
- Runner Groups Management
- Runner Groups Management Quiz5q
- IP Allow Lists and Networking
- IP Allow Lists and Networking Quiz5q
- Runner Troubleshooting
- Runner Troubleshooting Quiz5q
- Environment Protections
- Environment Protections Quiz5q
- Trustworthy Marketplace Actions
- Trustworthy Marketplace Actions Quiz5q
- Script Injection Mitigation
- Script Injection Mitigation Quiz5q
- GITHUB_TOKEN Permissions
- GITHUB_TOKEN Permissions Quiz5q
- OIDC Token for Cloud Federation
- OIDC Token for Cloud Federation Quiz5q
- Action Version Pinning
- Action Version Pinning Quiz5q
- Artifact Attestations and Provenance
- Artifact Attestations and Provenance Quiz5q
Spend your points to unlock exam-style practice questions
Spend points to unlock GH-200 exam-style practice questions.
Practice for GH-200