Github Actions in Short
Table of contents
Using GitHub Actions lets you create custom workflows that can be triggered based on several events happening in your GitHub repository, such as pushes, pull requests, or issues. You might then automate tasks such as building, testing, or deploying code.
Core Concepts
- Workflows
A workflow is a configurable automated process that consists of one or more jobs. Workflows are defined in YAML files in the.github/workflows directory of your repository. Each workflow can be triggered by specific events such as a push to a branch or the creation of a pull request.
2. Jobs
Jobs are lists of steps that run on the same runner. Every job runs in a new process of a virtual environment. Jobs can be defined for running sequentially or parallelly according to the workflow.
- Steps
Steps are single actions of a job. They may execute commands, utilize actions or execute scripts. Steps will be executed in the sequence that they have been defined.
- Actions
Actions are reusable blocks of code. These can be grouped together to form workflows. You can build your own action or you can make use of a shared action provided by the community. Shared actions are found in the GitHub Marketplace.
- Runners
Runners are the servers running your workflows. GitHub offers hosted runners with lots of different operating systems or you host your own for more control over the environment.
Example
Concept | Description | Example |
Workflow | A configurable automated process defined in YAML files. | on: push triggers a workflow on every push to the repository. |
Event | Specifies the activity that triggers the workflow. | on: pull_request triggers on pull request creation or updates. |
Job | A collection of steps executed on the same runner. | jobs: build defines a build job in the workflow. |
Step | Individual tasks within a job, such as running commands or actions. | - name: Install dependencies runs a script to install dependencies. |
Runner | The server that runs the workflow jobs. | GitHub-hosted runners (Linux, Windows, macOS) or self-hosted runners. |
Actions | Reusable commands or code packages for common tasks. | actions/checkout@v3 is used to check out the repository code. |
Secrets | Encrypted environment variables for secure credentials. | Add API keys as secrets in the repository settings. |
Matrix Builds | Allows running jobs in parallel with different configurations. | Run tests on multiple Node.js versions using a matrix strategy. |
Artifacts | Files generated by a workflow that can be downloaded after execution. | Build outputs stored using actions/upload-artifact. |
Environment | Defines deployment targets or groups of secrets specific to an environment. | Set up environments like staging or production. |
Triggers | Events like push, pull_request, or schedule (cron jobs) that start workflows. | on: schedule with a cron expression runs workflows periodically. |
Conditional Execution | Specify conditions for running steps or jobs using if. | if: github.event_name == 'push' runs a step only for push events. |
Caching | Save dependencies or files between jobs to speed up workflows. | Use actions/cache to cache npm modules or Python dependencies. |
Notifications | Send alerts or status updates based on workflow success or failure. | Configure notifications for workflow failures via email or Slack integrations. |