⌘K
Noting Found

View all related articles

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

    1ssh user@hostname

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

    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:

    1ssh-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

    1cat ~/.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:

    1cat ~/.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

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

    Inside deploy.yml add the following:

    1name: Deploy
    2 
    3on:
    4 push:
    5 branches:
    6 - main
    7jobs:
    8 build:
    9 
    10 runs-on: ubuntu-latest
    11 
    12 steps:
    13 - name: Checkout repository
    14 uses: actions/checkout@v2
    15 
    16 - name: Setup SSH connection
    17 uses: webfactory/[email protected]
    18 with:
    19 ssh-private-key: ${{ secrets.SSHKEY }}
    20 
    21 - name: Copy files into the server
    22 uses: appleboy/[email protected]
    23 with:
    24 host: ${{ secrets.HOST }}
    25 username: ${{ secrets.USERNAME }}
    26 key: ${{ secrets.SSHKEY }}
    27 source: ./
    28 target: /path/to/project
    29 
    30 - uses: shivammathur/setup-php@15c43e89cdef867065b0213be354c2841860869e
    31 with:
    32 php-version: '8.0'
    33 - uses: actions/checkout@v3
    34 with:
    35 ref: dev
    36 - name: Deploy
    37 uses: fifsky/ssh-action@master
    38 with:
    39 command: |
    40 cd /path/to/project
    41 # run any command you want.
    42 host: ${{ secrets.HOST }}
    43 port: ${{ secrets.PORT }}
    44 user: ${{ secrets.USERNAME }}
    45 key: ${{ secrets.SSHKEY}}
    46 args: "-tt -vvv"

    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

    1cd /path/to/file

    To get the base directory. Inside your project.

    1pwd

    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 | Technical Writer

    Oussama is an experienced full-stack web developer with a strong focus on Laravel. He's passionate about crafting web applications with Filament and the TALL Stack. With 8+ years of experience, and he's a dedicated open-source contributor.

    Comments

    Join our newsletter

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

    We care about the protection of your data. Read our Privacy Policy.