Deploy code on VPS with Gihub Actions via rsync

In one of my previous articles, I’ve demonstrated how to deploy code on a VPS server using CircleCI. In this article, I’ll show how to do a similar process, but only using Github Actions instead.

Tip: Here is also a nice article that you may like, for WordPress Delevopers, showing how to publish plugins in the wp.org repository using Github Actions. See it…

Let’s move on.

You’ll need to have a set of public and private ssh keys to continue. In case you don’t have them, follow step 1 to generate them.

Step 1: Generate the SSH keys.

You can jump to step 2 if you already have the keys.

Login to VPS using SSH.

Replace <SERVER_ADDRESS> with the right URL or IP to your VPS.

ssh root@<SERVER_ADRRESS>

Generate the ssh key.

ssh-keygen -t rsa

Leave the passphrase empty.

Step 2: Copy the public key to authorized_keys

On VPS, you must copy the public SSH key to ~/.ssh/authorized_keys .

If you generated it by following step 1, do this by running this command.

cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys

Step 3: Copy the private key to clipboard

If you generated it on VPS, copy this key using any of your preferred methods. For example, you may use xclip, but I’ll just open the file with nano and copy the contents manually. 😉

nano ~/.ssh/id_rsa

Tip:
If you have problems with the key being in an incorrect format and an error like this on the “Deploy” step:
Load key “/home/runner/.ssh/deploy_key”: invalid format,
then this error usually happens because of line endings having “CR LF” instead of only “LF“. You can easily convert this in any major text editor.
Learn more how to fix this.

Tip: Use the following command from your local terminal to copy the key:

scp root@<SERVER_ADRRESS>:~/.ssh/id_rsa /where/to/put

Step 4: Attach the private key to the repository on Github

Go to Settings -> Secrets and create two secret variables.

  1. DEPLOY_KEY: Is the private key, which you copied in the previous step.
  2. SEVER_IP: Is the server IP or address.

Step 5: Setup the Github Actions Workflow

In the root of your repository, create the following file .github/workflows/deploy.yml with this content:

name: Deploy

on:
    push:
        tags:
            - "*.*.*"
jobs:
    build:
        runs-on: ubuntu-latest
        timeout-minutes: 30
        steps:
            -   run: ls -la
            -   uses: actions/checkout@master

            -   run: sudo apt-get install rsync
           
            -   name: Deploy theme on server
                id: deploy_theme
                uses: Pendect/action-rsyncer@v1.1.0
                env:
                    DEPLOY_KEY: ${{secrets.DEPLOY_KEY}}
                with:
                    flags: '-avc --delete'
                    src: 'dist/'
                    dest: 'root@"${{secrets.SERVER_IP}}":/path/on/remote'

            -   name: Display status from deploy
                run: echo "${{ steps.deploy.outputs.status }}"

Before deploying the code on the server, you can run many commands that you may need. For example, to compile your code, to clean it, to test, etc.

A few aspects here. You must replace these lines:

  1. src: 'dist/' — The contents of this folder will be uploaded on server. You can change it to fit your needs.
  2. dest: 'root@"${{secrets.SERVER_IP}}":/path/on/remote' — Adjust the remote path as needed.

That’s it.

In case you have any questions, suggestions, or found an error, please let me know in the comments below. ✌

Member since January 2, 2019

As a seasoned WordPress developer with expertise in various tech stacks and languages, I bring years of experience to every project I handle. My passion for coding and dedication to delivering exceptional work ensures that each project I take on is of the highest quality. I specialize in creating custom themes, developing plugins, and building full-scale web systems. By staying up-to-date with the latest industry trends and best practices, I incorporate cutting-edge solutions into my work.

Comments

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