Autocodewizard Logo Workflow: Developing Locally and Pushing to Live - Autocodewizard Ebook: Web Development Getting Started

Chapter 9: Workflow: Developing Locally and Pushing to Live

In web development, it’s common to develop and test code locally before pushing changes to a live environment. In this chapter, we’ll cover traditional methods like FTP and SFTP for file transfers, and introduce automated Continuous Integration/Continuous Deployment (CI/CD) workflows using GitHub webhooks to streamline the process.

File Transfers: FTP vs. SFTP

File Transfer Protocol (FTP) and Secure File Transfer Protocol (SFTP) are commonly used methods for transferring files between your local machine and a remote server. Depending on your hosting provider, you may have access to FTP, SFTP, or both. Here’s a quick overview of each:

Setting Up an FTP/SFTP Client

To use FTP or SFTP, you’ll need a client like FileZilla or Cyberduck. Here’s how to set up a connection:

Transferring Files via FTP/SFTP

After establishing a connection, you can upload or download files to and from the server by dragging them between the local and remote directories within your FTP/SFTP client. Be sure to check with your hosting provider for specific folder structures, as many hosts have designated folders for live website files (e.g., public_html).

Web Development - Local Development and Live Deployment

Automating Deployments with CI/CD

Continuous Integration and Continuous Deployment (CI/CD) is a modern workflow that automates the process of testing and deploying code. In a CI/CD pipeline, code changes are automatically tested and deployed to a live environment when they’re pushed to a designated branch (such as main or production). Using CI/CD, you can avoid the manual file transfer process and reduce the risk of errors.

Setting Up CI/CD with GitHub Actions

GitHub Actions is a tool that enables CI/CD directly within GitHub. With GitHub Actions, you can define workflows to automate the testing and deployment process. Here’s a basic example to deploy a static site using GitHub Actions:

        # .github/workflows/deploy.yml
        name: Deploy to Production
        
        on:
          push:
            branches:
              - main
        
        jobs:
          deploy:
            runs-on: ubuntu-latest
            steps:
              - name: Checkout code
                uses: actions/checkout@v2
        
              - name: Deploy to server
                env:
                  HOST: ${{ secrets.SSH_HOST }}
                  USERNAME: ${{ secrets.SSH_USERNAME }}
                  PASSWORD: ${{ secrets.SSH_PASSWORD }}
                run: |
                  scp -r * $USERNAME@$HOST:/path/to/deployment/directory
                

In this example, GitHub Actions listens for pushes to the main branch. When a push is detected, it deploys the contents of the repository to a specified server directory using scp. Note that sensitive information like the host, username, and password is stored securely as GitHub Secrets.

Using GitHub Webhooks for Automated Deployments

GitHub webhooks can also be used to trigger CI/CD processes on external servers. A webhook is a URL endpoint that GitHub notifies when certain events (e.g., pushes) happen in a repository. When integrated with a CI/CD server, a webhook can trigger the deployment process automatically upon new code pushes.

Setting Up a GitHub Webhook

To set up a webhook in GitHub, follow these steps:

Web Development - GitHub Webhook Setup

Setting Up the Server to Receive Webhooks

To receive webhooks and automatically deploy code, your server must have an endpoint configured to handle the webhook payload. Here’s a simple example of a webhook receiver script using PHP:

        <?php
        // deploy.php - Script to automate deployment via GitHub webhook
        $payload = json_decode(file_get_contents('php://input'), true);
        
        if ($payload['ref'] === 'refs/heads/main') {
            // Execute deployment commands
            shell_exec('cd /path/to/your/project && git pull origin main');
            echo 'Deployment triggered!';
        } else {
            echo 'No deployment for this branch.';
        }
        ?>
                

This script checks if the push event was for the main branch and, if so, pulls the latest code from GitHub. Place this file on your server at the webhook URL you specified in GitHub, and set permissions to allow the web server to run Git commands.

The Importance of Testing Before and After Deployment

Testing code thoroughly before deploying it to a live environment is crucial to maintaining a stable and functional website. Even minor changes can sometimes lead to unexpected issues, so it’s important to check that all new features and modifications work as intended before going live. Here’s why testing is essential:

Verifying the Site After an Update

After deploying changes to your live site, it’s equally important to verify that everything is working as expected in the live environment. The following checks are recommended to ensure a successful deployment:

Testing before and after deploying code is essential for delivering a reliable, high-quality user experience. By establishing a consistent testing routine, you can deploy changes confidently, knowing that your site remains functional and user-friendly.

Summary

Developing locally and pushing to live environments involves either manual file transfers (using FTP/SFTP) or automated deployments (using CI/CD or GitHub webhooks). While FTP/SFTP offers control over individual files, CI/CD workflows streamline deployments and reduce manual effort. With GitHub Actions or webhooks, you can create a reliable, automated deployment pipeline that triggers updates to your live site whenever you push code to your main branch.