DEV Community

Dhruv Kumar
Dhruv Kumar

Posted on

Simplifying Deployment: Using GitHub Actions and SSH for Quick and Easy Updates

In the world of software development, deployment can often be a complex and daunting task, especially for those who are not familiar with tools like Docker or Kubernetes. However, there's an efficient and straightforward method for small-scale or quick deployments: using GitHub Actions combined with SSH. This approach is particularly useful for individuals who prefer a one-step deployment process without the need for extensive Docker knowledge.

Requirements
Before diving into the process, ensure you have the following:

  1. SSH Access: You need SSH permissions to the remote machine where your application is hosted. This access is crucial as it allows GitHub Actions to interact directly with your server.
  2. GitHub Account: Obviously, a GitHub account is required since you'll be using GitHub Actions for the deployment process.

Setting Up SSH Keys in GitHub

  1. Generate SSH Keys: If you haven't already, generate an SSH key pair on your local machine. Keep your private key secure and handy for the next steps.
  2. Add SSH Key to GitHub Repository: Navigate to your GitHub repository's settings. Under the 'Secrets and variables' section, you'll find an option to add new secrets.
  3. Create a Repository Secret: Name your new secret SSH_PRIVATE_KEY. Here, you will enter your private SSH key. This secret is what GitHub Actions will use to establish a secure SSH connection with your remote server.

The GitHub Actions Workflow
With your SSH key securely stored in your GitHub repository, the next step is to set up a GitHub Actions workflow. This workflow will define the steps that GitHub takes every time you push new changes to your repository.

  1. Create a Workflow File: In your repository, create a new file under .github/workflows/. You can name this file something descriptive, like deploy.yml.
  2. Define Workflow Steps: In this file, define the steps that your deployment process requires. This typically includes checking out your repository, setting up SSH keys, and running SSH commands to deploy your application on the remote server.
  3. Automate Deployment: With every git push to your repository, this workflow will trigger, automatically deploying your application using the steps you've defined.
name:  trigger Name
# Trigger this workflow on pushes to the specified branch
on:
  push:
    branches:
      - [your-branch-name] # Replace with your branch name
jobs:
  deploy:
    runs-on: ubuntu-latest # Run this job on the latest Ubuntu version

    steps:
      - name: Checkout
        uses: actions/checkout@v2 # Check out your repository code

      - name: SSH and run commands
        env:
          PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }} # Reference the private key stored in GitHub Secrets
        run: |
          echo "$PRIVATE_KEY" > private_key.pem # Write the private key to a file
          chmod 600 private_key.pem # Set the appropriate permissions for the key file

          # Establish an SSH connection and execute commands on the remote server
          ssh -o StrictHostKeyChecking=no -i private_key.pem user@your-server.com <<EOF
            # Your server commands go here. Replace these commands with those relevant to your project.

            cd  /home/azureuser/github/sirius
            source ../venv/bin/activate
            git checkout master
            git pull
            python3 manage.py makemigrations
            python3 migrate
            sudo systemctl restart vendosmart_api_gunicorn.service
            cd /home/azureuser/github/canopus-platform
            export NODE_OPTIONS=--max-old-space-size=4096
            cd /home/azureuser/github/canopus-platform
            git checkout main
            git pull
            git log --oneline -10
            # Add other commands if needed
            npm run test
            sudo service apache2 restart
          EOF

          rm -f private_key.pem # Remove the private key file after use for security
    permissions:
      contents: read
      actions: write
      id-token: write
Enter fullscreen mode Exit fullscreen mode

Developer Tip: Enhance User Experience with a Maintenance Page

When updating your web application, consider temporarily redirecting users to a maintenance page. This can be done by adjusting the Nginx or Apache configuration to point to a maintenance page during your deployment pipeline. Implementing this step ensures users have a pleasant experience, as they are informed about the ongoing updates rather than facing broken pages or unexpected errors. This practice is particularly useful in maintaining transparency and professionalism, contributing positively to user satisfaction.

Conclusion
This method of deploying updates not only automates the process but also enhances the user experience by incorporating a maintenance page. It's ideal for those looking for an efficient, Docker-free deployment strategy, particularly suitable for small-scale projects or quick deployments.

Top comments (0)