This guide provides step-by-step instructions to set up automated deployment for a static website (HTML, CSS, JS) hosted on a VPS using GitHub Actions. By the end of this guide, every push to your repository will trigger a workflow that automatically deploys your changes to the VPS.
- A VPS running Ubuntu with an accessible SSH port.
- A website hosted on your VPS at
/var/www/html/html-css-js. - A GitHub repository containing the website's code.
- Basic knowledge of SSH and command-line operations.
- Generate SSH Keys: Create an SSH key pair for secure communication between GitHub Actions and your VPS.
- Copy SSH Key to VPS: Add the public key to the VPS to allow passwordless access.
- Create GitHub Actions Workflow: Set up a workflow to automate the deployment process.
- Push Changes and Test Deployment: Push the workflow file and verify automatic deployment.
First, create an SSH key pair on your local machine to facilitate secure communication between GitHub Actions and your VPS.
ssh-keygen -t rsa -b 4096 -f ~/.ssh/github-actions-vps- When prompted, leave the passphrase empty by pressing Enter twice.
Copy the public SSH key (github-actions-vps.pub) to the VPS to allow GitHub Actions to connect.
ssh-copy-id -i ~/.ssh/github-actions-vps.pub -p 2222 danieloliveiradev@YOUR_VPS_IP- Replace
YOUR_VPS_IPwith the IP address of your VPS. - If using a different SSH port, make sure to specify it using the
-pflag (as shown with2222).
Add the SSH private key to GitHub so the workflow can use it.
- Open your GitHub repository.
- Go to Settings > Secrets and variables > Actions.
- Click on New repository secret.
- Name the secret
VPS_SSH_KEY. - Paste the content of
~/.ssh/github-actions-vps(the private key) into the value field. - Click Add secret.
Create a workflow file to automate deployment on push.
- In the root of your project, create a directory named
.github/workflows. - Inside the
workflowsfolder, create a file nameddeploy.yml. - Add the following content to
deploy.yml:
name: Deploy to VPS
on:
push:
branches:
- main # Adjust this to the branch you want to deploy from
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Copy files via SSH
env:
SSH_PRIVATE_KEY: ${{ secrets.VPS_SSH_KEY }}
run: |
mkdir -p ~/.ssh
echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
rsync -avz --delete --exclude='.git*' ./ danieloliveiradev@YOUR_VPS_IP:/var/www/html/html-css-js -e "ssh -p 2222 -o StrictHostKeyChecking=no"- Replace
YOUR_VPS_IPwith the IP address of your VPS. - Adjust the path
/var/www/html/html-css-jsif your project directory is different.
Push your changes to GitHub to trigger the workflow.
git add .
git commit -m "Add GitHub Actions workflow for automated deployment"
git push origin main- Navigate to the Actions tab in your GitHub repository.
- Verify that the "Deploy to VPS" workflow runs successfully.
After the workflow completes, SSH into your VPS to verify that the files have been updated.
ssh -p 2222 danieloliveiradev@YOUR_VPS_IP
cd /var/www/html/html-css-js
ls -laYou should see that the files have been updated with the latest changes.
-
Permission Denied: If you encounter a
Permission deniederror during the workflow execution, ensure that the SSH key has been correctly added to theauthorized_keyson the VPS. -
File Permissions: Ensure that the user on the VPS has the correct permissions for the project directory. You can change the ownership and permissions using:
sudo chown -R danieloliveiradev:danieloliveiradev /var/www/html/html-css-js sudo chmod -R 755 /var/www/html/html-css-js
You've successfully set up automatic deployment for your website using GitHub Actions. Every time you push to the specified branch (e.g., main), the changes will be deployed to your VPS automatically.
This documentation should help you or anyone else repeat the process in the future. Feel free to adjust it based on any specific nuances of your setup!