GitHub Actions
GitHub Actions is a built-in Continuous Integration and Continuous Deployment (CI/CD) platform that automates build, test, and deployment workflows directly within your GitHub repository.
Workflows can run automatically when events occur, such as pushing code, opening a Pull Request, creating a branch, or manually starting a workflow.
1. How GitHub Actions Works
GitHub Actions uses YAML files to define automation. Workflow files are normally stored in:
.github/workflows/
Events (Triggers)
Events determine when a workflow starts. Common
examples include push,
pull_request, and
workflow_dispatch for manual execution.
Jobs
A job is a group of steps executed on a runner.
Jobs run in parallel by default, but you can use
needs to make one job wait for another.
Runners
Runners are the environments that execute workflow jobs. GitHub-hosted runners offer environments such as Ubuntu Linux, Windows, and macOS. You can also configure self-hosted runners.
Steps and Actions
Steps run shell commands or reusable actions. Actions can perform tasks such as checking out code, setting up a programming language, or publishing build artifacts.
2. A Basic Example Workflow
Create a file named
test.yml inside
.github/workflows/. This example
runs a Node.js test command whenever code is
pushed to the main branch.
name: Run Automated Tests
on:
push:
branches: [main]
jobs:
test-project:
runs-on: ubuntu-latest
steps:
- name: Checkout repository code
uses: actions/checkout@v4
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
- name: Install dependencies
run: npm install
- name: Execute test commands
run: npm test
package.json
and a configured test script.
3. Understand the YAML File
| Configuration | Purpose |
|---|---|
name |
Sets the workflow's display name. |
on |
Defines the events that trigger the workflow. |
jobs |
Defines the jobs to execute. |
runs-on |
Specifies the runner environment. |
steps |
Lists the tasks executed by the job. |
uses |
Runs a reusable action. |
run |
Executes a shell command. |
with |
Provides configuration inputs to an action. |
4. Why Use GitHub Actions?
Automated Testing
Run tests whenever code is pushed or a Pull Request is opened. This helps catch problems before changes are merged.
Automated Guardrails
Repository rules and required status checks can prevent Pull Requests from being merged until required workflow checks pass.
Continuous Deployment
Workflows can build and deploy applications to platforms such as AWS, Azure, Google Cloud, or Vercel after the required checks succeed.
GitHub Marketplace
The Marketplace provides reusable actions for common tasks, including notifications, security scanning, cloud authentication, and deployment. Review an action's source and permissions before using it in a workflow.
5. Create and Run Your First Workflow
- Open your GitHub repository.
- Create the
.github/workflowsdirectory if it does not exist. - Create a workflow file such as
test.yml. - Paste the YAML workflow and adjust it for your project.
- Commit and push the file to GitHub.
- Open the repository's Actions tab.
- Select the workflow run to inspect its jobs, steps, and logs.