GYLD
← All posts

15 July 2024

Mastering Modern Web Development Tools: Continuous Integration and Deployment with GitHub Actions

#Continuous Integration and Deployment · #CI/CD · #Github · #Github Actions · #Development

Mastering Modern Web Development Tools: Continuous Integration and Deployment with GitHub Actions

Welcome back to our blog series on mastering modern web development tools. In this post, we'll explore continuous integration and deployment (CI/CD) with GitHub Actions. CI/CD is a critical practice in modern software development, enabling teams to deliver high-quality software faster and more reliably. Let’s dive into the importance of CI/CD, how to get started with GitHub Actions, and some best practices to follow.


The Importance of CI/CD


Continuous integration (CI) is the practice of automatically integrating code changes into a shared repository several times a day, ensuring that each change is tested and verified. Continuous deployment (CD) automates the release process, allowing code changes to be deployed to production frequently and reliably. CI/CD helps catch bugs early, reduces manual intervention, and ensures that your software is always in a deployable state. At The Gyld, we rely on CI/CD to maintain high code quality and deliver updates efficiently.


Getting Started with GitHub Actions


GitHub Actions is a powerful CI/CD platform that allows you to automate your workflows directly within your GitHub repository. Here’s how to get started with GitHub Actions:


1. Creating a Workflow File


Workflows in GitHub Actions are defined using YAML syntax. To create a new workflow, add a YAML file in the .github/workflows directory of your repository. Here’s an example workflow file:


# .github/workflows/ci.yml
name: CI

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

jobs:
  build:
    runs-on: ubuntu-latest

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

    - name: Set up Node.js
      uses: actions/setup-node@v2
      with:
        node-version: '14'

    - name: Install dependencies
      run: npm install

    - name: Run tests
      run: npm test

This workflow runs on every push and pull request to the main branch. It checks out the code, sets up Node.js, installs dependencies, and runs tests.


2. Setting Up Continuous Deployment


To set up continuous deployment, you can add additional steps to your workflow to build and deploy your application. Here’s an example of deploying a static site to GitHub Pages:


# .github/workflows/deploy.yml
name: Deploy

on:
  push:
    branches: [main]

jobs:
  build:
    runs-on: ubuntu-latest

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

    - name: Set up Node.js
      uses: actions/setup-node@v2
      with:
        node-version: '14'

    - name: Install dependencies
      run: npm install

    - name: Build site
      run: npm run build

    - name: Deploy to GitHub Pages
      uses: peaceiris/actions-gh-pages@v3
      with:
        github_token: ${{ secrets.GITHUB_TOKEN }}
        publish_dir: ./build

This workflow builds the site and deploys it to GitHub Pages whenever changes are pushed to the main branch.


3. Using Secrets for Secure Deployments


For secure deployments, you can use GitHub Secrets to store sensitive information like API keys and tokens. To add a secret, go to your repository settings, navigate to Secrets, and add a new secret. You can access these secrets in your workflow using the secrets context:


- name: Deploy to Production
  env:
    API_KEY: ${{ secrets.API_KEY }}
  run: npm run deploy

Embracing Best Practices


To make the most out of GitHub Actions, follow these best practices:


1. Keep Workflows Modular


Break down complex workflows into smaller, reusable jobs and steps. This makes your workflows easier to maintain and debug:


# .github/workflows/ci.yml
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout code
      uses: actions/checkout@v2
    - name: Set up Node.js
      uses: actions/setup-node@v2
      with:
        node-version: '14'
    - name: Install dependencies
      run: npm install

  test:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout code
      uses: actions/checkout@v2
    - name: Set up Node.js
      uses: actions/setup-node@v2
      with:
        node-version: '14'
    - name: Install dependencies
      run: npm install
    - name: Run tests
      run: npm test

2. Use Caching to Speed Up Builds


Use caching to store dependencies and other files that don’t change often, reducing build times:


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

3. Test Across Multiple Environments


Ensure your application works in different environments by running tests across multiple versions of Node.js or operating systems:


jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [10, 12, 14]

    steps:
    - name: Checkout code
      uses: actions/checkout@v2
    - name: Set up Node.js
      uses: actions/setup-node@v2
      with:
        node-version: ${{ matrix.node-version }}
    - name: Install dependencies
      run: npm install
    - name: Run tests
      run: npm test

4. Notify on Build Status


Set up notifications to stay informed about the status of your builds. You can use the actions/github-script to send notifications to Slack, email, or other communication tools:


- name: Notify Slack
  uses: 8398a7/action-slack@v3
  with:
    status: ${{ job.status }}
    fields: repo,message,commit,author,action,eventName,ref,workflow,job,took
  env:
    SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}

The Gyld’s Commitment to CI/CD


At The Gyld, we prioritize continuous integration and deployment to ensure our software is always in a deployable state. By using GitHub Actions, we automate our workflows, reduce manual intervention, and maintain high code quality.


We continuously review and refine our CI/CD practices, participate in code reviews, and stay updated with the latest tools and techniques. This commitment to CI/CD allows us to deliver high-quality software and maintain the trust of our clients.


Conclusion


Continuous integration and deployment with GitHub Actions is essential for modern web development. By understanding and implementing CI/CD practices, you can ensure your software is always in a deployable state, reduce manual intervention, and maintain high code quality.


Stay tuned for the next post in our series, where we’ll explore API development and testing with Postman. Until then, keep learning, stay curious, and embrace change. That’s the Gyld way.