Huzi Blogs
Blog
Blog
Disclaimer & Data Privacy Policy
Project by huzi.pk

© 2026 blogs.huzi.pk. All Rights Reserved.

    Back to all posts
    DevOps

    Automating Workflows with CI/CD and GitHub Actions

    By Huzi

    What is CI/CD?

    CI/CD is a cornerstone of modern DevOps practices. It's a method to frequently deliver apps to customers by introducing automation into the stages of app development.

    • Continuous Integration (CI): This is the practice of developers merging their code changes into a central repository frequently. Each merge triggers an automated build and test sequence. The goal is to detect integration issues early and maintain a stable codebase.
    • Continuous Deployment (CD): This is the practice of automatically deploying every change that passes the CI stage to a production environment. It's the next step after Continuous Delivery, which ensures the code is always in a deployable state but may require a manual trigger for the final push to production.

    Introducing GitHub Actions

    GitHub Actions is a powerful and flexible CI/CD platform built directly into GitHub. It allows you to automate, customize, and execute your software development workflows right in your repository. You can use it to build, test, and deploy your code, or to automate any other task associated with your project.

    Workflows are defined in YAML files within a .github/workflows directory in your repository.

    Creating Your First Workflow

    Let's create a simple workflow for a Node.js application that runs tests every time code is pushed to the main branch or a pull request is created.

    1. Create the directory: In your project root, create .github/workflows/.
    2. Create the YAML file: Inside that directory, create a file named ci.yml.
    
    # .github/workflows/ci.yml
    
    name: Node.js CI
    
    # Controls when the workflow will run
    on:
      push:
        branches: [ "main" ]
      pull_request:
        branches: [ "main" ]
    
    # A workflow run is made up of one or more jobs that can run sequentially or in parallel
    jobs:
    
      # This workflow contains a single job called "build"
      build:
    
        # The type of runner that the job will run on
        runs-on: ubuntu-latest
    
        # Steps represent a sequence of tasks that will be executed as part of the job
        steps:
    
          # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
          - name: Checkout repository
            uses: actions/checkout@v3
    
          # Sets up a specific version of Node.js
          - name: Use Node.js 18.x
            uses: actions/setup-node@v3
            with:
              node-version: '18.x'
              cache: 'npm' # Caches npm dependencies for faster builds
    
          # Installs dependencies
          - name: Install dependencies
            run: npm install
    
          # Runs your tests
          - name: Run tests
            run: npm test
    

    Breaking Down the Workflow File

    • name: The name of your workflow, which will appear in the "Actions" tab of your GitHub repository.
    • on: The event that triggers the workflow. Here, it's a push or pull_request to the main branch.
    • jobs: A workflow is composed of one or more jobs. Our job is named build.
    • runs-on: Specifies the virtual machine environment to run the job on. ubuntu-latest is a common choice.
    • steps: A sequence of tasks to be executed.
      • uses: This keyword specifies an "action" to run. Actions are reusable units of code. actions/checkout@v3 and actions/setup-node@v3 are popular, official actions.
      • run: This keyword executes command-line commands.

    Continuous Deployment Example

    Let's extend our workflow to deploy our application to a server after the tests pass. This example assumes you're deploying to a server via SSH.

    Important: You must first add your server's credentials (HOST, USERNAME, SSH_PRIVATE_KEY) as "Secrets" in your GitHub repository settings (Settings > Secrets and variables > Actions). Never hardcode secrets in your workflow files.

    Add a new job called deploy to your ci.yml file:

    
    # ... (keep the build job as is)
    
      deploy:
    
        # This job depends on the 'build' job to succeed
        needs: build
        runs-on: ubuntu-latest
        
        # Only run this job on a push to the main branch, not on pull requests
        if: github.event_name == 'push' && github.ref == 'refs/heads/main'
    
        steps:
          - name: Checkout repository
            uses: actions/checkout@v3
    
          - name: Deploy to server
            uses: appleboy/ssh-action@master
            with:
              host: ${{ secrets.HOST }}
              username: ${{ secrets.USERNAME }}
              key: ${{ secrets.SSH_PRIVATE_KEY }}
              script: |
                cd /path/to/your/app
                git pull
                npm install --production
                pm2 restart your-app-name
    
    • needs: build: Ensures the deploy job only runs if the build job completes successfully.
    • if: A conditional to ensure deployment only happens on a direct push to main.
    • appleboy/ssh-action: A popular marketplace action for executing commands on a remote server over SSH.
    • script: The set of commands to run on your server to pull the latest code and restart the application.

    Conclusion

    GitHub Actions provides a powerful, integrated way to implement CI/CD pipelines. By automating your build, test, and deployment processes, you can ship features faster, reduce manual errors, and improve the overall quality of your software. Start with a simple testing workflow and gradually add more automation to streamline your development lifecycle.

    Advertisements


    You Might Also Like

    Luxury Heavy Embroidered Lawn Suit 3-Pc | 4-Side Border Embroidered NET Dupatta (2025)

    Luxury Heavy Embroidered Lawn Suit 3-Pc | 4-Side Border Embroidered NET Dupatta (2025)

    PKR 6900

    Heavy Embroidered Masoori Bridal Suit | Luxury Velvet Shawl (4 Side Border)

    Heavy Embroidered Masoori Bridal Suit | Luxury Velvet Shawl (4 Side Border)

    PKR 7950

    Elegant Embroidered Chiffon Party Wear Dress 2026

    Elegant Embroidered Chiffon Party Wear Dress 2026

    PKR 5350

    6-in-1 Vegetable Cutter & Slicer – Stainless Blades, 1.2 L Container

    6-in-1 Vegetable Cutter & Slicer – Stainless Blades, 1.2 L Container

    PKR 1499

    Teal Blue Sequins Embroidered Chiffon Party & Wedding Dress – Unstitched Luxury Outfit

    Teal Blue Sequins Embroidered Chiffon Party & Wedding Dress – Unstitched Luxury Outfit

    PKR 5500

    Advertisements


    Related Posts

    DevOps
    An Introduction to Containerization with Docker
    Solve the 'it works on my machine' problem with Docker. Learn the fundamentals of containerization, how Docker works, and how to build and run your first Docker container.

    By Huzi

    Read More
    DevOps
    Understanding DevOps: It's More Than Just Tools
    DevOps is not just a set of tools; it's a culture and a mindset. Learn about the core principles of DevOps, including collaboration, automation, and continuous improvement, and how they can transform your organization.

    By Huzi

    Read More
    DevOps
    Introduction to Kubernetes: Managing Containers at Scale
    Docker is great for running containers, but what happens when you have hundreds of them? Kubernetes is the leading open-source platform for automating the deployment, scaling, and management of containerized applications.

    By Huzi

    Read More
    Freelancing
    Remote-Work Gold Rush 2025: The 10 High-Income Skills Pakistani Freelancers Must Master
    Discover the exact digital skills that are paying Pakistanis $30""$120/hour from home in 2025. Includes course links, project blueprints, salary data, and step-by-step 30-day action plans tailored for students, fresh grads, and career switchers.

    By Huzi

    Read More
    Legal
    Comprehensive Guide: Filing Police Misconduct Complaints in Pakistan (2025)
    In 2025, your complaint receipt is your shield—wield it against the 'thana culture'.

    By Huzi

    Read More
    Technology
    Best Phones for Battery Life: Top 10 Long-Battery Mobiles Available in Pakistan 2025
    In 2025, several phones in Pakistan combine large batteries, efficient chips, and fast charging to give you more hours of usage and less anxiety. Here are ten of the best.

    By Huzi

    Read More