Auto deployment with GitHub Actions for cPanel or VPS 🚀
For developers like us, working with Github is a daily task, having to work on it regularly. Most of us only care about Github's main functions such as Pull requests, Issues, or code, and rarely pay attention to Github's other functions. In today's article, Hion Coding introduces you to Github's Actions function and uses it to deploy projects to GitHub Pages. So what is GitHub Action? How to use it? Let's find out together
A small note is that those of you working with cPanel can also follow these steps to set up auto deploy for your project.
Some of the things we are going to cover in this tutorial.
- Generating an ssh key on your remote VPS or Terminal cPanel
- Adding your generated public key to authorized keys
- Creating GitHub secret keys
- Configuring GitHub actions to auto-deploy your private/public repository
Step 1 - Open your terminal and add ssh into your VPS
ssh user@hostname
cd ~/.ssh
Step 2 - Generate an SSH key
ssh-keygen -t rsa -b 4096 -C "test@example.com"
The email is the one you use on your GitHub account
Step 3 - Add a public key to authorized keys
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
Note: We’re using >> so that the id_rsa.pub contents are appended to the end of the contents in the authorized_keys file, rather than override the contents in the authorized_keys.
Step 4 - Create GitHub secrets
cat ~/.ssh/id_rsa
-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEAwBnywW0YOMVlsMzEQEFAtCZK7tL8fE/f8cmVH57PS42DtkB0
0CxWdU2VO0XZJtvFwTbJmZ/SwLUfUS2rsL0RnjXN25lpbT59EluFClN2kRkZp5w+
Xn3ygrvE2t+j8zjF2JgfwZ2zS7Dr8v0DPg79CRVjU0XwrC1Pf4D4Pvzgf7CqjxUQ
...
...
4SeULN9pM2wAn1SoZHvX0QvVSlY5/hboZm6TF2MIPY/HTwNHoKlZJRrhXY1VhzMn
oWfOQObmxJ9BUIZRckhRTxFMp8VQ3v1mp4IQVQKBgQCkDQyvow+Vvgk/WlTKR3jn
kfUcNhMzCz4sqjj5/ZKGRXGzVewBMBK/59wUKxyvz5+QyFvE9weznp4mZahq7q0l
...
...
QmKqBQKBgQC4W/xwQz+EuSOVD4qfn5f6AWZT9/ZJ4yB2N6R+VwPdsfsDvN2RAhU2
hfmQpV5xW9n+huI3Z2I6RFj/Yq8Nc5l1A8Nd13Z18KBaYz1vbl64iI/+3f6uWyr+
sNm91dWnYV4R8ghf7W9dk19X3pDnXHQtIWVJdPjMN6PQcGoxo4LK8w==
-----END RSA PRIVATE KEY-----
Head over to your GitHub repository you wish to configure, click on settings tab then in options menu click on and add the following secrets:
-
HOST: set the key to your hostname or ip address.
-
USERNAME: set the key to the username you use to SSH into your VPS.
-
SSHKEY: set the key to you copied contents from the command above.
-
PORT: set the key to 22
Step 5 - Configure GitHub actions to auto-deploy your private/public repository
Assuming that you have the repo cloned locally on your machine, go ahead and create .github/workflows
folder and inside that create a deploy.yml
file
Add the following contents to deploy.yml
file
Note: Below is the command 'cd test-deploy.hioncoding.com && git pull origin'
: I want to cd to the directory where I need to pull the code. You can change according to your actions ~.~'
If you want to pull code without requiring an account or password, you can refer to the SSH Github config article here.
name: SSH Pull Code
on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Setup SSH key
uses: longaodai/ssh-server@v2.0.0
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
ssh-private-key: ${{ secrets.SSHKEY }}
commands: 'cd test-deploy.hioncoding.com && git pull origin'
Commit the deploy.yml changes, and push to your repository.
It should build and push to the VPS without any errors.
git add .
git commit -m "deploy"
git push origin master
Thank you for reading!
Hion Coding! Good luck!