GitHub Actions Deploy through SSH


by snettscom
In this guide, we will show you how to deploy your code to a remote server using GitHub Actions through SSH.
GitHub Actions is a powerful tool that enables you to automate your software development workflows directly from your GitHub repository. By connecting GitHub Actions to your remote server through SSH, you can automate your deployment process and ensure that your code is always up to date.
The following step-by-step instructions will get you started with GitHub Actions and help you create a workflow that will deploy your code with ease.
Pre-requisites
Before you begin, you’ll need the following:
- A remote server that you want to deploy to
- An SSH key pair. You can generate a new key pair with the ssh-keygen command
- Access to the private key for the SSH key pair
Step 1: Add the public key to the remote server
To allow GitHub Actions to connect to your remote server, you’ll need to add the public key from your SSH key pair to the authorized keys on the remote server. To do this, follow these steps:
- Log in to your remote server using SSH.
- Create a new directory called
~/.ssh
if it doesn’t already exist:mkdir -p ~/.ssh
- Create a new file called
~/.ssh/authorized_keys
if it doesn’t already exist:touch ~/.ssh/authorized_keys
- Open the
~/.ssh/authorized_keys
file in a text editor and paste the contents of the public key file (~/.ssh/id_rsa.pub
by default) at the end of the file. - Save and close the
~/.ssh/authorized_keys
file.
Step 2: Configure GitHub Deploy Key
You’ll need to add the public key of your SSH key pair as a deploy key in your GitHub repository. To do this, follow these steps:
- Open your GitHub repository in your web browser.
- Under "Security" click on the "Deploy keys" tab item on the left side.
- Click on "Add deploy key".
- Give it any "Title" you wish.
- In the "Key" text area, paste the contents of the public key file (
~/.ssh/id_rsa.pub
by default). - Click on the "Add key" button.
Step 3: Configure GitHub Actions Secret
While on the Settings tab we will need to add the private key of our SSH key as a [GitHub Actions Secret](). Follow these steps to do that:
- Click on the "Secrets and variables" tab item on the left to expand it.
- Then click on "Actions" sub item.
- On the right side of the screen, click on the green "New repository secret" button.
- In the "Name" field, enter SSH_PRIVATE_KEY.
- In the "Value" field, paste the contents of the private key file (
~/.ssh/id_rsa
by default). - Click on the "Add secret" button.
Step 4: Create the Github Action Workflow
You’ll need to create a Github Action workflow that connects to your remote server using SSH and executes the necessary deployment commands. To do this, follow these steps:
-
Create a new file called
.github/workflows/deploy.yml
in the root directory of your Github repository or project folder if you are working locally. -
Open the
deploy.yml
file in a text editor. -
Copy and paste the following YAML code into the deploy.yml file:
name: Deploy to remote server on: push: branches: - main jobs: deploy: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v2 - name: Setup SSH Key uses: webfactory/ssh-agent@v0.5.0 with: ssh-private-key: ${{ secrets.DEPLOY_KEY }} - name: Deploy code uses: appleboy/ssh-action@master with: host: your-remote-server.com username: your-ssh-username key: ${{ secrets.SSH_PRIVATE_KEY }} script: | cd /var/www/my-app git pull origin main npm install npm run build
-
In the
with
section of theDeploy code step
, replaceyour-remote-server.com
with the hostname or IP address of your remote server, and replaceyour-ssh-username
with the username that you use to connect to your remote server. -
In the
script
section of theDeploy code step
, replace/var/www/my-app
with the path to your project on your remote server, and replace the deployment commands relevant for your project.
This workflow is triggered when a push is made to the main
branch. It checks out the code using actions/checkout@v2
action, and then sets up the SSH key using the webfactory/ssh-agent
action, finally it uses the appleboy/ssh-action
action to execute commands on the remote server.
The deployment commands in this example, changes directory to /var/www/my-app
, pulls the latest changes from the main
branch, installs the necessary packages and builds the application.
That’s it, thank you for following along. Please feel free to comment below for any questions or feedback. To learn more about GitHub Actions click here.
Recommended Posts

Getting Started with Laravel on Render
21st January 2023

How to Stay Organized with Microsoft Edge Vertical Tabs
9th March 2021

What is Growth Marketing? An Introductory Guide
19th February 2021