my alt

How to Deploy Your Code Directly to a Server Using GitHub Actions

This article provides a detailed guide on how to deploy your code directly to a server using GitHub Actions. It covers the prerequisites, creating an SSH user, identifying the server folder path, setting up your GitHub repository, creating a secret with the private SSH key, and creating a GitHub workflow.

Prerequisites

Before we start, make sure you have the following:

  • An SSH user with read and write rights on your server’s folder/files.
  • The path of the server folder where you want the code to reside.
  • A GitHub repository with the code you want to deploy.

Step 1: Create an SSH User

First, you need to create an SSH user who has read and write rights on your server’s folders and files. This user will be responsible for deploying the code to the server. You can follow this guide to create and upload the SSH (public) key to your server. Depending on the Hosting Provider you use the process might differ.

Step 2: Get the Server Folder Path

Next, you need to identify the path of the server folder where you want the code to reside. This is typically a directory in your web server’s root directory. You can use the pwd command in the terminal to print the current working directory.

Step 3: Set Up Your GitHub Repository

Now, you need to have a GitHub repository (it can be a private repository) with the code you want to deploy. If you don’t have one already, you can follow this guide to create a new repository.

Step 4: Create a Secret with the Private SSH Key

Next, you need to create a secret in your GitHub repository with the private SSH key of the user. This secret will be used by GitHub Actions to authenticate with your server. You can follow this guide to create a new secret.

Step 5: Create a GitHub Workflow

Finally, you need to create a GitHub workflow that will handle the deployment process. This involves creating a new file in the .github/workflows directory of your repository, let’s call it deploy.yml. The contents of this file will look something like this:

on:
  push:
    branches:
      - main
  workflow_dispatch:

jobs:
  run_pull:
    name: Deploy
    runs-on: ubuntu-latest

    steps:
    - name: Checkout code
      uses: actions/checkout@v2
    - name: Install ssh keys
      run: |
        install -m 600 -D /dev/null ~/.ssh/id_rsa
        echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa
        ssh-keyscan -H ${{ secrets.SSH_HOST }} > ~/.ssh/known_hosts
    - name: Connect and pull
      run: rsync -r --delete ${{ github.workspace }}/html ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }}:${{ secrets.WORK_DIR }}
    - name: Cleanup
      run: rm -rf ~/.ssh

Notice how in this workflow file we used two more secrets: {{ secrets.SSH_HOST }}:${{ secrets.WORK_DIR }}. These just represent our Server IP and the path to the server folder. You can choose to use secrets for this, which will avoid the IP and path being visible in the file, or just use the IP and path, if this is not a concern for you

This workflow will run whenever you push to the main branch or manually trigger it. It will checkout your code, install the SSH keys, connect to your server, and deploy the code. Then, it will clean up after itself and remove the SSH key it installed.

Things to Be Careful With When Using This Workflow

While the process of deploying code directly to a server using GitHub Actions is quite straightforward, there are a few things you should be careful with to ensure smooth and secure operations.

1. Protect Your SSH Keys

SSH keys are a crucial part of this workflow as they allow GitHub Actions to authenticate with your server. It’s essential to keep these keys secure. Never expose your private SSH key in your code or anywhere public. Always use GitHub’s secrets to store these keys securely.

2. Be Aware of the Deployment Branch

In the provided workflow, the deployment is triggered whenever you push to the main branch. Be cautious about what you push to this branch, as it will be deployed to your server automatically. It’s a good practice to have a separate development branch for testing and only merge it with the main branch when you’re sure the code is ready for deployment.

3. Handle Errors Gracefully

Errors can occur during the deployment process. It’s important to handle these errors gracefully to prevent them from causing bigger issues. Make sure to include error handling in your workflow and provide clear error messages to help you troubleshoot any issues that arise.

4. Keep Your Server Secure

While this workflow simplifies the deployment process, it doesn’t absolve you of the responsibility to keep your server secure. Regularly update your server’s software, monitor its activity, and follow best practices for server security.

5. Test Your Workflow

Before relying on this workflow for your deployments, make sure to test it thoroughly. Ensure that it works as expected and is able to handle different scenarios and edge cases. This will help you avoid any surprises during actual deployments.

By being aware of these points, you can use GitHub Actions to deploy your code directly to a server effectively and securely.

Conclusion

And that’s it! You’ve now set up a GitHub Actions workflow to deploy your code directly to a server. This will greatly simplify your deployment process and ensure that your server always has the latest version of your code. Happy coding!


Related Articles

Based on our Cosine Similarity Analysis
How to Deploy Your Code Directly to a Server Using GitHub Actions

Creating a Stellar GitHub Readme for Your Organization

A well-crafted GitHub Readme serves as the first point of contact for anyone visiting your organization's GitHub profile. It conveys the mission, values, and work of your organization, attracting more users and contributors to your projects. This guide provides a comprehensive approach to creating an effective GitHub Readme for your organization.

Read more

How to Deploy Your Code Directly to a Server Using GitHub Actions

Creating a WordPress Plugin to Generate QR Codes

This tutorial provides a step-by-step guide on creating a WordPress plugin that generates QR codes linking to your post URL. Using the phpqrcode library, you can accomplish this task without the need for a composer. All you need is a text file editor or a code editor, and about 30 minutes of your time.

Read more