Automating the deployment of your website on your VPS via GitHub Actions
Objective
Automating the deployment of your website on a VPS greatly simplifies the management of your updates. With GitHub Actions, you can configure an automatic deployment pipeline, avoiding manual deployments. This method ensures fast and reliable deployments, while reducing the risk of human errors. Whether you are a beginner or an experienced developer, this tutorial will help you set up a professional solution adapted to your needs.
Find out how to automate the deployment of your web applications with GitHub Actions on an OVHcloud VPS.
Requirements
- A functional VPS in your OVHcloud account
- An active GitHub account
- A repository containing your website code (optional)
- A VPS configured with the necessary services (e.g. Apache/Nginx, PHP, DBMS, etc.)
- Administrative access to the VPS (via SSH)
If you need assistance, please read our guide on Getting started with a VPS before reading this guide further.
Instructions
To ensure that you meet the requirements, please read the guides Installing a web development environment on a VPS or a dedicated server and Securing a VPS.
The main steps of the guide will be the following:
- Configure SSH access for GitHub Actions
- Add the private key to GitHub
- Initialize the GitHub repository (optional)
- Configure GitHub Actions for Automatic Deployment
- Check and test the GitHub Actions workflow
- Conclusion
Configure SSH access for GitHub Actions
If your website already exists, identify the path to the directory where it is hosted. For example, on an OVHcloud VPS, it can be /var/www/html. Keep this path in memory, so that you can use it when configuring the GitHub Actions pipeline.
To allow GitHub Actions to automatically deploy your website, configure secure SSH access to your VPS.
Create an SSH key pair
Log in to your VPS via SSH and generate a dedicated SSH key pair for GitHub Actions:
Replace <user> with the user configured to connect to your VPS.
Press Enter when a passphrase is requested (leaving the passphrase empty makes it easier to automate deployments with GitHub Actions. However, this requires securing the private key by limiting it to this use and storing it securely).
You get two files:
/home/<user>/.ssh/deploy_key: private key/home/<user>/.ssh/deploy_key.pub: public key
Configure the public key on the VPS
To allow GitHub Actions to connect to your VPS via SSH and deploy your website’s code to it, add the generated public key to the list of authorized keys on the VPS.
1. Create the .ssh directory:
2. Add the public key to the authorized_keys file:
3. Test the SSH connection
Test the SSH connection with the private key to confirm that the access is functional:
Replace <user> with the user configured to connect to your VPS and <VPS_IP> with your VPS IP.
Add the public key to GitHub
Once you have configured the public key on your VPS, add it to your GitHub account. Copy the content of the generated public key onto your VPS with:
Follow the steps from the "Adding a new SSH key to your account" section of the GitHub official documentation to add your public key to your GitHub account.
Configure SSH access to GitHub on the VPS
To ensure that GitHub uses the generated private key to establish a secure SSH connection, configure the ~/.ssh/config file. This step facilitates subsequent commands by avoiding the need to manually specify the private key each time you interact with GitHub.
On your VPS, create or modify the file ~/.ssh/config:
Add this configuration for GitHub access:
Save and exit the editor.
Test the SSH connection with GitHub:
You should see a message like this:
Add the private key to GitHub
Copy the content of the generated private key to your VPS with:
To allow GitHub Actions to automatically connect to your VPS, add the private key in a secret repository on GitHub. This will allow GitHub to deploy your website via SSH. Follow the steps from the "Creating secrets for a repository" section of the GitHub official documentation.
Initialize GitHub repository (optional)
If you already have a GitHub repository containing your website’s code, go to the next step.
Create a GitHub repository
To create a GitHub repository, follow the steps from the "Create a repository" page of the GitHub official documentation.
Initialize the Git repository on the VPS
1. Log in to your VPS via SSH:
2. Install Git:
3. Initialize a Git repository in your website directory:
Replace <github_user> with your GitHub username and <repository_name> with the name of your GitHub repository.
4. Add the files and make a first commit:
Configure GitHub Actions for automatic deployment
Create and configure a workflow to automatically synchronize your website code between GitHub and the VPS.
Create the GitHub Actions workflow file
1. Create a directory for workflows
On your VPS, create the directory .github/workflows in the folder containing your Git project (i.e. the directory containing your website’s code):
2. Create a workflow file
Create a deploy.yml file in the .github/workflows directory:
3. Configure the deploy.yml file
To configure the deployment pipeline, add the following content to the deploy.yml file:
Replace the following:
<VPS_IP>: by the IP address of your VPS.<user>: by the SSH user configured on your VPS.DEPLOY_KEY: by the secret name in your GitHub repository settings.
4. Add the workflow file to the GitHub repository
Once the workflow file has been configured, add it to your Git repository and push it to GitHub:
Check and test the GitHub workflow Actions
Check the execution of the first workflow
Go to the Actions tab of your GitHub repository and check that your first workflow has run correctly.
If an error occurs, click the failed workflow to view the logs. Ensure that your private key is correctly added as a secret in your GitHub repository, and that your public key is added to the .ssh/authorized_keys file.
Insufficient permissions
During the first deployment, errors can occur concerning permissions (Permission denied (13), rsync: failed to set times, etc.)
1. Verify that the user has the necessary permissions
Ensure that the SSH user configured on your VPS has write permissions to the entire Git directory (/var/www/html) and its subdirectories:
2. Test locally with rsync
Before relaunching the GitHub Actions workflow, test the rsync command manually from your local machine. This will allow you to confirm that the permissions are correctly configured:
If this command succeeds, then restart the workflow on GitHub.
Trigger the Workflow with git push
When a git push is performed on the main branch (or any other branch specified in your deploy.yml file), the workflow executes the steps defined in the deploy.yml file:
- Cloning the GitHub repository in the GitHub Actions environment.
- Configuring the SSH key to connect to your VPS.
- Synchronize files from the GitHub repository to the directory
/var/www/htmlon your VPS viarsync.
Test the workflow
1. Clone the GitHub repository in a test directory on the VPS
Create a temporary directory on your VPS to simulate another user environment. For example:
2. Clone the GitHub repository in this directory
If your repository is already in HTTPS , update it to use SSH:
3. Make a change in the test repository
Add a new file or modify an existing file in the test directory and perform a git push to your GitHub repository:
4. Check the workflow execution on GitHub
Go to the Actions tab of your GitHub repository and check that the workflow was triggered automatically after the git push. If the workflow is successful, the changes will be synchronized in your website’s folder (/var/www/html).
5. Confirm synchronization in /var/www/html
Return to your main deployment directory (/var/www/html) and check that the testfile.txt file is present:
Conclusion
By following this guide, you have set up an automatic deployment pipeline from your GitHub repository to your OVHcloud VPS using GitHub Actions. This workflow greatly optimizes the management of updates to your website, eliminating time-consuming manual deployments.
Go further
For specialized services (SEO, development, etc.), contact the OVHcloud partners.
Join our community of users.
-
Secure Shell (SSH) : un protocole de réseau sécurisé utilisé pour établir des connexions entre un client et un serveur. Il permet d'exécuter des commandes à distance de manière sécurisée. ↩