GitHub Actions is a GitHub feature used to automate software development workflows. It can automatically run tasks when events happen in a repository, such as pushing code or opening a pull request.
GitHub Actions is an automation platform integrated with GitHub repositories. It allows developers to create workflows that run automatically in response to repository events.
GitHub Event
↓
Workflow
↓
Jobs
↓
Steps
↓
Result
GitHub Actions can reduce repetitive manual work by automating development tasks.
A common workflow is to run tests whenever code is pushed.
Developer
↓
git push
↓
GitHub
↓
GitHub Actions
↓
Run Tests
↓
Pass / Fail
A workflow is an automated process defined in a YAML file inside the repository.
.github/
└── workflows/
└── test.yml
A workflow contains instructions describing when and how automation should run.
GitHub Actions workflow files are normally stored in:
.github/workflows/
For example:
.github/workflows/ci.yml
The workflow file uses YAML syntax.
name: CI
on:
push:
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: echo "Running tests"
This workflow runs when a push event occurs and then executes the defined job.
The name property gives a workflow a readable name.
name: My CI Workflow
The name helps developers identify the workflow in the GitHub Actions interface.
An event determines when a workflow should run.
Common examples include:
pushpull_requestworkflow_dispatchscheduleon:
push:
The push event can trigger a workflow when commits are
pushed to the repository.
on:
push:
This is commonly used for automated testing and build processes.
The pull_request event can trigger automation when a pull
request is created or updated.
on:
pull_request:
This is useful for automatically checking proposed changes.
A workflow can also be configured for manual execution using
workflow_dispatch.
on:
workflow_dispatch:
This allows an authorized user to start the workflow manually from GitHub.
GitHub Actions can also run workflows on a schedule using a cron expression.
on:
schedule:
- cron: '0 0 * * *'
Scheduled workflows are useful for recurring automation tasks.
A workflow contains one or more jobs. A job is a group of steps that runs as a unit.
jobs:
test:
...
build:
...
A workflow can contain multiple jobs.
Each job has an identifier.
jobs:
test:
Here, test is the job ID.
Another example:
jobs:
build:
...
A runner is the environment in which a GitHub Actions job executes.
runs-on: ubuntu-latest
The runner provides the environment needed to execute the job's steps.
A commonly used runner is:
runs-on: ubuntu-latest
The appropriate runner depends on the requirements of the project and workflow.
A job contains one or more steps. Steps perform the actual tasks of the job.
jobs:
test:
runs-on: ubuntu-latest
steps:
- run: echo "Hello"
- run: echo "Testing"
The run keyword executes a shell command.
- run: echo "Hello GitHub Actions"
Commands can be used to install dependencies, run tests, build projects, or perform other tasks.
An action is a reusable unit of automation that can perform a particular task.
- uses: actions/checkout@v4
The uses keyword allows a workflow to use an existing
action.
The checkout action is commonly used to make the repository's source code available to the workflow.
- uses: actions/checkout@v4
After checkout, later steps can work with the repository files.
A workflow can install project dependencies before running tests or building an application.
steps:
- uses: actions/checkout@v4
- run: npm install
- run: npm test
The exact commands depend on the programming language and project.
GitHub Actions can automatically run tests after code is pushed or a pull request is updated.
steps:
- uses: actions/checkout@v4
- run: npm install
- run: npm test
If the test command fails, the workflow can report a failed result.
Workflows can also build applications automatically.
steps:
- uses: actions/checkout@v4
- run: npm install
- run: npm run build
Build commands depend on the project's technology and configuration.
Jobs can be configured so that one job depends on another job.
jobs:
test:
...
build:
needs: test
...
Here, the build job depends on the test job.
Continuous Integration (CI) involves frequently integrating code changes and automatically checking them.
Push Code
↓
Workflow
↓
Install Dependencies
↓
Run Tests
↓
Build
↓
Result
GitHub Actions can also be used to automate deployment processes.
Code
↓
Test
↓
Build
↓
Deploy
Deployment steps depend on the hosting platform and project configuration.
name: Node.js CI
on:
push:
pull_request:
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm install
- run: npm test
This example checks the project whenever the configured events occur.
Repository
↓
GitHub Event
↓
Workflow Starts
↓
Runner
↓
Job
↓
Checkout Code
↓
Install Dependencies
↓
Run Tests
↓
Build
↓
Result
GitHub Actions provides automation directly inside GitHub repositories. Workflows are defined using YAML files and can run automatically in response to repository events.
Event
↓
Workflow
↓
Job
↓
Runner
↓
Steps
↓
Action / Command
↓
Result
GitHub Actions can be used for continuous integration, testing, building, deployment, scheduled tasks, and many other development automation processes.
.github/workflows/.Question: What is the main purpose of GitHub Actions?