Mantra Networking Mantra Networking

Github Actions (CI/CD): Action

Github Actions (CI/CD): Action
Created By: Lauren R. Garcia

Table of Contents

  • Overview
  • Core Components
  • Example: Deploy Network Automation with GitHub Actions
  • Commonly Used Actions
  • Best Practices
  • Conclusion

Overview of GitHub Actions (CI/CD): Action

What Is GitHub Actions?

GitHub Actions is an automation platform integrated into GitHub that enables you to build, test, deploy, and manage code via pipelines driven by events from your repositories. It allows you to create continuous integration (CI) and continuous deployment (CD) workflows with a clear, flexible structure—all defined as code in YAML files.

Why You Need to Know About GitHub Actions

  • Streamlines Automation: It automates repetitive development and deployment tasks, saving you time and eliminating manual steps.
  • Improves Code Quality: By integrating automated testing, linting, and security checks, you reduce bugs and spot issues earlier in the development cycle.
  • Enables Rapid Delivery: CI/CD pipelines allow you to release features and fixes faster, increasing agility and consistency across environments.
  • Supports Collaboration: Automated workflows help teams coordinate changes, enforce code reviews, and track deployment status, all within GitHub.
  • Fits Modern DevOps: If you work in DevOps, network automation, or infrastructure as code, GitHub Actions removes the friction from building, testing, and deploying projects at scale.

How GitHub Actions Works

  • Event-Driven Triggers: Actions respond to events (such as code pushes, pull requests, scheduled times, or manual triggers) in your repositories.
  • Workflows as Code: You define workflows in YAML files located in the .github/workflows/ directory. Each workflow specifies what triggers it, what jobs to run, and in what order.
  • Jobs and Steps: Workflows consist of jobs (which define separate stages or tasks), and each job contains a series of steps. Steps execute commands or invoke actions—reusable pieces of automation code.
  • Actions: These are standalone scripts or Docker containers that perform specific functions, such as checking out code, setting up languages/tools, or deploying artifacts. You can use official, community, or custom actions.
  • Runners: Each job is executed on a runner, which can be a hosted virtual machine provided by GitHub or your own self-hosted infrastructure.

By using GitHub Actions, you create robust pipelines for building, testing, securing, and deploying any application or automation—directly from your GitHub repositories. This approach bridges the gap between code and production, enabling smoother software delivery and infrastructure management.

Core Components

These are the essential building blocks that make GitHub Actions powerful for orchestrating automated CI/CD pipelines:

  • Workflow: A YAML-defined automation stored in the .github/workflows/ directory. Workflows are triggered by specific repository events (such as code pushes, pull requests, or scheduled times), and provide the overarching structure to define which jobs should run and when.
  • Job: A sequence of steps that execute on the same runner. Jobs are isolated from one another by default, can run sequentially or in parallel, and each specifies the environment required to perform its tasks.
  • Step: Individual tasks performed within a job. Steps execute in sequence in the job’s environment and can either run shell commands (run:) or call reusable actions (uses:).
  • Action: A reusable unit of code or automation invoked within a step. Actions can be official, community-built, or custom-developed for tasks like checking out code, setting up languages, or deploying builds.
  • Runner: The compute environment (hosted VM or self-hosted machine) that performs all steps and jobs. Each job runs in its allocated runner, ensuring jobs do not share state unless explicitly configured.
  • Events: Repository activities or scheduled triggers that initiate workflows (such as push, pull_request, or cron-based schedules).

Example: Deploy Network Automation with GitHub Actions

