Automation

GitHub Actions Tutorial: CI/CD Workflow Setup Guide

Learn how to set up CI/CD workflows with GitHub Actions using a step-by-step guide. Manage workflows, triggers, secrets, and environments effectively.

4 min read

Learn how to set up CI/CD workflows with GitHub Actions. This guide provides step-by-step instructions for creating workflows, configuring triggers, managing secrets, and deploying across environments effectively.

Introduction to GitHub Actions

GitHub Actions is a powerful automation tool seamlessly integrated into GitHub repositories. While it is commonly used for Continuous Integration and Continuous Deployment (CI/CD), it can also handle event-driven automation tasks. Workflows are defined in YAML files and triggered by repository events such as commits, pull requests, or schedules.

Key Features of GitHub Actions for CI/CD:

  • Native integration with GitHub repositories.
  • Event-driven workflows for automating CI/CD processes.
  • Thousands of reusable actions contributed by GitHub, third-party providers, and the community.

Prerequisites for Setting Up GitHub Actions CI/CD

prerequisites

  • A GitHub repository (public or private) containing your project code.
  • Basic knowledge of YAML syntax and CI/CD principles.
  • Familiarity with the tools and dependencies required by your project.

Creating a GitHub Actions Workflow

Workflows in GitHub Actions are defined in YAML files stored in the .github/workflows/ directory of your repository. This example will demonstrate creating a simple workflow to run tests on a Node.js project.

steps

  1. Navigate to your GitHub repository.
  2. Create a new folder named .github/workflows/.
  3. Inside the folder, create a file named ci.yml.
  4. Add the following contents to the file:
yaml
name: CI Workflow

on:
  push:
    branches:
      - main
  pull_request:

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - 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
  1. Commit your changes to the repository, and GitHub Actions will automatically trigger the workflow for applicable events.

Configuring Triggers and Events

GitHub Actions trigger workflows based on specific events. These triggers ensure that workflows run only when necessary.

steps

  1. Modify the on key in your ci.yml file to configure triggers. Example:

    yaml
    on:
      push:
        branches:
          - main
      pull_request:
        branches:
          - feature/*
      schedule:
        - cron: '0 0 * * *' # Runs daily at midnight
  2. For manual execution of workflows, use workflow_dispatch:

    yaml
    on:
      workflow_dispatch:
  3. Save the file and commit it to configure the updated triggers.

Implementing Secrets Management

Sensitive information like API keys or credentials should not be hardcoded into workflows. GitHub provides secure storage and retrieval of secrets.

Using Environments in GitHub Actions

Environments represent deployment stages like dev, test, or prod. They provide additional control over workflows.

steps

  1. In the GitHub repository, go to Settings > Environments.
  2. Click New environment, and name it, e.g., development.
  3. Optionally configure Environment protection rules:
    • Add reviewers who must approve deployments.
    • Configure wait timers and allowed branches.

Example YAML integration:

yaml
jobs:
  deploy:
    runs-on: ubuntu-latest
    environment:
      name: production
      url: https://my-production-app.com

    steps:
      - name: Deploy application
        run: echo "Deploying to production environment"

Best Practices for Workflow Configuration

When working with GitHub Actions, follow these best practices for maintainable and secure workflows:

  • Reuse workflows using reusable templates or shared actions.
  • Pin action versions. Example: actions/checkout@v4 ensures consistent behavior.
  • Keep workflows modular—break tasks into jobs for better manageability.
  • Document workflows to help collaborators understand triggers, jobs, and their purposes.

FAQ

How do I start a GitHub Actions workflow manually?

To manually trigger a workflow, include workflow_dispatch under the on key in your workflow file. Then, go to the Actions tab in your GitHub repository and click "Run workflow."

Can GitHub Actions work with private repositories?

Yes, GitHub Actions supports private repositories. However, certain features, such as environment protection rules, may require a GitHub Enterprise plan.

Is it possible to deploy to multiple environments in one workflow?

Yes, you can define multiple jobs in a single workflow and use environment-specific configuration. Use the environment key within each job to define the deployment stage.

How secure are GitHub Actions workflows?

GitHub provides robust security features, including secret masking, environment restrictions, and access control. Ensure you follow best practices like pinning action versions and avoiding sensitive data in logs.


Official reference: GitHub Actions documentation.