GitHub Actions

Ashutosh Kumar Sah
5 min readSep 25, 2024

--

GitHub Actions has revolutionized the way developers automate workflows, allowing seamless integration of Continuous Integration (CI) and Continuous Deployment (CD) directly into your repository. Whether it’s building, testing, or deploying code, GitHub Actions provides a flexible and scalable solution for DevOps tasks. This article will explore the core concepts of GitHub Actions, how to create workflows, and advanced features to supercharge your automation pipelines.

What is GitHub Actions?

GitHub Actions is a CI/CD service integrated into GitHub, allowing developers to automate software workflows. It enables you to define and run custom workflows triggered by events in your GitHub repository, such as pushes, pull requests, or scheduled events. By defining workflows as YAML configuration files, you can automate processes like:

  • Building and testing code.
  • Deploying applications.
  • Managing pull requests and issues.
  • Automating repetitive tasks.

GitHub Actions can be customized to run on different platforms (Linux, macOS, and Windows) and support multiple languages and frameworks.

Key Concepts

Before diving into creating workflows, it’s essential to understand some key concepts in GitHub Actions.

1. Workflows

A workflow is an automated process defined by a YAML file located in your repository under .github/workflows. Workflows are triggered by specific events (e.g., push, pull_request, schedule) and contain one or more jobs to be executed.

name: CI Workflow
on:
push:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: '16'
- name: Install dependencies
run: npm install
- name: Run tests
run: npm test

2. Events

Events are the triggers that start a workflow. GitHub Actions supports a wide variety of triggers, such as:

  • push: Triggered when changes are pushed to the repository.
  • pull_request: Triggered when a pull request is opened, synchronized, or closed.
  • schedule: Triggered at scheduled intervals (CRON syntax).
  • workflow_dispatch: Manually triggered workflows.

You can define multiple triggers within a workflow file.

3. Jobs

A job is a collection of steps that run sequentially on a given runner. Each job runs in a fresh virtual environment, ensuring isolation between workflows.

jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Run tests
run: npm tes

4. Runners

Runners are servers that execute the jobs in your workflow. GitHub provides hosted runners for Linux, Windows, and macOS. Alternatively, you can use self-hosted runners to gain more control over your environment.

5. Steps

Steps are individual tasks that make up a job. They can either use pre-built actions or execute custom shell commands.

steps:
- name: Install dependencies
run: npm install

6. Actions

Actions are reusable units of code that can be combined to create custom workflows. You can use public actions from the GitHub Marketplace or create your ow

- uses: actions/checkout@v3

Setting Up a Basic Workflow

Let’s walk through creating a simple GitHub Actions workflow that automatically runs tests every time code is pushed to the main branch.

1. Create a Repository

First, create a GitHub repository or use an existing one.

2. Define the Workflow

Inside your repository, create a new directory called .github/workflows. Inside that directory, create a YAML file, e.g., ci.yml.

name: CI
on:
push:
branches:
- main
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: '16'
- name: Install dependencies
run: npm install
- name: Run tests
run: npm test

3. Push Your Changes

Commit and push your changes to the repository. The workflow will automatically trigger whenever you push to the main branch.

4. Monitor the Workflow

You can monitor the status of your workflow by navigating to the “Actions” tab of your repository. This will show you all running and completed workflows, along with detailed logs.

Advanced Features

GitHub Actions provides several advanced features to make workflows more efficient and customizable.

1. Matrix Builds

Matrix builds allow you to run jobs across multiple combinations of environments. For instance, you can test your application against multiple versions of Node.js or Python

jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [14, 16]
steps:
- uses: actions/checkout@v3
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- name: Install dependencies
run: npm install
- name: Run tests
run: npm test

2. Reusable Workflows

You can reuse workflows between repositories, reducing duplication and promoting DRY (Don’t Repeat Yourself) principles.

jobs:
deploy:
uses: org/repo/.github/workflows/deploy.yml@main

3. Secrets and Environment Variables

GitHub Actions allows you to store sensitive data (like API keys or tokens) as secrets, which can be accessed securely in your workflows.

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Deploy to production
run: |
echo ${{ secrets.PRODUCTION_API_KEY }}

4. Caching

To speed up workflows, GitHub Actions supports caching dependencies like node_modules or build artifacts.

- name: Cache node modules
uses: actions/cache@v3
with:
path: node_modules
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-

5. Artifacts

Artifacts are files generated during a workflow that you can store and share between jobs or download after the workflow has completed.

- name: Upload test results
uses: actions/upload-artifact@v3
with:
name: test-results
path: test-results

Best Practices for GitHub Actions

1. Keep Workflows Modular

Break down large workflows into smaller, more manageable jobs. This allows parallel execution and faster feedback loops.

2. Use Caching Wisely

Efficient use of caching can significantly speed up builds, especially in monorepos or large projects with many dependencies.

3. Monitor Workflow Performance

Regularly monitor the time each workflow takes and optimize jobs for performance. For example, avoid unnecessary build steps and dependencies.

4. Leverage Third-Party Actions

Instead of writing everything from scratch, utilize the vast array of third-party actions available in the GitHub Marketplace.

5. Limit Workflow Triggers

Avoid triggering workflows unnecessarily, such as for minor documentation changes or pushes to feature branches that don’t require CI/CD.

on:
push:
branches:
- main
- develop

Conclusion

GitHub Actions is a powerful automation tool that allows developers to integrate CI/CD processes directly into their GitHub repositories. By mastering the core concepts of workflows, jobs, and actions, you can automate everything from code testing to complex multi-step deployment pipelines. Start small with basic workflows and gradually incorporate advanced features such as matrix builds, caching, and reusable workflows to optimize your development process.

Whether you’re a solo developer or part of a large team, GitHub Actions can streamline your development workflow, making it an essential tool in modern software engineering.

--

--

Ashutosh Kumar Sah
Ashutosh Kumar Sah

Written by Ashutosh Kumar Sah

DevOps Engineer | Ex - Teqfocus | Microsoft Certified: Az-900, Ai -900, Dp-900 | 2x Oracle cloud infrastructure certified fundamental 2022 | Aviatrix certified

No responses yet