Follow these steps to automate the deployment of a network automation script using GitHub Actions CI/CD.

  1. Create the Workflow File:
    Add a new YAML file (for example, .github/workflows/ci-cd.yml) in your repository to define your workflow.
  2. Set Workflow Triggers:
    Configure the workflow to run on events such as a code push or pull request to the main branch.
    on:
      push:
        branches: [ "main" ]
      pull_request:
        branches: [ "main" ]
  3. Add Deployment Job:
    Define a job with the environment where your automation will run.
    jobs:
      deploy-infra:
        runs-on: ubuntu-latest
  4. Define Job Steps:
    • Check out the repository:
      - name: Checkout code
        uses: actions/checkout@v4
    • Set up Python:
      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.11'
    • Install dependencies:
      - name: Install dependencies
        run: pip install -r requirements.txt
    • Lint the code:
      - name: Run Linting
        run: flake8 .
    • Run the automation script:
      - name: Run Automation Script
        run: python automate_network.py
    • Store the logs and artifacts:
      - name: Deploy artifacts
        uses: actions/upload-artifact@v4
        with:
          name: deployment-logs
          path: logs/
  5. Complete Example Workflow:
    name: Network Automation CI/CD
    
    on:
      push:
        branches: [ "main" ]
      pull_request:
        branches: [ "main" ]
    
    jobs:
      deploy-infra:
        runs-on: ubuntu-latest
    
        steps:
          - name: Checkout code
            uses: actions/checkout@v4
    
          - name: Set up Python
            uses: actions/setup-python@v5
            with:
              python-version: '3.11'
    
          - name: Install dependencies
            run: pip install -r requirements.txt
    
          - name: Run Linting
            run: flake8 .
    
          - name: Run Automation Script
            run: python automate_network.py
    
          - name: Deploy artifacts
            uses: actions/upload-artifact@v4
            with:
              name: deployment-logs
              path: logs/
        

Commonly Used Actions

Here are some commonly used GitHub Actions that help streamline CI/CD workflows and automate routine tasks:

  1. Checkout Repository Code:
    This action checks out your repository code so the workflow can access it.
    uses: actions/checkout@v4
  2. Set Up Python Environment:
    This action sets up a Python environment with a specified version, ready for your scripts.
    uses: actions/setup-python@v5
    with:
      python-version: '3.11'
  3. Upload Artifacts:
    Upload build or deployment artifacts so they can be accessed in later steps or downloaded after the run.
    uses: actions/upload-artifact@v4
    with:
      name: artifact-name
      path: path/to/artifacts/
  4. Download Artifacts:
    Download previously uploaded artifacts in the workflow for use in other jobs or steps.
    uses: actions/download-artifact@v4
    with:
      name: artifact-name
      path: path/to/download/
  5. Build and Push Docker Images:
    Build Docker images and push them to your container registry.
    uses: docker/build-push-action@v5
    with:
      push: true
      tags: user/image-name:tag

Using these actions can simplify your workflow by reusing well-maintained components that cover common tasks.

Best Practices

Follow these practices to create robust, secure, and maintainable GitHub Actions workflows for your CI/CD processes:

  1. Use Secrets for Sensitive Information:
    Store credentials, tokens, and other sensitive data securely in GitHub Secrets. Reference them in workflows using ${{ secrets.YOUR_SECRET_NAME }} to avoid exposing them in code.
  2. Pin Versions of Actions:
    Specify exact versions or tags (e.g., @v4) when using actions to ensure stability and reproducibility of your workflows.
  3. Structure Workflows Clearly:
    Organize workflows into modular jobs such as linting, testing, and deployment to improve readability and maintainability.
  4. Store Logs and Artifacts:
    Upload logs, test results, and build artifacts during workflow runs to enable easy troubleshooting and auditability.
  5. Limit Workflow Scope:
    Trigger workflows only on necessary branches or events to minimize unnecessary runs and reduce resource consumption.
  6. Use Self-hosted Runners When Appropriate:
    For more control over hardware, software, or network configuration, consider using self-hosted runners.
  7. Test Workflow Changes in Feature Branches:
    Validate and troubleshoot workflow modifications in dedicated branches or forks before merging to main.

Conclusion

Throughout this blog post, we have explored how GitHub Actions serves as a versatile tool for automating CI/CD workflows, especially in network automation and infrastructure deployment. We covered the foundational components such as workflows, jobs, steps, actions, runners, and events, which together enable seamless automation triggered by repository activities.

You saw a practical example demonstrating how to set up a workflow for deploying network automation scripts, including installation, linting, running scripts, and artifact management. We also reviewed commonly used actions that simplify your pipeline, such as checking out code, setting up environments, and managing artifacts.

Following best practices, like securely handling sensitive information through secrets, using fixed action versions, organizing workflows cleanly, and testing changes safely, helps build reliable and maintainable pipelines.

By integrating GitHub Actions into your CI/CD process, you can accelerate development cycles, reduce manual steps, and improve consistency across deployments. These automation techniques empower you to focus more on innovation and less on repetitive tasks.

Thanks for reading! Feel free to explore, experiment, and share your own workflows as you advance your automation journey with GitHub Actions.

Happy automating! 🚀