2 easy steps to automate a deployment in a VPS with GitHub Actions

General 2 mins read

Table of Content

    Introduction

    If you had a VPS and you are using already GitHub or any platform using version control. Probably noticed the difficulty of pushing the changes to GitHub and update the code base in your VPS server.

    So, in this article we will take a look on how to automate things and makes your life easier by using GitHub Actions to deploy the changes into your server.

    Requisite

    Before we dive in this article you need to know:

    • GitHub Account.
    • Git installed in your machine
    • uses of Git & GitHub (basics at least).
    • VPS Server (Doesn't matter which distro you use)
    • how to ssh into your server using command line.

    Generate SSH Keys

    So, I'm assuming you are already known how to deal with git and GitHub, at least how to push modifications from your local machine into GitHub repository by command line or any GUI can help you to do that.

    Here are the steps to generate your SSH Key in your VPs (server)

    Step 01: Open your terminal/command line and ssh into your VPS

    ssh [email protected]
    

    The user is the username of your server (e.g. root) and the hostname is your public IP address of your server. For example:

    ssh [email protected]
    

    After accessing the VPs successfully, it's time to generate some keys.

    Step 02: Generate an SSH key:

    Inside your VPs server type the following:

    ssh-keygen -t rsa -b 4096 -C "[email protected]"
    
    • ssh-keygen it's for generate a new ssh key
    • -t is for Specifying the type of key to create
    • -b is for specifying the length of key and
    • -C- is for Requests changing the comment.

    Note: In "[email protected]" use your GitHub email. & Don't use passphrase

    Step 03: Add public key to authorized keys

    You must add the generated public key onto authroized_keys file

    cat ~/.ssh/ssh-name.pub >> ~/.ssh/authorized_keys
    
    • ssh-name it's what's called your generated ssh key (by default is id_rsa)
    • the >> it's to append the content of ssh-name.pub in authorized_keys file

    Note: BE CAREFUL when using >> and don't use > cause that cause an overwritten in your authorized_keys file and may cause issue or effecting other services in your VPS such as login without password onto VPS.

    Configure GitHub Actions

    After the configuration went successfully. It's time to configure Our GitHub Actions

    You must copy the private ssh key and paste it in safe place untile you paste it again into Github Secret settings.

    To get the value of your private ssh key type the following inside in your VPS:

    cat ~/.ssh/ssh-name
    

    Step 01: Create GitHub Secrets

    After copied the ssh private key follow these steps:

    • Access Your Project Repository
    • Go to Settings
    • In the settings go to Secrets
    • Create 4 New Secrets
      • HOST: the value is the IP of your server (e.g. 123.334.55.66)
      • PORT: the value is the ssh port (by default is: 22 unless you changed the default port number)
      • SSHKEY: the value is the private generated key
      • USERNAME: the value in this case is your VPS username (e.g. root)

    [IMAGE]

    Step 02: Create deploy.yml file

    To Configure GitHub actions to auto-deploy your private or public repository you must create deploy.yml file under . GitHub/workflows folders

    mkdir -p .github/workflows
    
    touch .github/workflows/deploy.yml
    

    Inside deploy.yml add the following:

    name: Deploy
    
    on: [push]
    
    jobs:
      build:
    
        runs-on: ubuntu-latest
    
        steps:
        - uses: actions/[email protected]
    
        - name: Copy repository contents via scp
          uses: appleboy/[email protected]
          env:
            HOST: ${{ secrets.HOST }}
            USERNAME: ${{ secrets.USERNAME }}
            PORT: ${{ secrets.PORT }}
            KEY: ${{ secrets.SSHKEY }}
          with:
            source: "."
            target: "/your/website/directory"
    
        - name: Executing remote command
          uses: appleboy/[email protected]
          with:
            host: ${{ secrets.HOST }}
            USERNAME: ${{ secrets.USERNAME }}
            PORT: ${{ secrets.PORT }}
            KEY: ${{ secrets.SSHKEY }}
            script: ls
    

    I think the script it's self-explanatory but if you want to know the directory of the target (your website) you can it via accessing the website folder and type

    cd /path/to/file
    

    To get the base directory. Inside your project.

    pwd
    

    So, After doing all that. It should build and push changes into your VPS without any errors.

    Conclusion

    In this article we learn together how to use GitHub actions to automate the deployment.

    If you have any questions just ask, and I'll be happy to answer them.

    If you like my content don't forget to follow me on Twitter and share this article with your friend.


    Related Tags

    About the Author

    Oussama's Profile Picture
    Oussama
    Full Stack Web Developer

    I'm a full stack web developer and telecommunications engineer who love to share knowledge and build stuff online!


    Comments

    Join Our Newsletter

    Subscribe to Our Newsletter and never miss our offers, latest news, Articles, etc.

    Our news letter sent once a week, every tuesday.