Skip to content

Commit d794800

Browse files
committed
en-gb version
1 parent 26a50d3 commit d794800

File tree

3 files changed

+358
-5
lines changed

3 files changed

+358
-5
lines changed
Lines changed: 353 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,353 @@
1+
---
2+
title: "Automating the deployment of your website on your OVHcloud VPS via GitHub Actions"
3+
excerpt: 'Find out how to deploy and automate your website’s code via GitHub Actions on an OVHcloud VPS'
4+
updated: 2025-01-21
5+
---
6+
7+
## Objective
8+
9+
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 deployment, while reducing the risk of human error. Whether you are a beginner or an experienced developer, this tutorial will help you set up a professional solution adapted to your needs.
10+
11+
**Find out how to automate the deployment of your web applications with GitHub Actions on an OVHcloud VPS.**
12+
13+
## Requirements
14+
15+
- A functional [VPS](/links/bare-metal/vps) in your OVHcloud account
16+
- An active GitHub account
17+
- A repository containing your website code (optional)
18+
- A VPS configured with the necessary services (e.g. Apache/Nginx, PHP, DBMS, etc.)
19+
- Administrative access to the VPS (via SSH)
20+
21+
> [!warning]
22+
>
23+
> If you need assistance, please read our guide on [Getting started with a VPS](/pages/bare_metal_cloud/virtual_private_servers/starting_with_a_vps) before reading this guide further.
24+
25+
## Instructions
26+
27+
> [!primary]
28+
> To ensure that you meet the requirements, please read the guides “[Installing a web development environment on a VPS or a dedicated server](/pages/bare_metal_cloud/virtual_private_servers/install_env_web_dev_on_vps)” and “[Securing a VPS](/pages/bare_metal_cloud/virtual_private_servers/secure_your_vps)”.
29+
30+
## Contents
31+
32+
- [Configure SSH access for GitHub Actions](#configure-ssh)
33+
- [Add private key to GitHub](#add-private-key-github)
34+
- [Initialize GitHub repository (optional)](#init-github-repo)
35+
- [Configure GitHub Actions for Automatic Deployment](#configure-github-actions)
36+
- [Check and test the GitHub Actions workflow](#verify-workflow-github)
37+
- [Conclusion](#conclusion)
38+
39+
### Configure SSH access for GitHub Actions <a name="configure-ssh"></a>
40+
41+
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 to use when configuring the Actions GitHub pipeline.
42+
43+
To allow GitHub Actions to automatically deploy your website, configure secure SSH access to your VPS.
44+
45+
#### Create an SSH key pair
46+
47+
Log in to your VPS via SSH and generate a dedicated SSH key pair for GitHub Actions:
48+
49+
```bash
50+
ssh-keygen -t rsa -b 4096 -C "github-actions" -f /home/<user>/.ssh/deploy_key
51+
```
52+
53+
Replace `<user>` with the user configured to connect to your VPS.
54+
55+
Press `Enter` when a passphrase is requested (leaving the passphrase empty makes it easier to automate deployment with GitHub Actions. However, this requires securing the private key by limiting it to this use and storing it securely).
56+
57+
You get two files:
58+
59+
- `/home/<user>/.ssh/deploy_key`: private key
60+
- `/home/<user>/.ssh/deploy_key.pub`: public key
61+
62+
#### Configure the public key on the VPS
63+
64+
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.
65+
66+
1\. Create `.ssh` directory:
67+
68+
```bash
69+
mkdir -p /home/<user>/.ssh
70+
chmod 700 /home/<user>/.ssh
71+
```
72+
73+
2\. Add public key to `authorized_keys` file:
74+
75+
```bash
76+
cat /home/<user>/.ssh/deploy_key.pub >> /home/<user>/.ssh/authorized_keys
77+
chmod 600 /home/<user>/.ssh/authorized_keys
78+
```
79+
80+
3\. Test the SSH connection
81+
82+
Test the SSH connection with the private key to confirm that the access is functional:
83+
84+
```bash
85+
ssh -i /home/<user>/.ssh/deploy_key <user>@<VPS_IP>
86+
```
87+
88+
Replace `<user>` with the user configured to connect to your VPS and `<VPS_IP>` with your VPS IP.
89+
90+
#### Add the public key to GitHub
91+
92+
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:
93+
94+
```bash
95+
cat /home/<user>/.ssh/deploy_key.pub
96+
```
97+
98+
Follow the steps in the “Adding a new SSH key to your account” section of [GitHub official documentation](https://docs.github.com/gb/en/authentication/connecting-to-github-with-ssh/adding-a-new-ssh-key-to-your-github-account#adding-a-new-ssh-key-to-your-account) to add your public key to your GitHub account.
99+
100+
#### Configure SSH access to GitHub on the VPS
101+
102+
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.
103+
104+
On your VPS, create or modify the file `~/.ssh/config`:
105+
106+
```bash
107+
nano ~/.ssh/config
108+
```
109+
110+
Add this configuration for GitHub access:
111+
112+
```console
113+
Host github.com
114+
HostName github.com
115+
User git
116+
IdentityFile /home/<user>/.ssh/deploy_key
117+
```
118+
119+
Save and exit the editor.
120+
121+
Test the SSH connection with GitHub:
122+
123+
```bash
124+
125+
```
126+
127+
You should see a message like this:
128+
129+
```console
130+
Hi <user-github>! You've successfully authenticated, but GitHub does not provide shell access.
131+
```
132+
133+
### Add private key to GitHub <a name="add-private-key-github"></a>
134+
135+
Copy the content of the generated private key to your VPS with:
136+
137+
```bash
138+
cat /home/<user>/.ssh/deploy_key
139+
```
140+
141+
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 in the “Creating secrets for a repository” section of the [GitHub official documentation](https://docs.github.com/gb/en/actions/security-for-github-actions/security-guides/using-secrets-in-github-actions#creating-secrets-for-a-repository).
142+
143+
### Initialize GitHub repository (optional) <a name="init-github-repo"></a>
144+
145+
> [!primary]
146+
> If you already have a GitHub repository containing your website’s code, go to the [next step](#configure-github-actions).
147+
148+
#### Create a GitHub repository
149+
150+
To create a GitHub repository, follow the steps on the Create a repository page in the [GitHub official documentation](https://docs.github.com/gb/en/repositories/creating-and-managing-repositories/creating-a-new-repository){.external}.
151+
152+
#### Initialize upload on the VPS
153+
154+
1\. Log in to your VPS via SSH:
155+
156+
```bash
157+
ssh <user>@<VPS_IP>
158+
```
159+
160+
2\. Install Git:
161+
162+
```bash
163+
sudo apt update && sudo apt install git -y
164+
```
165+
166+
3\. Initialize a Git repository in your website directory:
167+
168+
```bash
169+
cd /var/www/html
170+
sudo git init
171+
sudo git remote add origin [email protected]:<github_user>/<repository_name>.git
172+
```
173+
174+
Replace `<github_user>` with your GitHub username and `<repository_name>` with the name of your GitHub repository.
175+
176+
4\. Add the files and make a first commit:
177+
178+
```bash
179+
git add .
180+
git commit -m "Initial commit"
181+
git branch -M main
182+
git push -u origin main
183+
```
184+
185+
### Configure GitHub Actions for automatic deployment <a name="configure-github-actions"></a>
186+
187+
Create and configure a workflow to automatically synchronize your website code between GitHub and the VPS.
188+
189+
#### Create the GitHub Actions workflow file
190+
191+
1\. Create a directory for workflows
192+
193+
On your VPS, create the directory `.github/workflows` in the folder containing your Git project (i.e. the directory containing your website’s code):
194+
195+
```bash
196+
cd /var/www/html
197+
mkdir -p .github/workflows
198+
```
199+
200+
2\. Create a workflow file
201+
202+
Create a `deploy.yml` file in the `.github/workflows` directory:
203+
204+
```bash
205+
nano .github/workflows/deploy.yml
206+
```
207+
208+
3\. Configure the `deploy.yml` file.
209+
210+
To configure the deployment pipeline, add the following content to the `deploy.yml` file:
211+
212+
```yaml
213+
name: Deploy to VPS
214+
215+
on:
216+
push:
217+
branches:
218+
- main
219+
220+
jobs:
221+
deploy:
222+
runs-on: ubuntu-latest
223+
224+
steps:
225+
- name: Checkout code
226+
uses: actions/checkout@v3
227+
228+
- name: Copy files to VPS
229+
env:
230+
SSH_PRIVATE_KEY: ${{ secrets.DEPLOY_KEY }}
231+
run: |
232+
mkdir -p ~/.ssh
233+
echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa
234+
chmod 600 ~/.ssh/id_rsa
235+
ssh-keyscan -H <VPS_IP> >> ~/.ssh/known_hosts
236+
rsync -avz --delete ./ <user>@<VPS_IP>:/var/www/html/
237+
```
238+
239+
Replace the following:
240+
241+
- `<VPS_IP>`: by the IP address of your VPS.
242+
- `<user>`: by the SSH user configured on your VPS.
243+
- `DEPLOY_KEY`: by the secret name in your GitHub repository settings.
244+
245+
4\. Add the workflow file to the GitHub repository
246+
247+
Once the workflow file has been configured, add it to your Git repository and push it to GitHub:
248+
249+
```bash
250+
git add .github/workflows/deploy.yml
251+
git commit -m "Adding GitHub Actions workflow for deployment"
252+
git push origin main
253+
```
254+
255+
### Check and test the GitHub workflow Actions <a name="verify-workflow-github"></a>
256+
257+
#### Check the execution of the first workflow
258+
259+
Go to the `Actions` tab of your GitHub repository and check that your first workflow has run correctly.
260+
261+
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.
262+
263+
##### **Insufficient permissions**
264+
265+
During the first deployment, errors can occur concerning permissions (`Permission denied (13)`, `rsync: failed to set times`, etc.)
266+
267+
1\. Verify that the user has the necessary permissions
268+
269+
Ensure that the SSH user configured on your VPS has write permissions to the entire Git directory (`/var/www/html`) and its subdirectories:
270+
271+
```bash
272+
sudo chown -R <user>:www-data /var/www/html
273+
sudo chmod -R 775 /var/www/html
274+
```
275+
276+
2\. Test locally with rsync
277+
278+
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:
279+
280+
```bash
281+
rsync -avz --no-times --exclude='.git*' -e "ssh -i ./deploy_key -o StrictHostKeyChecking=no" ./ <user>@<VPS_IP>:/var/www/html/
282+
```
283+
284+
If this command succeeds, then restart the workflow on GitHub.
285+
286+
#### Workflow triggered with `git push`
287+
288+
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:
289+
290+
- Cloning the GitHub repository in the GitHub Actions environment.
291+
- Configuring the SSH key to connect to your VPS.
292+
- Synchronize files from the GitHub repository to the directory `/var/www/html' on your VPS via `rsync'.
293+
294+
##### **Test the workflow**
295+
296+
1\. Clone the GitHub repository in a test directory on the VPS
297+
298+
Create a temporary directory on your VPS to simulate another user environment. For example:
299+
300+
```bash
301+
mkdir /home/<user>/test-github-actions
302+
cd /home/<user>/test-github-actions
303+
```
304+
305+
2\. Clone the GitHub repository in this directory
306+
307+
```bash
308+
git clone [email protected]:<github_user>/github-actions.git .
309+
```
310+
311+
If your repository is already in HTTPS , update it to use SSH:
312+
313+
```bash
314+
git remote set-url origin [email protected]:<github_user>/github-actions.git
315+
```
316+
317+
3\. Make a change in the test repository
318+
319+
Add a new file or modify an existing file in the test directory and push a git to your GitHub repository:
320+
321+
```bash
322+
echo "Test from VPS user number 2" >> testfile.txt
323+
git add testfile.txt
324+
git commit -m "Add a test from the VPS"
325+
git push origin main
326+
```
327+
328+
4\. Check workflow execution on GitHub
329+
330+
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`).
331+
332+
5\. Confirm synchronization in `/var/www/html`
333+
334+
Return to your main deployment directory (`/var/www/html`) and check that the `testfile.txt` file is present:
335+
336+
```bash
337+
ls /var/www/html
338+
cat /var/www/html/testfile.txt
339+
```
340+
341+
### Conclusion <a name="conclusion"></a>
342+
343+
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.
344+
345+
## Go further <a name="go-further"></a>
346+
347+
[Getting started with a VPS](/pages/bare_metal_cloud/virtual_private_servers/starting_with_a_vps)
348+
349+
[Secure a VPS](/pages/bare_metal_cloud/virtual_private_servers/secure_your_vps)
350+
351+
For specialized services (SEO, development, etc.), contact the [OVHcloud partners](/links/partner).
352+
353+
Join our [community of users](/links/community).

pages/bare_metal_cloud/virtual_private_servers/deploy-website-github-actions/guide.fr-fr.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "Automatiser le déploiement de votre site web sur votre VPS OVHcloud via GitHub Actions"
33
excerpt: 'Découvrez comment déployer et automatiser le code de votre site web via GitHub Actions sur un VPS OVHcloud'
4-
updated: 2024-12-16
4+
updated: 2025-01-21
55
---
66

77
## Objectif
@@ -56,8 +56,8 @@ Appuyez sur `Entrée` lorsqu'une passphrase vous est demandée (laisser la passp
5656

5757
Vous obtenez deux fichiers :
5858

59-
- /home/<user>/.ssh/deploy_key : clé privée
60-
- /home/<user>/.ssh/deploy_key.pub : clé publique
59+
- `/home/<user>/.ssh/deploy_key` : clé privée
60+
- `/home/<user>/.ssh/deploy_key.pub` : clé publique
6161

6262
#### Configurer la clé publique sur le VPS
6363

@@ -338,7 +338,7 @@ ls /var/www/html
338338
cat /var/www/html/testfile.txt
339339
```
340340

341-
### Conclusion
341+
### Conclusion <a name="conclusion"></a>
342342

343343
En suivant ce guide, vous avez mis en place un pipeline de déploiement automatique entre votre dépôt GitHub et votre VPS OVHcloud à l’aide de GitHub Actions. Ce workflow optimise considérablement la gestion des mises à jour de votre site web, en éliminant les déploiements manuels chronophages.
344344

pages/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@
268268
+ [How to install WordPress with Docker on a VPS or a dedicated server](bare_metal_cloud/virtual_private_servers/install_wordpress_docker_on_vps)
269269
+ [How to install WordPress with WP-CLI on a VPS or a dedicated server](bare_metal_cloud/virtual_private_servers/install_wordpress_site_on_vps)
270270
+ [How to migrate a website from a VPS to a Dedicated Server or a Public Cloud instance](bare_metal_cloud/virtual_private_servers/migrate-to-pci-or-dedicated-server)
271-
+ [How to automate the deployment of your website on your OVHcloud VPS via GitHub Actions](bare_metal_cloud/virtual_private_servers/deploy-website-github-actions)
271+
+ [Automating the deployment of your website on your OVHcloud VPS via GitHub Actions](bare_metal_cloud/virtual_private_servers/deploy-website-github-actions)
272272
+ [Managed Bare Metal](products/bare-metal-cloud-managed-bare-metal)
273273
+ [OVHcloud services and options](bare-metal-cloud-managed-bare-metal-ovhcloud-services-and-options)
274274
+ [Setting up a VPN for OVHcloud Zerto DRP](bare_metal_cloud/managed_bare_metal/zerto-virtual-replication-customer-to-ovhcloud)

0 commit comments

Comments
 (0)