Automation is important in software development to reduce manual effort, improve efficiency, and streamline workflows. GitHub Actions is a built-in CI/CD (Continuous Integration and Continuous Deployment) tool in GitHub that allows developers to automate tasks such as building, testing, and deploying applications.
In this article, we will learn how to use GitHub Actions, from understanding its core components to creating your first workflow and optimizing it for maximum efficiency.
What are GitHub Actions?
GitHub Actions is a powerful automation tool built directly into GitHub, allowing developers to automate, customize, and run workflows directly from their repositories. By using GitHub Actions, we can automate processes such as continuous integration, continuous deployment, testing, and code reviews.
Unlike traditional CI/CD tools, GitHub Actions is event-driven, meaning it can be triggered by various GitHub events like code commits, pull requests, issue creation, and more.
Key Features of GitHub Actions
- Seamless Integration: Directly integrated with GitHub, providing a unified development experience.
- Flexibility and Customization: Supports a wide range of automation tasks, from testing and building to deployment.
- Powerful CI/CD Capabilities: Automates complex CI/CD pipelines with ease.
- Multi-Platform Support: It runs workflows across different operating systems (Linux, macOS, Windows).
- Community-Driven: Extensive marketplace with reusable actions created by the community and GitHub.
How to Use GitHub Actions?
There are two ways through which we can create the GitHub Actions.
- Using the Github UI
- Locally with your IDE
1. Create a GitHub Action Using the GitHub UI
Click on the Action tab to create a GitHub Action. Youâll see the following page:
Creating GitHub Action Using the GitHub UIStep 1: Click on the Action
GitHub ActionsStep 2: Select the workflow action
GitHub suggestions automatically work according to the nature of your project. Select the GitHub workflow and click on the configure button to create your action.
GitHub ActionStep 3.Create the GitHub workflow
Here you can edit and create your action. Click on the commit change button to save the action.
Create GitHub WorkflowYour GitHub Action has been created.
2. Create a GitHub Action Locally with Your IDE
- Open your project on the particular IDE like Vs code.
- Then create a .github/workflow/name-of-workflow.yml file in your project.
- Copy and paste the following code and save and push your local code into the GitHub repository.
- Below is the workflow action example mentioned which will print the hello world message.
XML
# .github/workflows/demo.yml
name: CI
on:
push:
branches: ["main"]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Run a one-line script
run: echo Hello, world!
- This workflow runs whenever a push is made to the main branch.
- Job Execution defines a 'job' called build that runs on an Ubuntu environment.
- The workflow triggers on a push to the main branch, runs a job on an Ubuntu environment, checks out the repository code, and executes a script that prints "Hello, world!".
GitHub Actions Workflow Components
1. Events in GitHub Actions
In GitHub Actions, events are the triggers that kick off workflows. These events are specific activities in your repository that lead to the execution of actions. For example, pushing new code or creating a pull request triggers specific events that initiate associated workflows.
Common GitHub Action Event Types
- Push: Triggered when code is pushed to the repository.
- Pull Request: Activated when a pull request is created, opened, or updated.
- Release: Occurs when a release is created or updated.
- Label: Happens when a label is applied or removed.
- Issues: Fired when an issue is opened, edited, or closed.
- Milestone: Triggered when a milestone is added or updated.
Each event type can be further customized to trigger only for specific actions. For example, a pull_request event can be restricted to occur only when a PR is opened for the 'releases/**' branch.
# .github/workflows/demo.yml
on:
issues:
types: [opened, edited, milestoned]
pull_request:
types:
- opened
branches:
- "releases/**"This precise control over event types ensures that your workflow is efficient and minimizes unnecessary resource usage.
2. Jobs in GitHub Actions
Jobs are tasks that run within a GitHub Action workflow. By default, jobs run in parallel, but you can define dependencies so that one job runs after another. This is useful when certain tasks depend on the output of previous ones, such as building an app before running tests.
# .github/workflows/demo.yml
name: Demo Workflows
on:
push:
jobs:
build:
name: Build
needs: [Development]
steps:
- name: Build and deploy on Cloud
dev:
name: Development
steps:
- name: Run the developer
Test:
needs: [build, dev]
name: Testing
steps:
- name: Testing the applicationIn the above example, the Test job depends on the build and dev jobs. The jobs will run in parallel, but Test will wait until the other two jobs complete.
3. Runners in GitHub Actions
Runners are the virtual machines or environments where your GitHub Actions workflows execute. GitHub provides runners for various operating systems, such as Ubuntu, Windows, and MacOS.
You can specify the type of runner to use for each job in the workflow by setting the runs-on option.
Defining a Runner:
# .github/workflows/demo.yml
name: Demo workflows
on:
push:
branches: ["main"]
jobs:
build:
runs-on: ubuntu-latest
This configuration specifies that the build job should run on the latest version of Ubuntu. You can also define multiple runners for a job using an array:
Why Use GitHub Actions?
GitHub Actions offers several advantages over traditional CI/CD tools
- Seamless Integration: Works natively within GitHub, reducing the need for external CI/CD services.
- Event-Driven Automation: Triggers workflows based on repository events like pushes, pull requests, and issues.
- Parallel Execution: Run multiple jobs concurrently to speed up CI/CD pipelines.
- Customizable Workflows: Define workflows in YAML files to suit specific project needs.
- Supports Multiple Runners: Works with GitHub-hosted and self-hosted runners for diverse environments.
- Marketplace for Actions: Utilize pre-built actions from the GitHub Marketplace to simplify workflow creation.
Use Cases of GitHub Actions
GitHub Actions can be applied in various scenarios
- CI/CD Pipelines: Automate testing, building, and deployment.
- Automated Code Reviews: Run static analysis and linting tools.
- Scheduled Jobs: Automate routine tasks such as backups.
- Security Scans: Detect vulnerabilities using security tools.
- Notification Triggers: Send alerts to Slack, email, or Discord.
Similar Reads
Github Actions For Android
GitHub Actions is a continuous integration and continuous delivery (CI/CD) platform that allows you to automate your build, test, and deployment pipeline. CI/CD methodology provides speed, accuracy, and reliability. It increases the release rates by detecting the failures faster. Android is a widely
3 min read
Introduction to GitHub Actions
GitHub Actions is a CI/CD (Continuous Integration/ Continuous Deployment) platform for automating the builds, test, and deployment process. Using GitHub actions, we can build and test every pull request in the repository using workflows, or push the merged pull requests to production with workflows.
4 min read
How to Add GitHub Actions Secrets ?
When it comes to safely managing sensitive data in your workflowsâlike access tokens, API keys, and other credentialsâGitHub Actions secrets are essential. By using these tricks, you can securely access and save private information without exposing it to the source code of your repository. You may i
5 min read
How to Delete GitHub Actions Cache ?
Resources or dependencies that you regularly utilize in your workflows can be temporarily stored in the GitHub Actions cache. The GitHub Actions the cache significantly speeds up the workflow execution by reuse these files and eliminate the need to rebuild dependencies or download them each time a p
5 min read
Gitlab CLI
GitLab, a leading platform for version control, offers a range of tools for managing your repositories, CI/CD pipelines, and more. The GitLab Command Line Interface (CLI) is one of the most powerful tools provided by GitLab and helps to interact with GitLab services directly from the terminal. The G
5 min read
How to Skip a Job in GitHub Actions ?
With the help of GitHub Actions, developers can effectively automate a range of processes and workflows right within their repositories. You can develop, test, and publish your code using GitHub Actions without ever leaving the GitHub website. It provides an adaptable and configurable workflow frame
6 min read
What is GitHub CLI?
GitHub CLI (Command Line Interface) is a powerful tool developed by GitHub that allows developers to interact with GitHub directly from the terminal. It provides a simple way to perform many GitHub tasks without leaving the command line interface, such as managing repositories, handling pull request
7 min read
What are GitHub Collaborators?
In GitHub, we can invite other GitHub users to become collaborators to our private repositories(which expires after 7 days if not accepted, restoring any unclaimed licenses). Being a collaborator, of a personal repository you can pull (read) the contents of the repository and push (write) changes to
3 min read
How to use AWS CLI in GitHub Actions ?
Through a command-line interface, Amazon offers a powerful tool called the Amazon Web Services Command Line Interface, or AWS CLI, which makes managing AWS resources easier. The importance of this is that it can simplify the process of utilizing AWS services straight from the terminal, removing the
4 min read
The Matrix Strategy in GitHub Actions
GitHub Actions provides a platform for automating your CI/CD pipelines, allowing you to run workflows directly from your GitHub repositories. One of the most powerful features of GitHub Actions is the matrix strategy, which allows to run a single job in multiple configurations. This can include diff
5 min read