Automating DirectAdmin Server Deployments from GitHub Using GitHub Actions and Password-Based SSH Authentication

Automating deployments ensures that your website or application is always up to date with minimal manual effort. By integrating GitHub with DirectAdmin and using GitHub Actions for deployment, you can push changes to GitHub from your local machine and have them automatically deployed to your DirectAdmin server.

In this guide, we’ll demonstrate how to automate DirectAdmin Server deployments from GitHub using GitHub Actions and password-based SSH authentication.

Prerequisites

  • A GitHub account and repository for your website or application.
  • Access to a DirectAdmin server.
  • SSH password for a user account on your DirectAdmin server.
  • Git installed on your local machine.

Step 1: Create a GitHub Repository

If you don’t already have a repository for your project, let’s create one on GitHub.

  1. Go to GitHub and log in.
  2. In the upper-right corner, click the + button and select New repository.
  3. Name your repository (e.g., my-website), provide a description, and set it to private (or public).
  4. Click Create repository.

Step 2: Set Up SSH Password Authentication on DirectAdmin

Ensure your DirectAdmin server is ready for password-based SSH authentication.

  1. Log in to your DirectAdmin server:
ssh testuser@your-server-ip
  1. Verify that password-based SSH authentication is enabled. If it’s not, enable it in the SSH configuration file:
sudo nano /etc/ssh/sshd_config

Make sure the following settings are in place:

PasswordAuthentication yes
PubkeyAuthentication yes
  1. Save the file and restart the SSH service:
sudo systemctl restart ssh

Step 3: Set Up the Development Environment on Your Local Machine

Now, let’s set up a local development environment where you can make changes to your website or application and push them to GitHub.

Install Git (if it’s not already installed)

On macOS or Linux, open a terminal and run:

sudo apt install git # For Linux
brew install git # For macOS (with Homebrew installed)

For Windows, you can download Git from Git’s official website.

Clone the GitHub Repository

  1. Open a terminal (or Git Bash on Windows).
  2. Clone the repository you created on GitHub:
git clone git@github.com:your_username/my-website.git
  1. Navigate into the repository directory:
cd my-website

Create a File and Push it to GitHub

  1. Create a simple HTML file in the repository folder on your local machine:
echo "<h1>Welcome to my website</h1>" > index.html
  1. Add the file to Git:
git add index.html
  1. Commit the file:
git commit -m "Add index.html"
  1. Push the changes to GitHub:
git push origin main

Step 4: Store Your SSH Password in GitHub Secrets

To enable GitHub Actions to authenticate with your DirectAdmin server, store your SSH password securely in GitHub Secrets.

  1. Go to your GitHub repository.
  2. Navigate to Settings > Secrets and Variables > Actions.
  3. Click New repository secret.
  4. Name the secret SSH_PASSWORD and paste your SSH password into the value field.
  5. Click Add secret.

Step 5: Set Up GitHub Actions for Automatic Deployment

Now that your local machine and GitHub repository are set up, you can configure GitHub Actions to automatically deploy changes to your DirectAdmin server whenever you push code to GitHub.

  1. In your local repository, create a new directory for your GitHub Actions workflows:
mkdir -p .github/workflows
  1. Inside this directory, create a workflow file called deploy.yml:
nano .github/workflows/deploy.yml
  1. Add the following content to the file:
name: Deploy to DirectAdmin

on:
  push:
    branches:
      - main

jobs:
  deploy:
    runs-on: ubuntu-latest

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

      - name: Install SSH and sshpass
        run: |
          sudo apt-get update
          sudo apt-get install -y openssh-client sshpass

      - name: Deploy to DirectAdmin server using password authentication
        run: |
          sshpass -p "${{ secrets.SSH_PASSWORD }}" ssh -o StrictHostKeyChecking=no testuser@your-server-ip 'cd /home/testuser/public_html && git pull origin main'
  1. Push the GitHub Actions workflow to GitHub:
git add .github/workflows/deploy.yml
git commit -m "Add GitHub Actions deployment workflow"
git push origin main

Step 6: Test the Deployment

Now that everything is set up, let’s test the deployment process.

  1. Make a change in your local machine by editing index.html:
echo "<h1>Updated website content</h1>" > index.html
  1. Commit and push the changes to GitHub:
git add index.html
git commit -m "Update website content"
git push origin main
  1. Check GitHub Actions:
  2. Go to the Actions tab in your GitHub repository.
  3. You should see the workflow running, automatically deploying the changes to your DirectAdmin server.
  4. Verify the changes:
  5. Visit your website to confirm that the updated content has been deployed.

Conclusion

By following this guide, you have successfully set up an automated deployment process from your local development environment to a DirectAdmin server using GitHub Actions and password-based SSH authentication. Consider switching to SSH key-based authentication for enhanced security.

Leave a Reply

Your email address will not be published. Required fields are marked *