Implementing CI/CD with GitHub Actions on Shared Hosting (HostGator)

2 min read

Introduction

In the world of web development, an efficient CI/CD pipeline is crucial to ensure that new features and fixes are deployed quickly and securely. With tools like GitHub Actions, developers can automate and optimize their workflow. In this article, we will explore the benefits of GitHub Actions and how to configure it to work with a shared hosting environment like HostGator.

Benefits of GitHub Actions

  1. Complete Automation: GitHub Actions allows for the automation of processes from testing to deployment, saving time and minimizing manual errors.
  2. Easy Integration: Seamlessly integrates with GitHub, simplifying the setup and monitoring of workflows.
  3. Flexibility: Supports multiple environments and languages, offering a wide range of pre-configured actions.
  4. Scalability: Ideal for projects of all sizes, from small personal sites to large corporate applications.

Step-by-Step Configuration

1. Preparing the Environment

On HostGator, we need to ensure that necessary tools like composer and drush are installed and available in the PATH.

# Install Composer
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer

# Install Drush
composer global require drush/drush
echo 'export PATH="$HOME/.composer/vendor/bin:$PATH"' >> ~/.bash_profile
source ~/.bash_profile

 

2. Configuring GitHub Actions

In your GitHub repository, create a file .github/workflows/deploy.yml with the following content:

name: CI/CD Pipeline

on:
  push:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest

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

      - name: Set up PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: '8.2'
          extensions: mbstring, xml, ctype, iconv, tokenizer, gd, openssl, json, pdo_mysql

      - name: Cache Composer dependencies
        uses: actions/cache@v2
        with:
          path: vendor
          key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
          restore-keys: ${{ runner.os }}-composer

      - name: Install dependencies
        run: composer install --prefer-dist --no-progress --no-suggest

      - name: Run Code Sniffer
        run: |
          export PHPCS_MEMORY_LIMIT=512M
          vendor/bin/phpcs --standard=Drupal --ignore=*.min.js,*.min.css, web/themes/custom/

      - name: Add SSH Key and Known Hosts
        run: |
          mkdir -p ~/.ssh
          echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa
          chmod 600 ~/.ssh/id_rsa
          ssh-keyscan -H 162.241.2.203 >> ~/.ssh/known_hosts
          ssh-keyscan -H github.com >> ~/.ssh/known_hosts

      - name: Deploy to Production Server
        uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.SSH_HOST }}
          username: ${{ secrets.SSH_USER }}
          password: ${{ secrets.SSH_PASSWORD }}
          port: 22
          use_insecure_cipher: true
          script: |
            source /etc/profile || true
            source /etc/bashrc || true
            source ~/.bashrc || true
            source ~/.bash_profile || true
            export PATH="$HOME/.composer/vendor/bin:$PATH"
            cd ~/public_html
            git pull
            composer install
            drush @default.production cr
            drush @default.production updb -y
            drush @default.production cim -y
            drush @default.production cr

 

3. Configuring HostGator

On HostGator, create and configure the ~/.ssh/config file to ensure SSH authentication works correctly:

nano ~/.ssh/config

Add the following content:

Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa
  StrictHostKeyChecking no


 

4. Adding SSH Keys to GitHub

Add the SSH public key to your repository on GitHub under Settings > SSH and GPG keys.

Conclusion

Configuring GitHub Actions for automatic deployments on a shared hosting environment like HostGator may initially seem challenging, but the benefits of automation, integration, and scalability make it worthwhile. With this configuration, you ensure that your workflow is efficient and your changes are deployed quickly and securely.

Share this article