diff --git a/pages/bare_metal_cloud/virtual_private_servers/deploy-website-github-actions/guide.de-de.md b/pages/bare_metal_cloud/virtual_private_servers/deploy-website-github-actions/guide.de-de.md new file mode 100644 index 00000000000..2a3ef080c36 --- /dev/null +++ b/pages/bare_metal_cloud/virtual_private_servers/deploy-website-github-actions/guide.de-de.md @@ -0,0 +1,353 @@ +--- +title: "Automating the deployment of your website on your VPS via GitHub Actions" +excerpt: 'Find out how to deploy and automate your website’s code via GitHub Actions on an OVHcloud VPS' +updated: 2025-01-21 +--- + +## 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](/links/bare-metal/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) + +> [!warning] +> +> 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. + +## Instructions + +> [!primary] +> 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). + +**The main steps of the guide will be the following:** + +- [Configure SSH access for GitHub Actions](#configure-ssh) +- [Add the private key to GitHub](#add-private-key-github) +- [Initialize the GitHub repository (optional)](#init-github-repo) +- [Configure GitHub Actions for Automatic Deployment](#configure-github-actions) +- [Check and test the GitHub Actions workflow](#verify-workflow-github) +- [Conclusion](#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: + +```bash +ssh-keygen -t rsa -b 4096 -C "github-actions" -f /home//.ssh/deploy_key +``` + +Replace `` 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//.ssh/deploy_key`: private key +- `/home//.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: + +```bash +mkdir -p /home//.ssh +chmod 700 /home//.ssh +``` + +2\. Add the public key to the `authorized_keys` file: + +```bash +cat /home//.ssh/deploy_key.pub >> /home//.ssh/authorized_keys +chmod 600 /home//.ssh/authorized_keys +``` + +3\. Test the SSH connection + +Test the SSH connection with the private key to confirm that the access is functional: + +```bash +ssh -i /home//.ssh/deploy_key @ +``` + +Replace `` with the user configured to connect to your VPS and `` 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: + +```bash +cat /home//.ssh/deploy_key.pub +``` + +Follow the steps from the "Adding a new SSH key to your account" section of the [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. + +#### 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`: + +```bash +nano ~/.ssh/config +``` + +Add this configuration for GitHub access: + +```console +Host github.com + HostName github.com + User git + IdentityFile /home//.ssh/deploy_key +``` + +Save and exit the editor. + +Test the SSH connection with GitHub: + +```bash +ssh -T git@github.com +``` + +You should see a message like this: + +```console +Hi ! You've successfully authenticated, but GitHub does not provide shell access. +``` + +### Add the private key to GitHub + +Copy the content of the generated private key to your VPS with: + +```bash +cat /home//.ssh/deploy_key +``` + +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](https://docs.github.com/gb/en/actions/security-for-github-actions/security-guides/using-secrets-in-github-actions#creating-secrets-for-a-repository). + +### Initialize GitHub repository (optional) + +> [!primary] +> If you already have a GitHub repository containing your website’s code, go to the [next step](#configure-github-actions). + +#### Create a GitHub repository + +To create a GitHub repository, follow the steps from the "Create a repository" page of the [GitHub official documentation](https://docs.github.com/gb/en/repositories/creating-and-managing-repositories/creating-a-new-repository){.external}. + +#### Initialize the Git repository on the VPS + +1\. Log in to your VPS via SSH: + +```bash +ssh @ +``` + +2\. Install Git: + +```bash +sudo apt update && sudo apt install git -y +``` + +3\. Initialize a Git repository in your website directory: + +```bash +cd /var/www/html +sudo git init +sudo git remote add origin git@github.com:/.git +``` + +Replace `` with your GitHub username and `` with the name of your GitHub repository. + +4\. Add the files and make a first commit: + +```bash +git add . +git commit -m "Initial commit" +git branch -M main +git push -u origin main +``` + +### 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): + +```bash +cd /var/www/html +mkdir -p .github/workflows +``` + +2\. Create a workflow file + +Create a `deploy.yml` file in the `.github/workflows` directory: + +```bash +nano .github/workflows/deploy.yml +``` + +3\. Configure the `deploy.yml` file + +To configure the deployment pipeline, add the following content to the `deploy.yml` file: + +```yaml +name: Deploy to VPS + +on: + push: + branches: + - main + +jobs: + deploy: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Copy files to VPS + env: + SSH_PRIVATE_KEY: ${{ secrets.DEPLOY_KEY }} + run: | + mkdir -p ~/.ssh + echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa + chmod 600 ~/.ssh/id_rsa + ssh-keyscan -H >> ~/.ssh/known_hosts + rsync -avz --delete ./ @:/var/www/html/ +``` + +Replace the following: + +- ``: by the IP address of your VPS. +- ``: 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: + +```bash +git add .github/workflows/deploy.yml +git commit -m "Adding GitHub Actions workflow for deployment" +git push origin main +``` + +### 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: + +```bash +sudo chown -R :www-data /var/www/html +sudo chmod -R 775 /var/www/html +``` + +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: + +```bash +rsync -avz --no-times --exclude='.git*' -e "ssh -i ./deploy_key -o StrictHostKeyChecking=no" ./ @:/var/www/html/ +``` + +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/html` on your VPS via `rsync`. + +##### **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: + +```bash +mkdir /home//test-github-actions +cd /home//test-github-actions +``` + +2\. Clone the GitHub repository in this directory + +```bash +git clone git@github.com:/github-actions.git . +``` + +If your repository is already in HTTPS , update it to use SSH: + +```bash +git remote set-url origin git@github.com:/github-actions.git +``` + +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: + +```bash +echo "Test from VPS user number 2" >> testfile.txt +git add testfile.txt +git commit -m "Add a test from the VPS" +git push origin main +``` + +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: + +```bash +ls /var/www/html +cat /var/www/html/testfile.txt +``` + +### 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 + +[Getting started with a VPS](/pages/bare_metal_cloud/virtual_private_servers/starting_with_a_vps) + +[Securing a VPS](/pages/bare_metal_cloud/virtual_private_servers/secure_your_vps) + +For specialized services (SEO, development, etc.), contact the [OVHcloud partners](/links/partner). + +Join our [community of users](/links/community). \ No newline at end of file diff --git a/pages/bare_metal_cloud/virtual_private_servers/deploy-website-github-actions/guide.en-asia.md b/pages/bare_metal_cloud/virtual_private_servers/deploy-website-github-actions/guide.en-asia.md new file mode 100644 index 00000000000..2a3ef080c36 --- /dev/null +++ b/pages/bare_metal_cloud/virtual_private_servers/deploy-website-github-actions/guide.en-asia.md @@ -0,0 +1,353 @@ +--- +title: "Automating the deployment of your website on your VPS via GitHub Actions" +excerpt: 'Find out how to deploy and automate your website’s code via GitHub Actions on an OVHcloud VPS' +updated: 2025-01-21 +--- + +## 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](/links/bare-metal/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) + +> [!warning] +> +> 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. + +## Instructions + +> [!primary] +> 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). + +**The main steps of the guide will be the following:** + +- [Configure SSH access for GitHub Actions](#configure-ssh) +- [Add the private key to GitHub](#add-private-key-github) +- [Initialize the GitHub repository (optional)](#init-github-repo) +- [Configure GitHub Actions for Automatic Deployment](#configure-github-actions) +- [Check and test the GitHub Actions workflow](#verify-workflow-github) +- [Conclusion](#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: + +```bash +ssh-keygen -t rsa -b 4096 -C "github-actions" -f /home//.ssh/deploy_key +``` + +Replace `` 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//.ssh/deploy_key`: private key +- `/home//.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: + +```bash +mkdir -p /home//.ssh +chmod 700 /home//.ssh +``` + +2\. Add the public key to the `authorized_keys` file: + +```bash +cat /home//.ssh/deploy_key.pub >> /home//.ssh/authorized_keys +chmod 600 /home//.ssh/authorized_keys +``` + +3\. Test the SSH connection + +Test the SSH connection with the private key to confirm that the access is functional: + +```bash +ssh -i /home//.ssh/deploy_key @ +``` + +Replace `` with the user configured to connect to your VPS and `` 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: + +```bash +cat /home//.ssh/deploy_key.pub +``` + +Follow the steps from the "Adding a new SSH key to your account" section of the [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. + +#### 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`: + +```bash +nano ~/.ssh/config +``` + +Add this configuration for GitHub access: + +```console +Host github.com + HostName github.com + User git + IdentityFile /home//.ssh/deploy_key +``` + +Save and exit the editor. + +Test the SSH connection with GitHub: + +```bash +ssh -T git@github.com +``` + +You should see a message like this: + +```console +Hi ! You've successfully authenticated, but GitHub does not provide shell access. +``` + +### Add the private key to GitHub + +Copy the content of the generated private key to your VPS with: + +```bash +cat /home//.ssh/deploy_key +``` + +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](https://docs.github.com/gb/en/actions/security-for-github-actions/security-guides/using-secrets-in-github-actions#creating-secrets-for-a-repository). + +### Initialize GitHub repository (optional) + +> [!primary] +> If you already have a GitHub repository containing your website’s code, go to the [next step](#configure-github-actions). + +#### Create a GitHub repository + +To create a GitHub repository, follow the steps from the "Create a repository" page of the [GitHub official documentation](https://docs.github.com/gb/en/repositories/creating-and-managing-repositories/creating-a-new-repository){.external}. + +#### Initialize the Git repository on the VPS + +1\. Log in to your VPS via SSH: + +```bash +ssh @ +``` + +2\. Install Git: + +```bash +sudo apt update && sudo apt install git -y +``` + +3\. Initialize a Git repository in your website directory: + +```bash +cd /var/www/html +sudo git init +sudo git remote add origin git@github.com:/.git +``` + +Replace `` with your GitHub username and `` with the name of your GitHub repository. + +4\. Add the files and make a first commit: + +```bash +git add . +git commit -m "Initial commit" +git branch -M main +git push -u origin main +``` + +### 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): + +```bash +cd /var/www/html +mkdir -p .github/workflows +``` + +2\. Create a workflow file + +Create a `deploy.yml` file in the `.github/workflows` directory: + +```bash +nano .github/workflows/deploy.yml +``` + +3\. Configure the `deploy.yml` file + +To configure the deployment pipeline, add the following content to the `deploy.yml` file: + +```yaml +name: Deploy to VPS + +on: + push: + branches: + - main + +jobs: + deploy: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Copy files to VPS + env: + SSH_PRIVATE_KEY: ${{ secrets.DEPLOY_KEY }} + run: | + mkdir -p ~/.ssh + echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa + chmod 600 ~/.ssh/id_rsa + ssh-keyscan -H >> ~/.ssh/known_hosts + rsync -avz --delete ./ @:/var/www/html/ +``` + +Replace the following: + +- ``: by the IP address of your VPS. +- ``: 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: + +```bash +git add .github/workflows/deploy.yml +git commit -m "Adding GitHub Actions workflow for deployment" +git push origin main +``` + +### 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: + +```bash +sudo chown -R :www-data /var/www/html +sudo chmod -R 775 /var/www/html +``` + +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: + +```bash +rsync -avz --no-times --exclude='.git*' -e "ssh -i ./deploy_key -o StrictHostKeyChecking=no" ./ @:/var/www/html/ +``` + +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/html` on your VPS via `rsync`. + +##### **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: + +```bash +mkdir /home//test-github-actions +cd /home//test-github-actions +``` + +2\. Clone the GitHub repository in this directory + +```bash +git clone git@github.com:/github-actions.git . +``` + +If your repository is already in HTTPS , update it to use SSH: + +```bash +git remote set-url origin git@github.com:/github-actions.git +``` + +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: + +```bash +echo "Test from VPS user number 2" >> testfile.txt +git add testfile.txt +git commit -m "Add a test from the VPS" +git push origin main +``` + +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: + +```bash +ls /var/www/html +cat /var/www/html/testfile.txt +``` + +### 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 + +[Getting started with a VPS](/pages/bare_metal_cloud/virtual_private_servers/starting_with_a_vps) + +[Securing a VPS](/pages/bare_metal_cloud/virtual_private_servers/secure_your_vps) + +For specialized services (SEO, development, etc.), contact the [OVHcloud partners](/links/partner). + +Join our [community of users](/links/community). \ No newline at end of file diff --git a/pages/bare_metal_cloud/virtual_private_servers/deploy-website-github-actions/guide.en-au.md b/pages/bare_metal_cloud/virtual_private_servers/deploy-website-github-actions/guide.en-au.md new file mode 100644 index 00000000000..2a3ef080c36 --- /dev/null +++ b/pages/bare_metal_cloud/virtual_private_servers/deploy-website-github-actions/guide.en-au.md @@ -0,0 +1,353 @@ +--- +title: "Automating the deployment of your website on your VPS via GitHub Actions" +excerpt: 'Find out how to deploy and automate your website’s code via GitHub Actions on an OVHcloud VPS' +updated: 2025-01-21 +--- + +## 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](/links/bare-metal/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) + +> [!warning] +> +> 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. + +## Instructions + +> [!primary] +> 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). + +**The main steps of the guide will be the following:** + +- [Configure SSH access for GitHub Actions](#configure-ssh) +- [Add the private key to GitHub](#add-private-key-github) +- [Initialize the GitHub repository (optional)](#init-github-repo) +- [Configure GitHub Actions for Automatic Deployment](#configure-github-actions) +- [Check and test the GitHub Actions workflow](#verify-workflow-github) +- [Conclusion](#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: + +```bash +ssh-keygen -t rsa -b 4096 -C "github-actions" -f /home//.ssh/deploy_key +``` + +Replace `` 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//.ssh/deploy_key`: private key +- `/home//.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: + +```bash +mkdir -p /home//.ssh +chmod 700 /home//.ssh +``` + +2\. Add the public key to the `authorized_keys` file: + +```bash +cat /home//.ssh/deploy_key.pub >> /home//.ssh/authorized_keys +chmod 600 /home//.ssh/authorized_keys +``` + +3\. Test the SSH connection + +Test the SSH connection with the private key to confirm that the access is functional: + +```bash +ssh -i /home//.ssh/deploy_key @ +``` + +Replace `` with the user configured to connect to your VPS and `` 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: + +```bash +cat /home//.ssh/deploy_key.pub +``` + +Follow the steps from the "Adding a new SSH key to your account" section of the [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. + +#### 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`: + +```bash +nano ~/.ssh/config +``` + +Add this configuration for GitHub access: + +```console +Host github.com + HostName github.com + User git + IdentityFile /home//.ssh/deploy_key +``` + +Save and exit the editor. + +Test the SSH connection with GitHub: + +```bash +ssh -T git@github.com +``` + +You should see a message like this: + +```console +Hi ! You've successfully authenticated, but GitHub does not provide shell access. +``` + +### Add the private key to GitHub + +Copy the content of the generated private key to your VPS with: + +```bash +cat /home//.ssh/deploy_key +``` + +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](https://docs.github.com/gb/en/actions/security-for-github-actions/security-guides/using-secrets-in-github-actions#creating-secrets-for-a-repository). + +### Initialize GitHub repository (optional) + +> [!primary] +> If you already have a GitHub repository containing your website’s code, go to the [next step](#configure-github-actions). + +#### Create a GitHub repository + +To create a GitHub repository, follow the steps from the "Create a repository" page of the [GitHub official documentation](https://docs.github.com/gb/en/repositories/creating-and-managing-repositories/creating-a-new-repository){.external}. + +#### Initialize the Git repository on the VPS + +1\. Log in to your VPS via SSH: + +```bash +ssh @ +``` + +2\. Install Git: + +```bash +sudo apt update && sudo apt install git -y +``` + +3\. Initialize a Git repository in your website directory: + +```bash +cd /var/www/html +sudo git init +sudo git remote add origin git@github.com:/.git +``` + +Replace `` with your GitHub username and `` with the name of your GitHub repository. + +4\. Add the files and make a first commit: + +```bash +git add . +git commit -m "Initial commit" +git branch -M main +git push -u origin main +``` + +### 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): + +```bash +cd /var/www/html +mkdir -p .github/workflows +``` + +2\. Create a workflow file + +Create a `deploy.yml` file in the `.github/workflows` directory: + +```bash +nano .github/workflows/deploy.yml +``` + +3\. Configure the `deploy.yml` file + +To configure the deployment pipeline, add the following content to the `deploy.yml` file: + +```yaml +name: Deploy to VPS + +on: + push: + branches: + - main + +jobs: + deploy: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Copy files to VPS + env: + SSH_PRIVATE_KEY: ${{ secrets.DEPLOY_KEY }} + run: | + mkdir -p ~/.ssh + echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa + chmod 600 ~/.ssh/id_rsa + ssh-keyscan -H >> ~/.ssh/known_hosts + rsync -avz --delete ./ @:/var/www/html/ +``` + +Replace the following: + +- ``: by the IP address of your VPS. +- ``: 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: + +```bash +git add .github/workflows/deploy.yml +git commit -m "Adding GitHub Actions workflow for deployment" +git push origin main +``` + +### 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: + +```bash +sudo chown -R :www-data /var/www/html +sudo chmod -R 775 /var/www/html +``` + +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: + +```bash +rsync -avz --no-times --exclude='.git*' -e "ssh -i ./deploy_key -o StrictHostKeyChecking=no" ./ @:/var/www/html/ +``` + +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/html` on your VPS via `rsync`. + +##### **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: + +```bash +mkdir /home//test-github-actions +cd /home//test-github-actions +``` + +2\. Clone the GitHub repository in this directory + +```bash +git clone git@github.com:/github-actions.git . +``` + +If your repository is already in HTTPS , update it to use SSH: + +```bash +git remote set-url origin git@github.com:/github-actions.git +``` + +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: + +```bash +echo "Test from VPS user number 2" >> testfile.txt +git add testfile.txt +git commit -m "Add a test from the VPS" +git push origin main +``` + +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: + +```bash +ls /var/www/html +cat /var/www/html/testfile.txt +``` + +### 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 + +[Getting started with a VPS](/pages/bare_metal_cloud/virtual_private_servers/starting_with_a_vps) + +[Securing a VPS](/pages/bare_metal_cloud/virtual_private_servers/secure_your_vps) + +For specialized services (SEO, development, etc.), contact the [OVHcloud partners](/links/partner). + +Join our [community of users](/links/community). \ No newline at end of file diff --git a/pages/bare_metal_cloud/virtual_private_servers/deploy-website-github-actions/guide.en-ca.md b/pages/bare_metal_cloud/virtual_private_servers/deploy-website-github-actions/guide.en-ca.md new file mode 100644 index 00000000000..2a3ef080c36 --- /dev/null +++ b/pages/bare_metal_cloud/virtual_private_servers/deploy-website-github-actions/guide.en-ca.md @@ -0,0 +1,353 @@ +--- +title: "Automating the deployment of your website on your VPS via GitHub Actions" +excerpt: 'Find out how to deploy and automate your website’s code via GitHub Actions on an OVHcloud VPS' +updated: 2025-01-21 +--- + +## 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](/links/bare-metal/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) + +> [!warning] +> +> 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. + +## Instructions + +> [!primary] +> 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). + +**The main steps of the guide will be the following:** + +- [Configure SSH access for GitHub Actions](#configure-ssh) +- [Add the private key to GitHub](#add-private-key-github) +- [Initialize the GitHub repository (optional)](#init-github-repo) +- [Configure GitHub Actions for Automatic Deployment](#configure-github-actions) +- [Check and test the GitHub Actions workflow](#verify-workflow-github) +- [Conclusion](#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: + +```bash +ssh-keygen -t rsa -b 4096 -C "github-actions" -f /home//.ssh/deploy_key +``` + +Replace `` 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//.ssh/deploy_key`: private key +- `/home//.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: + +```bash +mkdir -p /home//.ssh +chmod 700 /home//.ssh +``` + +2\. Add the public key to the `authorized_keys` file: + +```bash +cat /home//.ssh/deploy_key.pub >> /home//.ssh/authorized_keys +chmod 600 /home//.ssh/authorized_keys +``` + +3\. Test the SSH connection + +Test the SSH connection with the private key to confirm that the access is functional: + +```bash +ssh -i /home//.ssh/deploy_key @ +``` + +Replace `` with the user configured to connect to your VPS and `` 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: + +```bash +cat /home//.ssh/deploy_key.pub +``` + +Follow the steps from the "Adding a new SSH key to your account" section of the [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. + +#### 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`: + +```bash +nano ~/.ssh/config +``` + +Add this configuration for GitHub access: + +```console +Host github.com + HostName github.com + User git + IdentityFile /home//.ssh/deploy_key +``` + +Save and exit the editor. + +Test the SSH connection with GitHub: + +```bash +ssh -T git@github.com +``` + +You should see a message like this: + +```console +Hi ! You've successfully authenticated, but GitHub does not provide shell access. +``` + +### Add the private key to GitHub + +Copy the content of the generated private key to your VPS with: + +```bash +cat /home//.ssh/deploy_key +``` + +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](https://docs.github.com/gb/en/actions/security-for-github-actions/security-guides/using-secrets-in-github-actions#creating-secrets-for-a-repository). + +### Initialize GitHub repository (optional) + +> [!primary] +> If you already have a GitHub repository containing your website’s code, go to the [next step](#configure-github-actions). + +#### Create a GitHub repository + +To create a GitHub repository, follow the steps from the "Create a repository" page of the [GitHub official documentation](https://docs.github.com/gb/en/repositories/creating-and-managing-repositories/creating-a-new-repository){.external}. + +#### Initialize the Git repository on the VPS + +1\. Log in to your VPS via SSH: + +```bash +ssh @ +``` + +2\. Install Git: + +```bash +sudo apt update && sudo apt install git -y +``` + +3\. Initialize a Git repository in your website directory: + +```bash +cd /var/www/html +sudo git init +sudo git remote add origin git@github.com:/.git +``` + +Replace `` with your GitHub username and `` with the name of your GitHub repository. + +4\. Add the files and make a first commit: + +```bash +git add . +git commit -m "Initial commit" +git branch -M main +git push -u origin main +``` + +### 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): + +```bash +cd /var/www/html +mkdir -p .github/workflows +``` + +2\. Create a workflow file + +Create a `deploy.yml` file in the `.github/workflows` directory: + +```bash +nano .github/workflows/deploy.yml +``` + +3\. Configure the `deploy.yml` file + +To configure the deployment pipeline, add the following content to the `deploy.yml` file: + +```yaml +name: Deploy to VPS + +on: + push: + branches: + - main + +jobs: + deploy: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Copy files to VPS + env: + SSH_PRIVATE_KEY: ${{ secrets.DEPLOY_KEY }} + run: | + mkdir -p ~/.ssh + echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa + chmod 600 ~/.ssh/id_rsa + ssh-keyscan -H >> ~/.ssh/known_hosts + rsync -avz --delete ./ @:/var/www/html/ +``` + +Replace the following: + +- ``: by the IP address of your VPS. +- ``: 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: + +```bash +git add .github/workflows/deploy.yml +git commit -m "Adding GitHub Actions workflow for deployment" +git push origin main +``` + +### 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: + +```bash +sudo chown -R :www-data /var/www/html +sudo chmod -R 775 /var/www/html +``` + +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: + +```bash +rsync -avz --no-times --exclude='.git*' -e "ssh -i ./deploy_key -o StrictHostKeyChecking=no" ./ @:/var/www/html/ +``` + +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/html` on your VPS via `rsync`. + +##### **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: + +```bash +mkdir /home//test-github-actions +cd /home//test-github-actions +``` + +2\. Clone the GitHub repository in this directory + +```bash +git clone git@github.com:/github-actions.git . +``` + +If your repository is already in HTTPS , update it to use SSH: + +```bash +git remote set-url origin git@github.com:/github-actions.git +``` + +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: + +```bash +echo "Test from VPS user number 2" >> testfile.txt +git add testfile.txt +git commit -m "Add a test from the VPS" +git push origin main +``` + +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: + +```bash +ls /var/www/html +cat /var/www/html/testfile.txt +``` + +### 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 + +[Getting started with a VPS](/pages/bare_metal_cloud/virtual_private_servers/starting_with_a_vps) + +[Securing a VPS](/pages/bare_metal_cloud/virtual_private_servers/secure_your_vps) + +For specialized services (SEO, development, etc.), contact the [OVHcloud partners](/links/partner). + +Join our [community of users](/links/community). \ No newline at end of file diff --git a/pages/bare_metal_cloud/virtual_private_servers/deploy-website-github-actions/guide.en-gb.md b/pages/bare_metal_cloud/virtual_private_servers/deploy-website-github-actions/guide.en-gb.md new file mode 100644 index 00000000000..2a3ef080c36 --- /dev/null +++ b/pages/bare_metal_cloud/virtual_private_servers/deploy-website-github-actions/guide.en-gb.md @@ -0,0 +1,353 @@ +--- +title: "Automating the deployment of your website on your VPS via GitHub Actions" +excerpt: 'Find out how to deploy and automate your website’s code via GitHub Actions on an OVHcloud VPS' +updated: 2025-01-21 +--- + +## 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](/links/bare-metal/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) + +> [!warning] +> +> 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. + +## Instructions + +> [!primary] +> 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). + +**The main steps of the guide will be the following:** + +- [Configure SSH access for GitHub Actions](#configure-ssh) +- [Add the private key to GitHub](#add-private-key-github) +- [Initialize the GitHub repository (optional)](#init-github-repo) +- [Configure GitHub Actions for Automatic Deployment](#configure-github-actions) +- [Check and test the GitHub Actions workflow](#verify-workflow-github) +- [Conclusion](#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: + +```bash +ssh-keygen -t rsa -b 4096 -C "github-actions" -f /home//.ssh/deploy_key +``` + +Replace `` 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//.ssh/deploy_key`: private key +- `/home//.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: + +```bash +mkdir -p /home//.ssh +chmod 700 /home//.ssh +``` + +2\. Add the public key to the `authorized_keys` file: + +```bash +cat /home//.ssh/deploy_key.pub >> /home//.ssh/authorized_keys +chmod 600 /home//.ssh/authorized_keys +``` + +3\. Test the SSH connection + +Test the SSH connection with the private key to confirm that the access is functional: + +```bash +ssh -i /home//.ssh/deploy_key @ +``` + +Replace `` with the user configured to connect to your VPS and `` 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: + +```bash +cat /home//.ssh/deploy_key.pub +``` + +Follow the steps from the "Adding a new SSH key to your account" section of the [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. + +#### 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`: + +```bash +nano ~/.ssh/config +``` + +Add this configuration for GitHub access: + +```console +Host github.com + HostName github.com + User git + IdentityFile /home//.ssh/deploy_key +``` + +Save and exit the editor. + +Test the SSH connection with GitHub: + +```bash +ssh -T git@github.com +``` + +You should see a message like this: + +```console +Hi ! You've successfully authenticated, but GitHub does not provide shell access. +``` + +### Add the private key to GitHub + +Copy the content of the generated private key to your VPS with: + +```bash +cat /home//.ssh/deploy_key +``` + +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](https://docs.github.com/gb/en/actions/security-for-github-actions/security-guides/using-secrets-in-github-actions#creating-secrets-for-a-repository). + +### Initialize GitHub repository (optional) + +> [!primary] +> If you already have a GitHub repository containing your website’s code, go to the [next step](#configure-github-actions). + +#### Create a GitHub repository + +To create a GitHub repository, follow the steps from the "Create a repository" page of the [GitHub official documentation](https://docs.github.com/gb/en/repositories/creating-and-managing-repositories/creating-a-new-repository){.external}. + +#### Initialize the Git repository on the VPS + +1\. Log in to your VPS via SSH: + +```bash +ssh @ +``` + +2\. Install Git: + +```bash +sudo apt update && sudo apt install git -y +``` + +3\. Initialize a Git repository in your website directory: + +```bash +cd /var/www/html +sudo git init +sudo git remote add origin git@github.com:/.git +``` + +Replace `` with your GitHub username and `` with the name of your GitHub repository. + +4\. Add the files and make a first commit: + +```bash +git add . +git commit -m "Initial commit" +git branch -M main +git push -u origin main +``` + +### 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): + +```bash +cd /var/www/html +mkdir -p .github/workflows +``` + +2\. Create a workflow file + +Create a `deploy.yml` file in the `.github/workflows` directory: + +```bash +nano .github/workflows/deploy.yml +``` + +3\. Configure the `deploy.yml` file + +To configure the deployment pipeline, add the following content to the `deploy.yml` file: + +```yaml +name: Deploy to VPS + +on: + push: + branches: + - main + +jobs: + deploy: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Copy files to VPS + env: + SSH_PRIVATE_KEY: ${{ secrets.DEPLOY_KEY }} + run: | + mkdir -p ~/.ssh + echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa + chmod 600 ~/.ssh/id_rsa + ssh-keyscan -H >> ~/.ssh/known_hosts + rsync -avz --delete ./ @:/var/www/html/ +``` + +Replace the following: + +- ``: by the IP address of your VPS. +- ``: 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: + +```bash +git add .github/workflows/deploy.yml +git commit -m "Adding GitHub Actions workflow for deployment" +git push origin main +``` + +### 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: + +```bash +sudo chown -R :www-data /var/www/html +sudo chmod -R 775 /var/www/html +``` + +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: + +```bash +rsync -avz --no-times --exclude='.git*' -e "ssh -i ./deploy_key -o StrictHostKeyChecking=no" ./ @:/var/www/html/ +``` + +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/html` on your VPS via `rsync`. + +##### **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: + +```bash +mkdir /home//test-github-actions +cd /home//test-github-actions +``` + +2\. Clone the GitHub repository in this directory + +```bash +git clone git@github.com:/github-actions.git . +``` + +If your repository is already in HTTPS , update it to use SSH: + +```bash +git remote set-url origin git@github.com:/github-actions.git +``` + +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: + +```bash +echo "Test from VPS user number 2" >> testfile.txt +git add testfile.txt +git commit -m "Add a test from the VPS" +git push origin main +``` + +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: + +```bash +ls /var/www/html +cat /var/www/html/testfile.txt +``` + +### 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 + +[Getting started with a VPS](/pages/bare_metal_cloud/virtual_private_servers/starting_with_a_vps) + +[Securing a VPS](/pages/bare_metal_cloud/virtual_private_servers/secure_your_vps) + +For specialized services (SEO, development, etc.), contact the [OVHcloud partners](/links/partner). + +Join our [community of users](/links/community). \ No newline at end of file diff --git a/pages/bare_metal_cloud/virtual_private_servers/deploy-website-github-actions/guide.en-ie.md b/pages/bare_metal_cloud/virtual_private_servers/deploy-website-github-actions/guide.en-ie.md new file mode 100644 index 00000000000..2a3ef080c36 --- /dev/null +++ b/pages/bare_metal_cloud/virtual_private_servers/deploy-website-github-actions/guide.en-ie.md @@ -0,0 +1,353 @@ +--- +title: "Automating the deployment of your website on your VPS via GitHub Actions" +excerpt: 'Find out how to deploy and automate your website’s code via GitHub Actions on an OVHcloud VPS' +updated: 2025-01-21 +--- + +## 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](/links/bare-metal/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) + +> [!warning] +> +> 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. + +## Instructions + +> [!primary] +> 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). + +**The main steps of the guide will be the following:** + +- [Configure SSH access for GitHub Actions](#configure-ssh) +- [Add the private key to GitHub](#add-private-key-github) +- [Initialize the GitHub repository (optional)](#init-github-repo) +- [Configure GitHub Actions for Automatic Deployment](#configure-github-actions) +- [Check and test the GitHub Actions workflow](#verify-workflow-github) +- [Conclusion](#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: + +```bash +ssh-keygen -t rsa -b 4096 -C "github-actions" -f /home//.ssh/deploy_key +``` + +Replace `` 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//.ssh/deploy_key`: private key +- `/home//.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: + +```bash +mkdir -p /home//.ssh +chmod 700 /home//.ssh +``` + +2\. Add the public key to the `authorized_keys` file: + +```bash +cat /home//.ssh/deploy_key.pub >> /home//.ssh/authorized_keys +chmod 600 /home//.ssh/authorized_keys +``` + +3\. Test the SSH connection + +Test the SSH connection with the private key to confirm that the access is functional: + +```bash +ssh -i /home//.ssh/deploy_key @ +``` + +Replace `` with the user configured to connect to your VPS and `` 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: + +```bash +cat /home//.ssh/deploy_key.pub +``` + +Follow the steps from the "Adding a new SSH key to your account" section of the [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. + +#### 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`: + +```bash +nano ~/.ssh/config +``` + +Add this configuration for GitHub access: + +```console +Host github.com + HostName github.com + User git + IdentityFile /home//.ssh/deploy_key +``` + +Save and exit the editor. + +Test the SSH connection with GitHub: + +```bash +ssh -T git@github.com +``` + +You should see a message like this: + +```console +Hi ! You've successfully authenticated, but GitHub does not provide shell access. +``` + +### Add the private key to GitHub + +Copy the content of the generated private key to your VPS with: + +```bash +cat /home//.ssh/deploy_key +``` + +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](https://docs.github.com/gb/en/actions/security-for-github-actions/security-guides/using-secrets-in-github-actions#creating-secrets-for-a-repository). + +### Initialize GitHub repository (optional) + +> [!primary] +> If you already have a GitHub repository containing your website’s code, go to the [next step](#configure-github-actions). + +#### Create a GitHub repository + +To create a GitHub repository, follow the steps from the "Create a repository" page of the [GitHub official documentation](https://docs.github.com/gb/en/repositories/creating-and-managing-repositories/creating-a-new-repository){.external}. + +#### Initialize the Git repository on the VPS + +1\. Log in to your VPS via SSH: + +```bash +ssh @ +``` + +2\. Install Git: + +```bash +sudo apt update && sudo apt install git -y +``` + +3\. Initialize a Git repository in your website directory: + +```bash +cd /var/www/html +sudo git init +sudo git remote add origin git@github.com:/.git +``` + +Replace `` with your GitHub username and `` with the name of your GitHub repository. + +4\. Add the files and make a first commit: + +```bash +git add . +git commit -m "Initial commit" +git branch -M main +git push -u origin main +``` + +### 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): + +```bash +cd /var/www/html +mkdir -p .github/workflows +``` + +2\. Create a workflow file + +Create a `deploy.yml` file in the `.github/workflows` directory: + +```bash +nano .github/workflows/deploy.yml +``` + +3\. Configure the `deploy.yml` file + +To configure the deployment pipeline, add the following content to the `deploy.yml` file: + +```yaml +name: Deploy to VPS + +on: + push: + branches: + - main + +jobs: + deploy: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Copy files to VPS + env: + SSH_PRIVATE_KEY: ${{ secrets.DEPLOY_KEY }} + run: | + mkdir -p ~/.ssh + echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa + chmod 600 ~/.ssh/id_rsa + ssh-keyscan -H >> ~/.ssh/known_hosts + rsync -avz --delete ./ @:/var/www/html/ +``` + +Replace the following: + +- ``: by the IP address of your VPS. +- ``: 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: + +```bash +git add .github/workflows/deploy.yml +git commit -m "Adding GitHub Actions workflow for deployment" +git push origin main +``` + +### 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: + +```bash +sudo chown -R :www-data /var/www/html +sudo chmod -R 775 /var/www/html +``` + +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: + +```bash +rsync -avz --no-times --exclude='.git*' -e "ssh -i ./deploy_key -o StrictHostKeyChecking=no" ./ @:/var/www/html/ +``` + +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/html` on your VPS via `rsync`. + +##### **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: + +```bash +mkdir /home//test-github-actions +cd /home//test-github-actions +``` + +2\. Clone the GitHub repository in this directory + +```bash +git clone git@github.com:/github-actions.git . +``` + +If your repository is already in HTTPS , update it to use SSH: + +```bash +git remote set-url origin git@github.com:/github-actions.git +``` + +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: + +```bash +echo "Test from VPS user number 2" >> testfile.txt +git add testfile.txt +git commit -m "Add a test from the VPS" +git push origin main +``` + +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: + +```bash +ls /var/www/html +cat /var/www/html/testfile.txt +``` + +### 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 + +[Getting started with a VPS](/pages/bare_metal_cloud/virtual_private_servers/starting_with_a_vps) + +[Securing a VPS](/pages/bare_metal_cloud/virtual_private_servers/secure_your_vps) + +For specialized services (SEO, development, etc.), contact the [OVHcloud partners](/links/partner). + +Join our [community of users](/links/community). \ No newline at end of file diff --git a/pages/bare_metal_cloud/virtual_private_servers/deploy-website-github-actions/guide.en-sg.md b/pages/bare_metal_cloud/virtual_private_servers/deploy-website-github-actions/guide.en-sg.md new file mode 100644 index 00000000000..2a3ef080c36 --- /dev/null +++ b/pages/bare_metal_cloud/virtual_private_servers/deploy-website-github-actions/guide.en-sg.md @@ -0,0 +1,353 @@ +--- +title: "Automating the deployment of your website on your VPS via GitHub Actions" +excerpt: 'Find out how to deploy and automate your website’s code via GitHub Actions on an OVHcloud VPS' +updated: 2025-01-21 +--- + +## 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](/links/bare-metal/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) + +> [!warning] +> +> 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. + +## Instructions + +> [!primary] +> 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). + +**The main steps of the guide will be the following:** + +- [Configure SSH access for GitHub Actions](#configure-ssh) +- [Add the private key to GitHub](#add-private-key-github) +- [Initialize the GitHub repository (optional)](#init-github-repo) +- [Configure GitHub Actions for Automatic Deployment](#configure-github-actions) +- [Check and test the GitHub Actions workflow](#verify-workflow-github) +- [Conclusion](#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: + +```bash +ssh-keygen -t rsa -b 4096 -C "github-actions" -f /home//.ssh/deploy_key +``` + +Replace `` 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//.ssh/deploy_key`: private key +- `/home//.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: + +```bash +mkdir -p /home//.ssh +chmod 700 /home//.ssh +``` + +2\. Add the public key to the `authorized_keys` file: + +```bash +cat /home//.ssh/deploy_key.pub >> /home//.ssh/authorized_keys +chmod 600 /home//.ssh/authorized_keys +``` + +3\. Test the SSH connection + +Test the SSH connection with the private key to confirm that the access is functional: + +```bash +ssh -i /home//.ssh/deploy_key @ +``` + +Replace `` with the user configured to connect to your VPS and `` 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: + +```bash +cat /home//.ssh/deploy_key.pub +``` + +Follow the steps from the "Adding a new SSH key to your account" section of the [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. + +#### 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`: + +```bash +nano ~/.ssh/config +``` + +Add this configuration for GitHub access: + +```console +Host github.com + HostName github.com + User git + IdentityFile /home//.ssh/deploy_key +``` + +Save and exit the editor. + +Test the SSH connection with GitHub: + +```bash +ssh -T git@github.com +``` + +You should see a message like this: + +```console +Hi ! You've successfully authenticated, but GitHub does not provide shell access. +``` + +### Add the private key to GitHub + +Copy the content of the generated private key to your VPS with: + +```bash +cat /home//.ssh/deploy_key +``` + +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](https://docs.github.com/gb/en/actions/security-for-github-actions/security-guides/using-secrets-in-github-actions#creating-secrets-for-a-repository). + +### Initialize GitHub repository (optional) + +> [!primary] +> If you already have a GitHub repository containing your website’s code, go to the [next step](#configure-github-actions). + +#### Create a GitHub repository + +To create a GitHub repository, follow the steps from the "Create a repository" page of the [GitHub official documentation](https://docs.github.com/gb/en/repositories/creating-and-managing-repositories/creating-a-new-repository){.external}. + +#### Initialize the Git repository on the VPS + +1\. Log in to your VPS via SSH: + +```bash +ssh @ +``` + +2\. Install Git: + +```bash +sudo apt update && sudo apt install git -y +``` + +3\. Initialize a Git repository in your website directory: + +```bash +cd /var/www/html +sudo git init +sudo git remote add origin git@github.com:/.git +``` + +Replace `` with your GitHub username and `` with the name of your GitHub repository. + +4\. Add the files and make a first commit: + +```bash +git add . +git commit -m "Initial commit" +git branch -M main +git push -u origin main +``` + +### 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): + +```bash +cd /var/www/html +mkdir -p .github/workflows +``` + +2\. Create a workflow file + +Create a `deploy.yml` file in the `.github/workflows` directory: + +```bash +nano .github/workflows/deploy.yml +``` + +3\. Configure the `deploy.yml` file + +To configure the deployment pipeline, add the following content to the `deploy.yml` file: + +```yaml +name: Deploy to VPS + +on: + push: + branches: + - main + +jobs: + deploy: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Copy files to VPS + env: + SSH_PRIVATE_KEY: ${{ secrets.DEPLOY_KEY }} + run: | + mkdir -p ~/.ssh + echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa + chmod 600 ~/.ssh/id_rsa + ssh-keyscan -H >> ~/.ssh/known_hosts + rsync -avz --delete ./ @:/var/www/html/ +``` + +Replace the following: + +- ``: by the IP address of your VPS. +- ``: 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: + +```bash +git add .github/workflows/deploy.yml +git commit -m "Adding GitHub Actions workflow for deployment" +git push origin main +``` + +### 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: + +```bash +sudo chown -R :www-data /var/www/html +sudo chmod -R 775 /var/www/html +``` + +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: + +```bash +rsync -avz --no-times --exclude='.git*' -e "ssh -i ./deploy_key -o StrictHostKeyChecking=no" ./ @:/var/www/html/ +``` + +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/html` on your VPS via `rsync`. + +##### **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: + +```bash +mkdir /home//test-github-actions +cd /home//test-github-actions +``` + +2\. Clone the GitHub repository in this directory + +```bash +git clone git@github.com:/github-actions.git . +``` + +If your repository is already in HTTPS , update it to use SSH: + +```bash +git remote set-url origin git@github.com:/github-actions.git +``` + +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: + +```bash +echo "Test from VPS user number 2" >> testfile.txt +git add testfile.txt +git commit -m "Add a test from the VPS" +git push origin main +``` + +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: + +```bash +ls /var/www/html +cat /var/www/html/testfile.txt +``` + +### 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 + +[Getting started with a VPS](/pages/bare_metal_cloud/virtual_private_servers/starting_with_a_vps) + +[Securing a VPS](/pages/bare_metal_cloud/virtual_private_servers/secure_your_vps) + +For specialized services (SEO, development, etc.), contact the [OVHcloud partners](/links/partner). + +Join our [community of users](/links/community). \ No newline at end of file diff --git a/pages/bare_metal_cloud/virtual_private_servers/deploy-website-github-actions/guide.en-us.md b/pages/bare_metal_cloud/virtual_private_servers/deploy-website-github-actions/guide.en-us.md new file mode 100644 index 00000000000..2a3ef080c36 --- /dev/null +++ b/pages/bare_metal_cloud/virtual_private_servers/deploy-website-github-actions/guide.en-us.md @@ -0,0 +1,353 @@ +--- +title: "Automating the deployment of your website on your VPS via GitHub Actions" +excerpt: 'Find out how to deploy and automate your website’s code via GitHub Actions on an OVHcloud VPS' +updated: 2025-01-21 +--- + +## 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](/links/bare-metal/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) + +> [!warning] +> +> 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. + +## Instructions + +> [!primary] +> 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). + +**The main steps of the guide will be the following:** + +- [Configure SSH access for GitHub Actions](#configure-ssh) +- [Add the private key to GitHub](#add-private-key-github) +- [Initialize the GitHub repository (optional)](#init-github-repo) +- [Configure GitHub Actions for Automatic Deployment](#configure-github-actions) +- [Check and test the GitHub Actions workflow](#verify-workflow-github) +- [Conclusion](#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: + +```bash +ssh-keygen -t rsa -b 4096 -C "github-actions" -f /home//.ssh/deploy_key +``` + +Replace `` 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//.ssh/deploy_key`: private key +- `/home//.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: + +```bash +mkdir -p /home//.ssh +chmod 700 /home//.ssh +``` + +2\. Add the public key to the `authorized_keys` file: + +```bash +cat /home//.ssh/deploy_key.pub >> /home//.ssh/authorized_keys +chmod 600 /home//.ssh/authorized_keys +``` + +3\. Test the SSH connection + +Test the SSH connection with the private key to confirm that the access is functional: + +```bash +ssh -i /home//.ssh/deploy_key @ +``` + +Replace `` with the user configured to connect to your VPS and `` 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: + +```bash +cat /home//.ssh/deploy_key.pub +``` + +Follow the steps from the "Adding a new SSH key to your account" section of the [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. + +#### 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`: + +```bash +nano ~/.ssh/config +``` + +Add this configuration for GitHub access: + +```console +Host github.com + HostName github.com + User git + IdentityFile /home//.ssh/deploy_key +``` + +Save and exit the editor. + +Test the SSH connection with GitHub: + +```bash +ssh -T git@github.com +``` + +You should see a message like this: + +```console +Hi ! You've successfully authenticated, but GitHub does not provide shell access. +``` + +### Add the private key to GitHub + +Copy the content of the generated private key to your VPS with: + +```bash +cat /home//.ssh/deploy_key +``` + +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](https://docs.github.com/gb/en/actions/security-for-github-actions/security-guides/using-secrets-in-github-actions#creating-secrets-for-a-repository). + +### Initialize GitHub repository (optional) + +> [!primary] +> If you already have a GitHub repository containing your website’s code, go to the [next step](#configure-github-actions). + +#### Create a GitHub repository + +To create a GitHub repository, follow the steps from the "Create a repository" page of the [GitHub official documentation](https://docs.github.com/gb/en/repositories/creating-and-managing-repositories/creating-a-new-repository){.external}. + +#### Initialize the Git repository on the VPS + +1\. Log in to your VPS via SSH: + +```bash +ssh @ +``` + +2\. Install Git: + +```bash +sudo apt update && sudo apt install git -y +``` + +3\. Initialize a Git repository in your website directory: + +```bash +cd /var/www/html +sudo git init +sudo git remote add origin git@github.com:/.git +``` + +Replace `` with your GitHub username and `` with the name of your GitHub repository. + +4\. Add the files and make a first commit: + +```bash +git add . +git commit -m "Initial commit" +git branch -M main +git push -u origin main +``` + +### 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): + +```bash +cd /var/www/html +mkdir -p .github/workflows +``` + +2\. Create a workflow file + +Create a `deploy.yml` file in the `.github/workflows` directory: + +```bash +nano .github/workflows/deploy.yml +``` + +3\. Configure the `deploy.yml` file + +To configure the deployment pipeline, add the following content to the `deploy.yml` file: + +```yaml +name: Deploy to VPS + +on: + push: + branches: + - main + +jobs: + deploy: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Copy files to VPS + env: + SSH_PRIVATE_KEY: ${{ secrets.DEPLOY_KEY }} + run: | + mkdir -p ~/.ssh + echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa + chmod 600 ~/.ssh/id_rsa + ssh-keyscan -H >> ~/.ssh/known_hosts + rsync -avz --delete ./ @:/var/www/html/ +``` + +Replace the following: + +- ``: by the IP address of your VPS. +- ``: 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: + +```bash +git add .github/workflows/deploy.yml +git commit -m "Adding GitHub Actions workflow for deployment" +git push origin main +``` + +### 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: + +```bash +sudo chown -R :www-data /var/www/html +sudo chmod -R 775 /var/www/html +``` + +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: + +```bash +rsync -avz --no-times --exclude='.git*' -e "ssh -i ./deploy_key -o StrictHostKeyChecking=no" ./ @:/var/www/html/ +``` + +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/html` on your VPS via `rsync`. + +##### **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: + +```bash +mkdir /home//test-github-actions +cd /home//test-github-actions +``` + +2\. Clone the GitHub repository in this directory + +```bash +git clone git@github.com:/github-actions.git . +``` + +If your repository is already in HTTPS , update it to use SSH: + +```bash +git remote set-url origin git@github.com:/github-actions.git +``` + +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: + +```bash +echo "Test from VPS user number 2" >> testfile.txt +git add testfile.txt +git commit -m "Add a test from the VPS" +git push origin main +``` + +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: + +```bash +ls /var/www/html +cat /var/www/html/testfile.txt +``` + +### 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 + +[Getting started with a VPS](/pages/bare_metal_cloud/virtual_private_servers/starting_with_a_vps) + +[Securing a VPS](/pages/bare_metal_cloud/virtual_private_servers/secure_your_vps) + +For specialized services (SEO, development, etc.), contact the [OVHcloud partners](/links/partner). + +Join our [community of users](/links/community). \ No newline at end of file diff --git a/pages/bare_metal_cloud/virtual_private_servers/deploy-website-github-actions/guide.es-es.md b/pages/bare_metal_cloud/virtual_private_servers/deploy-website-github-actions/guide.es-es.md new file mode 100644 index 00000000000..2a3ef080c36 --- /dev/null +++ b/pages/bare_metal_cloud/virtual_private_servers/deploy-website-github-actions/guide.es-es.md @@ -0,0 +1,353 @@ +--- +title: "Automating the deployment of your website on your VPS via GitHub Actions" +excerpt: 'Find out how to deploy and automate your website’s code via GitHub Actions on an OVHcloud VPS' +updated: 2025-01-21 +--- + +## 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](/links/bare-metal/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) + +> [!warning] +> +> 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. + +## Instructions + +> [!primary] +> 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). + +**The main steps of the guide will be the following:** + +- [Configure SSH access for GitHub Actions](#configure-ssh) +- [Add the private key to GitHub](#add-private-key-github) +- [Initialize the GitHub repository (optional)](#init-github-repo) +- [Configure GitHub Actions for Automatic Deployment](#configure-github-actions) +- [Check and test the GitHub Actions workflow](#verify-workflow-github) +- [Conclusion](#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: + +```bash +ssh-keygen -t rsa -b 4096 -C "github-actions" -f /home//.ssh/deploy_key +``` + +Replace `` 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//.ssh/deploy_key`: private key +- `/home//.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: + +```bash +mkdir -p /home//.ssh +chmod 700 /home//.ssh +``` + +2\. Add the public key to the `authorized_keys` file: + +```bash +cat /home//.ssh/deploy_key.pub >> /home//.ssh/authorized_keys +chmod 600 /home//.ssh/authorized_keys +``` + +3\. Test the SSH connection + +Test the SSH connection with the private key to confirm that the access is functional: + +```bash +ssh -i /home//.ssh/deploy_key @ +``` + +Replace `` with the user configured to connect to your VPS and `` 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: + +```bash +cat /home//.ssh/deploy_key.pub +``` + +Follow the steps from the "Adding a new SSH key to your account" section of the [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. + +#### 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`: + +```bash +nano ~/.ssh/config +``` + +Add this configuration for GitHub access: + +```console +Host github.com + HostName github.com + User git + IdentityFile /home//.ssh/deploy_key +``` + +Save and exit the editor. + +Test the SSH connection with GitHub: + +```bash +ssh -T git@github.com +``` + +You should see a message like this: + +```console +Hi ! You've successfully authenticated, but GitHub does not provide shell access. +``` + +### Add the private key to GitHub + +Copy the content of the generated private key to your VPS with: + +```bash +cat /home//.ssh/deploy_key +``` + +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](https://docs.github.com/gb/en/actions/security-for-github-actions/security-guides/using-secrets-in-github-actions#creating-secrets-for-a-repository). + +### Initialize GitHub repository (optional) + +> [!primary] +> If you already have a GitHub repository containing your website’s code, go to the [next step](#configure-github-actions). + +#### Create a GitHub repository + +To create a GitHub repository, follow the steps from the "Create a repository" page of the [GitHub official documentation](https://docs.github.com/gb/en/repositories/creating-and-managing-repositories/creating-a-new-repository){.external}. + +#### Initialize the Git repository on the VPS + +1\. Log in to your VPS via SSH: + +```bash +ssh @ +``` + +2\. Install Git: + +```bash +sudo apt update && sudo apt install git -y +``` + +3\. Initialize a Git repository in your website directory: + +```bash +cd /var/www/html +sudo git init +sudo git remote add origin git@github.com:/.git +``` + +Replace `` with your GitHub username and `` with the name of your GitHub repository. + +4\. Add the files and make a first commit: + +```bash +git add . +git commit -m "Initial commit" +git branch -M main +git push -u origin main +``` + +### 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): + +```bash +cd /var/www/html +mkdir -p .github/workflows +``` + +2\. Create a workflow file + +Create a `deploy.yml` file in the `.github/workflows` directory: + +```bash +nano .github/workflows/deploy.yml +``` + +3\. Configure the `deploy.yml` file + +To configure the deployment pipeline, add the following content to the `deploy.yml` file: + +```yaml +name: Deploy to VPS + +on: + push: + branches: + - main + +jobs: + deploy: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Copy files to VPS + env: + SSH_PRIVATE_KEY: ${{ secrets.DEPLOY_KEY }} + run: | + mkdir -p ~/.ssh + echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa + chmod 600 ~/.ssh/id_rsa + ssh-keyscan -H >> ~/.ssh/known_hosts + rsync -avz --delete ./ @:/var/www/html/ +``` + +Replace the following: + +- ``: by the IP address of your VPS. +- ``: 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: + +```bash +git add .github/workflows/deploy.yml +git commit -m "Adding GitHub Actions workflow for deployment" +git push origin main +``` + +### 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: + +```bash +sudo chown -R :www-data /var/www/html +sudo chmod -R 775 /var/www/html +``` + +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: + +```bash +rsync -avz --no-times --exclude='.git*' -e "ssh -i ./deploy_key -o StrictHostKeyChecking=no" ./ @:/var/www/html/ +``` + +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/html` on your VPS via `rsync`. + +##### **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: + +```bash +mkdir /home//test-github-actions +cd /home//test-github-actions +``` + +2\. Clone the GitHub repository in this directory + +```bash +git clone git@github.com:/github-actions.git . +``` + +If your repository is already in HTTPS , update it to use SSH: + +```bash +git remote set-url origin git@github.com:/github-actions.git +``` + +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: + +```bash +echo "Test from VPS user number 2" >> testfile.txt +git add testfile.txt +git commit -m "Add a test from the VPS" +git push origin main +``` + +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: + +```bash +ls /var/www/html +cat /var/www/html/testfile.txt +``` + +### 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 + +[Getting started with a VPS](/pages/bare_metal_cloud/virtual_private_servers/starting_with_a_vps) + +[Securing a VPS](/pages/bare_metal_cloud/virtual_private_servers/secure_your_vps) + +For specialized services (SEO, development, etc.), contact the [OVHcloud partners](/links/partner). + +Join our [community of users](/links/community). \ No newline at end of file diff --git a/pages/bare_metal_cloud/virtual_private_servers/deploy-website-github-actions/guide.es-us.md b/pages/bare_metal_cloud/virtual_private_servers/deploy-website-github-actions/guide.es-us.md new file mode 100644 index 00000000000..2a3ef080c36 --- /dev/null +++ b/pages/bare_metal_cloud/virtual_private_servers/deploy-website-github-actions/guide.es-us.md @@ -0,0 +1,353 @@ +--- +title: "Automating the deployment of your website on your VPS via GitHub Actions" +excerpt: 'Find out how to deploy and automate your website’s code via GitHub Actions on an OVHcloud VPS' +updated: 2025-01-21 +--- + +## 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](/links/bare-metal/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) + +> [!warning] +> +> 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. + +## Instructions + +> [!primary] +> 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). + +**The main steps of the guide will be the following:** + +- [Configure SSH access for GitHub Actions](#configure-ssh) +- [Add the private key to GitHub](#add-private-key-github) +- [Initialize the GitHub repository (optional)](#init-github-repo) +- [Configure GitHub Actions for Automatic Deployment](#configure-github-actions) +- [Check and test the GitHub Actions workflow](#verify-workflow-github) +- [Conclusion](#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: + +```bash +ssh-keygen -t rsa -b 4096 -C "github-actions" -f /home//.ssh/deploy_key +``` + +Replace `` 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//.ssh/deploy_key`: private key +- `/home//.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: + +```bash +mkdir -p /home//.ssh +chmod 700 /home//.ssh +``` + +2\. Add the public key to the `authorized_keys` file: + +```bash +cat /home//.ssh/deploy_key.pub >> /home//.ssh/authorized_keys +chmod 600 /home//.ssh/authorized_keys +``` + +3\. Test the SSH connection + +Test the SSH connection with the private key to confirm that the access is functional: + +```bash +ssh -i /home//.ssh/deploy_key @ +``` + +Replace `` with the user configured to connect to your VPS and `` 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: + +```bash +cat /home//.ssh/deploy_key.pub +``` + +Follow the steps from the "Adding a new SSH key to your account" section of the [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. + +#### 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`: + +```bash +nano ~/.ssh/config +``` + +Add this configuration for GitHub access: + +```console +Host github.com + HostName github.com + User git + IdentityFile /home//.ssh/deploy_key +``` + +Save and exit the editor. + +Test the SSH connection with GitHub: + +```bash +ssh -T git@github.com +``` + +You should see a message like this: + +```console +Hi ! You've successfully authenticated, but GitHub does not provide shell access. +``` + +### Add the private key to GitHub + +Copy the content of the generated private key to your VPS with: + +```bash +cat /home//.ssh/deploy_key +``` + +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](https://docs.github.com/gb/en/actions/security-for-github-actions/security-guides/using-secrets-in-github-actions#creating-secrets-for-a-repository). + +### Initialize GitHub repository (optional) + +> [!primary] +> If you already have a GitHub repository containing your website’s code, go to the [next step](#configure-github-actions). + +#### Create a GitHub repository + +To create a GitHub repository, follow the steps from the "Create a repository" page of the [GitHub official documentation](https://docs.github.com/gb/en/repositories/creating-and-managing-repositories/creating-a-new-repository){.external}. + +#### Initialize the Git repository on the VPS + +1\. Log in to your VPS via SSH: + +```bash +ssh @ +``` + +2\. Install Git: + +```bash +sudo apt update && sudo apt install git -y +``` + +3\. Initialize a Git repository in your website directory: + +```bash +cd /var/www/html +sudo git init +sudo git remote add origin git@github.com:/.git +``` + +Replace `` with your GitHub username and `` with the name of your GitHub repository. + +4\. Add the files and make a first commit: + +```bash +git add . +git commit -m "Initial commit" +git branch -M main +git push -u origin main +``` + +### 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): + +```bash +cd /var/www/html +mkdir -p .github/workflows +``` + +2\. Create a workflow file + +Create a `deploy.yml` file in the `.github/workflows` directory: + +```bash +nano .github/workflows/deploy.yml +``` + +3\. Configure the `deploy.yml` file + +To configure the deployment pipeline, add the following content to the `deploy.yml` file: + +```yaml +name: Deploy to VPS + +on: + push: + branches: + - main + +jobs: + deploy: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Copy files to VPS + env: + SSH_PRIVATE_KEY: ${{ secrets.DEPLOY_KEY }} + run: | + mkdir -p ~/.ssh + echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa + chmod 600 ~/.ssh/id_rsa + ssh-keyscan -H >> ~/.ssh/known_hosts + rsync -avz --delete ./ @:/var/www/html/ +``` + +Replace the following: + +- ``: by the IP address of your VPS. +- ``: 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: + +```bash +git add .github/workflows/deploy.yml +git commit -m "Adding GitHub Actions workflow for deployment" +git push origin main +``` + +### 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: + +```bash +sudo chown -R :www-data /var/www/html +sudo chmod -R 775 /var/www/html +``` + +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: + +```bash +rsync -avz --no-times --exclude='.git*' -e "ssh -i ./deploy_key -o StrictHostKeyChecking=no" ./ @:/var/www/html/ +``` + +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/html` on your VPS via `rsync`. + +##### **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: + +```bash +mkdir /home//test-github-actions +cd /home//test-github-actions +``` + +2\. Clone the GitHub repository in this directory + +```bash +git clone git@github.com:/github-actions.git . +``` + +If your repository is already in HTTPS , update it to use SSH: + +```bash +git remote set-url origin git@github.com:/github-actions.git +``` + +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: + +```bash +echo "Test from VPS user number 2" >> testfile.txt +git add testfile.txt +git commit -m "Add a test from the VPS" +git push origin main +``` + +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: + +```bash +ls /var/www/html +cat /var/www/html/testfile.txt +``` + +### 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 + +[Getting started with a VPS](/pages/bare_metal_cloud/virtual_private_servers/starting_with_a_vps) + +[Securing a VPS](/pages/bare_metal_cloud/virtual_private_servers/secure_your_vps) + +For specialized services (SEO, development, etc.), contact the [OVHcloud partners](/links/partner). + +Join our [community of users](/links/community). \ No newline at end of file diff --git a/pages/bare_metal_cloud/virtual_private_servers/deploy-website-github-actions/guide.fr-ca.md b/pages/bare_metal_cloud/virtual_private_servers/deploy-website-github-actions/guide.fr-ca.md new file mode 100644 index 00000000000..3db6ebfbdc2 --- /dev/null +++ b/pages/bare_metal_cloud/virtual_private_servers/deploy-website-github-actions/guide.fr-ca.md @@ -0,0 +1,353 @@ +--- +title: "Automatiser le déploiement de votre site web sur votre VPS via GitHub Actions" +excerpt: 'Découvrez comment déployer et automatiser le code de votre site web via GitHub Actions sur un VPS OVHcloud' +updated: 2025-01-21 +--- + +## Objectif + +Automatiser le déploiement de votre site web sur un VPS simplifie considérablement la gestion de vos mises à jour. Avec GitHub Actions, vous pouvez configurer un pipeline de déploiement automatique, évitant ainsi les déploiements manuels. Cette méthode garantit un déploiement rapide et fiable, tout en réduisant les risques d'erreurs humaines. Que vous soyez développeur débutant ou expérimenté, ce tutoriel vous permettra de mettre en place une solution professionnelle adaptée à vos besoins. + +**Découvrez comment automatiser le déploiement de vos applications web avec GitHub Actions sur un VPS OVHcloud.** + +## Prérequis + +- Un [VPS](/links/bare-metal/vps) fonctionnel dans votre compte OVHcloud +- Un compte GitHub actif +- Un dépôt contenant le code de votre site web (facultatif) +- Un VPS configuré avec les services nécessaires (ex. Apache/Nginx, PHP, SGBD, etc.) +- Disposer d'un accès administrateur au VPS (via SSH) + +> [!warning] +> +> Si besoin, consultez notre guide « [Débuter avec un VPS](/pages/bare_metal_cloud/virtual_private_servers/starting_with_a_vps) » avant de poursuivre la lecture de ce guide. + +## En pratique + +> [!primary] +> Afin d'être sûr de remplir les prérequis, consultez les guides « [Installer un environnement de développement web sur un VPS ou un serveur dédié](/pages/bare_metal_cloud/virtual_private_servers/install_env_web_dev_on_vps) » et « [Sécuriser un VPS](/pages/bare_metal_cloud/virtual_private_servers/secure_your_vps) ». + +**Les étapes principales du guide seront les suivantes :** + +- [Configurer l'accès SSH pour GitHub Actions](#configure-ssh) +- [Ajouter la clé privée à GitHub](#add-private-key-github) +- [Initialiser le dépôt GitHub (facultatif)](#init-github-repo) +- [Configurer GitHub Actions pour le déploiement automatique](#configure-github-actions) +- [Vérifier et tester le workflow GitHub Actions](#verify-workflow-github) +- [Conclusion](#conclusion) + +### Configurer l'accès SSH pour GitHub Actions + +Si votre site web existe déjà, identifiez le chemin du répertoire où il est hébergé. Par exemple, sur un VPS OVHcloud, il peut s'agir de `/var/www/html`. Conservez ce chemin en mémoire, afin de l'utiliser lors de la configuration du pipeline GitHub Actions. + +Pour permettre à GitHub Actions de déployer automatiquement votre site web, configurez un accès SSH sécurisé à votre VPS. + +#### Créer une paire de clés SSH + +Connectez-vous à votre VPS via SSH et générez une paire de clés SSH dédiée pour GitHub Actions : + +```bash +ssh-keygen -t rsa -b 4096 -C "github-actions" -f /home//.ssh/deploy_key +``` + +Remplacez `` par l'utilisateur configuré pour se connecter à votre VPS. + +Appuyez sur `Entrée` lorsqu'une passphrase vous est demandée (laisser la passphrase vide facilite l'automatisation du déploiement avec GitHub Actions. Cependant, cela nécessite de sécuriser la clé privée en la limitant à cet usage et en la stockant de manière sécurisée). + +Vous obtenez deux fichiers : + +- `/home//.ssh/deploy_key` : clé privée +- `/home//.ssh/deploy_key.pub` : clé publique + +#### Configurer la clé publique sur le VPS + +Pour permettre à GitHub Actions de se connecter à votre VPS via SSH et d'y déployer le code de votre site web, ajoutez la clé publique générée à la liste des clés autorisées sur le VPS. + +1\. Créer le répertoire `.ssh` : + +```bash +mkdir -p /home//.ssh +chmod 700 /home//.ssh +``` + +2\. Ajouter la clé publique au fichier `authorized_keys` : + +```bash +cat /home//.ssh/deploy_key.pub >> /home//.ssh/authorized_keys +chmod 600 /home//.ssh/authorized_keys +``` + +3\. Tester la connexion SSH + +Testez la connexion SSH avec la clé privée pour confirmer que l'accès est fonctionnel : + +```bash +ssh -i /home//.ssh/deploy_key @ +``` + +Remplacez `` par l'utilisateur configuré pour se connecter à votre VPS et `` par l'IP de votre VPS. + +#### Ajouter la clé publique à GitHub + +Après avoir configuré la clé publique sur votre VPS, ajoutez-la à votre compte GitHub. Copiez le contenu de la clé publique générée sur votre VPS avec : + +```bash +cat /home//.ssh/deploy_key.pub +``` + +Suivez les étapes de la section « Ajout d'une nouvelle clé SSH à votre compte » de la [documentation officielle de GitHub](https://docs.github.com/fr/authentication/connecting-to-github-with-ssh/adding-a-new-ssh-key-to-your-github-account#adding-a-new-ssh-key-to-your-account) pour ajouter votre clé publique à votre compte GitHub. + +#### Configurer l'accès SSH à GitHub sur le VPS + +Pour garantir que GitHub utilise la clé privée générée pour établir une connexion SSH sécurisée, configurez le fichier `~/.ssh/config`. Cette étape facilite les commandes ultérieures en évitant d'avoir à spécifier manuellement la clé privée à chaque interaction avec GitHub. + +Sur votre VPS, créez ou modifiez le fichier `~/.ssh/config` : + +```bash +nano ~/.ssh/config +``` + +Ajoutez cette configuration pour l'accès à GitHub : + +```console +Host github.com + HostName github.com + User git + IdentityFile /home//.ssh/deploy_key +``` + +Sauvegardez et quittez l'éditeur. + +Testez la connexion SSH avec GitHub : + +```bash +ssh -T git@github.com +``` + +Vous devriez voir un message tel que celui-ci : + +```console +Hi ! You've successfully authenticated, but GitHub does not provide shell access. +``` + +### Ajouter la clé privée à GitHub + +Copiez le contenu de la clé privée générée sur votre VPS avec : + +```bash +cat /home//.ssh/deploy_key +``` + +Pour permettre à GitHub Actions de se connecter automatiquement à votre VPS, ajoutez la clé privée dans un dépôt secret sur GitHub. Cela permettra à GitHub de déployer votre site web via SSH. Suivez les étapes de la section « Création de secrets pour un dépôt » de la [documentation officielle de GitHub](https://docs.github.com/fr/actions/security-for-github-actions/security-guides/using-secrets-in-github-actions#creating-secrets-for-a-repository). + +### Initialiser le dépôt GitHub (facultatif) + +> [!primary] +> Si vous possédez déjà un dépôt GitHub contenant le code de votre site web, passez à l'[étape suivante](#configure-github-actions). + +#### Créer un dépôt GitHub + +Pour créer un dépôt GitHub, suivez les étapes de la page « Création d'un dépôt » de la [documentation officielle de GitHub](https://docs.github.com/fr/repositories/creating-and-managing-repositories/creating-a-new-repository){.external}. + +#### Initialiser le dépôt Git sur le VPS + +1\. Connectez-vous à votre VPS via SSH : + +```bash +ssh @ +``` + +2\. Installez Git : + +```bash +sudo apt update && sudo apt install git -y +``` + +3\. Initialisez un dépôt Git dans le répertoire de votre site web : + +```bash +cd /var/www/html +sudo git init +sudo git remote add origin git@github.com:/.git +``` + +Remplacez `` par votre nom d'utilisateur GitHub et `` par le nom de votre dépôt GitHub. + +4\. Ajoutez les fichiers et effectuez un premier commit : + +```bash +git add . +git commit -m "Initial commit" +git branch -M main +git push -u origin main +``` + +### Configurer GitHub Actions pour le déploiement automatique + +Créez et configurez un workflow pour synchroniser automatiquement le code de votre site web entre GitHub et le VPS. + +#### Créer le fichier de workflow GitHub Actions + +1\. Créez un répertoire pour les workflows + +Sur votre VPS, créez le répertoire `.github/workflows` dans le dossier contenant votre projet Git (c'est-à-dire le répertoire contenant le code de votre site web) : + +```bash +cd /var/www/html +mkdir -p .github/workflows +``` + +2\. Créez un fichier de workflow + +Créez un fichier `deploy.yml` dans le répertoire `.github/workflows` : + +```bash +nano .github/workflows/deploy.yml +``` + +3\. Configurez le fichier `deploy.yml`. + +Pour configurer le pipeline de déploiement, ajoutez le contenu suivant au fichier `deploy.yml` : + +```yaml +name: Deploy to VPS + +on: + push: + branches: + - main + +jobs: + deploy: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Copy files to VPS + env: + SSH_PRIVATE_KEY: ${{ secrets.DEPLOY_KEY }} + run: | + mkdir -p ~/.ssh + echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa + chmod 600 ~/.ssh/id_rsa + ssh-keyscan -H >> ~/.ssh/known_hosts + rsync -avz --delete ./ @:/var/www/html/ +``` + +Remplacez les éléments suivants : + +- `` : par l'adresse IP de votre VPS. +- `` : par l'utilisateur SSH configuré sur votre VPS. +- `DEPLOY_KEY` : par le nom du secret dans les paramètres de votre dépôt GitHub. + +4\. Ajoutez le fichier de workflow au dépôt GitHub + +Une fois le fichier de workflow configuré, ajoutez-le à votre dépôt Git et poussez-le vers GitHub : + +```bash +git add .github/workflows/deploy.yml +git commit -m "Ajout du workflow GitHub Actions pour le déploiement" +git push origin main +``` + +### Vérifier et tester le workflow GitHub Actions + +#### Vérifier l'exécution du premier workflow + +Rendez-vous dans l'onglet `Actions` de votre dépôt GitHub et vérifiez que votre premier workflow s'est bien exécuté. + +Si une erreur survient, cliquez sur le workflow échoué pour consulter les logs. Assurez-vous que votre clé privée est correctement ajoutée comme secret dans votre dépôt GitHub et que votre clé publique est bien ajoutée dans le fichier `.ssh/authorized_keys`. + +##### **Permissions insuffisantes** + +Lors du premier déploiement, des erreurs peuvent survenir concernant les permissions (`Permission denied (13)`, `rsync: failed to set times`, etc.) + +1\. Vérifiez que l'utilisateur a les permissions nécessaires + +Assurez-vous que l'utilisateur SSH configuré sur votre VPS a les droits d'écriture sur tout le répertoire Git (`/var/www/html`) et ses sous-répertoires : + +```bash +sudo chown -R :www-data /var/www/html +sudo chmod -R 775 /var/www/html +``` + +2\. Testez localement avec rsync + +Avant de relancer le workflow GitHub Actions, testez la commande `rsync` manuellement à partir de votre machine locale. Cela vous permettra de confirmer que les permissions sont correctement configurées : + +```bash +rsync -avz --no-times --exclude='.git*' -e "ssh -i ./deploy_key -o StrictHostKeyChecking=no" ./ @:/var/www/html/ +``` + +Si cette commande réussit, relancez ensuite le workflow sur GitHub. + +#### Déclenchement du workflow avec `git push` + +Lorsqu'un `git push` est effectué sur la branche `main` (ou toute autre branche spécifiée dans votre fichier `deploy.yml`), le workflow exécute les étapes définies dans le fichier `deploy.yml` : + +- Clonage du dépôt GitHub dans l'environnement GitHub Actions. +- Configuration de la clé SSH pour établir une connexion avec votre VPS. +- Synchronisztion des fichiers depuis le dépôt GitHub vers le répertoire `/var/www/html` de votre VPS via `rsync`. + +##### **Tester le workflow** + +1\. Clonez le dépôt GitHub dans un répertoire de test sur le VPS + +Créez un répertoire temporaire sur votre VPS pour simuler un autre environnement utilisateur. Par exemple : + +```bash +mkdir /home//test-github-actions +cd /home//test-github-actions +``` + +2\. Clonez le dépôt GitHub dans ce répertoire + +```bash +git clone git@github.com:/github-actions.git . +``` + +Si votre dépôt est déjà en HTTPS , mettez-le à jour pour utiliser SSH : + +```bash +git remote set-url origin git@github.com:/github-actions.git +``` + +3\. Effectuez une modification dans le dépôt de test + +Ajoutez un nouveau fichier ou modifiez un fichier existant dans le répertoire de test et faites un `git push` sur votre dépôt GitHub : + +```bash +echo "Test depuis l'utilisateur numéro 2 du VPS" >> testfile.txt +git add testfile.txt +git commit -m "Ajout d'un test depuis le VPS" +git push origin main +``` + +4\. Vérifiez l'exécution du workflow sur GitHub + +Rendez-vous dans l'onglet `Actions` de votre dépôt GitHub et vérifiez que le workflow a été déclenché automatiquement après le `git push`. Si le workflow réussit, les modifications seront synchronisées dans le dossier de votre site web (`/var/www/html`). + +5\. Confirmer la synchronisation dans `/var/www/html` + +Retournez dans votre répertoire principal de déploiement (`/var/www/html`) et vérifiez que le fichier `testfile.txt` est bien présent : + +```bash +ls /var/www/html +cat /var/www/html/testfile.txt +``` + +### Conclusion + +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. + +## Aller plus loin + +[Premiers pas avec un VPS](/pages/bare_metal_cloud/virtual_private_servers/starting_with_a_vps) + +[Sécuriser un VPS](/pages/bare_metal_cloud/virtual_private_servers/secure_your_vps) + +Pour des prestations spécialisées (référencement, développement, etc), contactez les [partenaires OVHcloud](/links/partner). + +Échangez avec notre [communauté d'utilisateurs](/links/community). \ No newline at end of file diff --git a/pages/bare_metal_cloud/virtual_private_servers/deploy-website-github-actions/guide.fr-fr.md b/pages/bare_metal_cloud/virtual_private_servers/deploy-website-github-actions/guide.fr-fr.md new file mode 100644 index 00000000000..3db6ebfbdc2 --- /dev/null +++ b/pages/bare_metal_cloud/virtual_private_servers/deploy-website-github-actions/guide.fr-fr.md @@ -0,0 +1,353 @@ +--- +title: "Automatiser le déploiement de votre site web sur votre VPS via GitHub Actions" +excerpt: 'Découvrez comment déployer et automatiser le code de votre site web via GitHub Actions sur un VPS OVHcloud' +updated: 2025-01-21 +--- + +## Objectif + +Automatiser le déploiement de votre site web sur un VPS simplifie considérablement la gestion de vos mises à jour. Avec GitHub Actions, vous pouvez configurer un pipeline de déploiement automatique, évitant ainsi les déploiements manuels. Cette méthode garantit un déploiement rapide et fiable, tout en réduisant les risques d'erreurs humaines. Que vous soyez développeur débutant ou expérimenté, ce tutoriel vous permettra de mettre en place une solution professionnelle adaptée à vos besoins. + +**Découvrez comment automatiser le déploiement de vos applications web avec GitHub Actions sur un VPS OVHcloud.** + +## Prérequis + +- Un [VPS](/links/bare-metal/vps) fonctionnel dans votre compte OVHcloud +- Un compte GitHub actif +- Un dépôt contenant le code de votre site web (facultatif) +- Un VPS configuré avec les services nécessaires (ex. Apache/Nginx, PHP, SGBD, etc.) +- Disposer d'un accès administrateur au VPS (via SSH) + +> [!warning] +> +> Si besoin, consultez notre guide « [Débuter avec un VPS](/pages/bare_metal_cloud/virtual_private_servers/starting_with_a_vps) » avant de poursuivre la lecture de ce guide. + +## En pratique + +> [!primary] +> Afin d'être sûr de remplir les prérequis, consultez les guides « [Installer un environnement de développement web sur un VPS ou un serveur dédié](/pages/bare_metal_cloud/virtual_private_servers/install_env_web_dev_on_vps) » et « [Sécuriser un VPS](/pages/bare_metal_cloud/virtual_private_servers/secure_your_vps) ». + +**Les étapes principales du guide seront les suivantes :** + +- [Configurer l'accès SSH pour GitHub Actions](#configure-ssh) +- [Ajouter la clé privée à GitHub](#add-private-key-github) +- [Initialiser le dépôt GitHub (facultatif)](#init-github-repo) +- [Configurer GitHub Actions pour le déploiement automatique](#configure-github-actions) +- [Vérifier et tester le workflow GitHub Actions](#verify-workflow-github) +- [Conclusion](#conclusion) + +### Configurer l'accès SSH pour GitHub Actions + +Si votre site web existe déjà, identifiez le chemin du répertoire où il est hébergé. Par exemple, sur un VPS OVHcloud, il peut s'agir de `/var/www/html`. Conservez ce chemin en mémoire, afin de l'utiliser lors de la configuration du pipeline GitHub Actions. + +Pour permettre à GitHub Actions de déployer automatiquement votre site web, configurez un accès SSH sécurisé à votre VPS. + +#### Créer une paire de clés SSH + +Connectez-vous à votre VPS via SSH et générez une paire de clés SSH dédiée pour GitHub Actions : + +```bash +ssh-keygen -t rsa -b 4096 -C "github-actions" -f /home//.ssh/deploy_key +``` + +Remplacez `` par l'utilisateur configuré pour se connecter à votre VPS. + +Appuyez sur `Entrée` lorsqu'une passphrase vous est demandée (laisser la passphrase vide facilite l'automatisation du déploiement avec GitHub Actions. Cependant, cela nécessite de sécuriser la clé privée en la limitant à cet usage et en la stockant de manière sécurisée). + +Vous obtenez deux fichiers : + +- `/home//.ssh/deploy_key` : clé privée +- `/home//.ssh/deploy_key.pub` : clé publique + +#### Configurer la clé publique sur le VPS + +Pour permettre à GitHub Actions de se connecter à votre VPS via SSH et d'y déployer le code de votre site web, ajoutez la clé publique générée à la liste des clés autorisées sur le VPS. + +1\. Créer le répertoire `.ssh` : + +```bash +mkdir -p /home//.ssh +chmod 700 /home//.ssh +``` + +2\. Ajouter la clé publique au fichier `authorized_keys` : + +```bash +cat /home//.ssh/deploy_key.pub >> /home//.ssh/authorized_keys +chmod 600 /home//.ssh/authorized_keys +``` + +3\. Tester la connexion SSH + +Testez la connexion SSH avec la clé privée pour confirmer que l'accès est fonctionnel : + +```bash +ssh -i /home//.ssh/deploy_key @ +``` + +Remplacez `` par l'utilisateur configuré pour se connecter à votre VPS et `` par l'IP de votre VPS. + +#### Ajouter la clé publique à GitHub + +Après avoir configuré la clé publique sur votre VPS, ajoutez-la à votre compte GitHub. Copiez le contenu de la clé publique générée sur votre VPS avec : + +```bash +cat /home//.ssh/deploy_key.pub +``` + +Suivez les étapes de la section « Ajout d'une nouvelle clé SSH à votre compte » de la [documentation officielle de GitHub](https://docs.github.com/fr/authentication/connecting-to-github-with-ssh/adding-a-new-ssh-key-to-your-github-account#adding-a-new-ssh-key-to-your-account) pour ajouter votre clé publique à votre compte GitHub. + +#### Configurer l'accès SSH à GitHub sur le VPS + +Pour garantir que GitHub utilise la clé privée générée pour établir une connexion SSH sécurisée, configurez le fichier `~/.ssh/config`. Cette étape facilite les commandes ultérieures en évitant d'avoir à spécifier manuellement la clé privée à chaque interaction avec GitHub. + +Sur votre VPS, créez ou modifiez le fichier `~/.ssh/config` : + +```bash +nano ~/.ssh/config +``` + +Ajoutez cette configuration pour l'accès à GitHub : + +```console +Host github.com + HostName github.com + User git + IdentityFile /home//.ssh/deploy_key +``` + +Sauvegardez et quittez l'éditeur. + +Testez la connexion SSH avec GitHub : + +```bash +ssh -T git@github.com +``` + +Vous devriez voir un message tel que celui-ci : + +```console +Hi ! You've successfully authenticated, but GitHub does not provide shell access. +``` + +### Ajouter la clé privée à GitHub + +Copiez le contenu de la clé privée générée sur votre VPS avec : + +```bash +cat /home//.ssh/deploy_key +``` + +Pour permettre à GitHub Actions de se connecter automatiquement à votre VPS, ajoutez la clé privée dans un dépôt secret sur GitHub. Cela permettra à GitHub de déployer votre site web via SSH. Suivez les étapes de la section « Création de secrets pour un dépôt » de la [documentation officielle de GitHub](https://docs.github.com/fr/actions/security-for-github-actions/security-guides/using-secrets-in-github-actions#creating-secrets-for-a-repository). + +### Initialiser le dépôt GitHub (facultatif) + +> [!primary] +> Si vous possédez déjà un dépôt GitHub contenant le code de votre site web, passez à l'[étape suivante](#configure-github-actions). + +#### Créer un dépôt GitHub + +Pour créer un dépôt GitHub, suivez les étapes de la page « Création d'un dépôt » de la [documentation officielle de GitHub](https://docs.github.com/fr/repositories/creating-and-managing-repositories/creating-a-new-repository){.external}. + +#### Initialiser le dépôt Git sur le VPS + +1\. Connectez-vous à votre VPS via SSH : + +```bash +ssh @ +``` + +2\. Installez Git : + +```bash +sudo apt update && sudo apt install git -y +``` + +3\. Initialisez un dépôt Git dans le répertoire de votre site web : + +```bash +cd /var/www/html +sudo git init +sudo git remote add origin git@github.com:/.git +``` + +Remplacez `` par votre nom d'utilisateur GitHub et `` par le nom de votre dépôt GitHub. + +4\. Ajoutez les fichiers et effectuez un premier commit : + +```bash +git add . +git commit -m "Initial commit" +git branch -M main +git push -u origin main +``` + +### Configurer GitHub Actions pour le déploiement automatique + +Créez et configurez un workflow pour synchroniser automatiquement le code de votre site web entre GitHub et le VPS. + +#### Créer le fichier de workflow GitHub Actions + +1\. Créez un répertoire pour les workflows + +Sur votre VPS, créez le répertoire `.github/workflows` dans le dossier contenant votre projet Git (c'est-à-dire le répertoire contenant le code de votre site web) : + +```bash +cd /var/www/html +mkdir -p .github/workflows +``` + +2\. Créez un fichier de workflow + +Créez un fichier `deploy.yml` dans le répertoire `.github/workflows` : + +```bash +nano .github/workflows/deploy.yml +``` + +3\. Configurez le fichier `deploy.yml`. + +Pour configurer le pipeline de déploiement, ajoutez le contenu suivant au fichier `deploy.yml` : + +```yaml +name: Deploy to VPS + +on: + push: + branches: + - main + +jobs: + deploy: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Copy files to VPS + env: + SSH_PRIVATE_KEY: ${{ secrets.DEPLOY_KEY }} + run: | + mkdir -p ~/.ssh + echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa + chmod 600 ~/.ssh/id_rsa + ssh-keyscan -H >> ~/.ssh/known_hosts + rsync -avz --delete ./ @:/var/www/html/ +``` + +Remplacez les éléments suivants : + +- `` : par l'adresse IP de votre VPS. +- `` : par l'utilisateur SSH configuré sur votre VPS. +- `DEPLOY_KEY` : par le nom du secret dans les paramètres de votre dépôt GitHub. + +4\. Ajoutez le fichier de workflow au dépôt GitHub + +Une fois le fichier de workflow configuré, ajoutez-le à votre dépôt Git et poussez-le vers GitHub : + +```bash +git add .github/workflows/deploy.yml +git commit -m "Ajout du workflow GitHub Actions pour le déploiement" +git push origin main +``` + +### Vérifier et tester le workflow GitHub Actions + +#### Vérifier l'exécution du premier workflow + +Rendez-vous dans l'onglet `Actions` de votre dépôt GitHub et vérifiez que votre premier workflow s'est bien exécuté. + +Si une erreur survient, cliquez sur le workflow échoué pour consulter les logs. Assurez-vous que votre clé privée est correctement ajoutée comme secret dans votre dépôt GitHub et que votre clé publique est bien ajoutée dans le fichier `.ssh/authorized_keys`. + +##### **Permissions insuffisantes** + +Lors du premier déploiement, des erreurs peuvent survenir concernant les permissions (`Permission denied (13)`, `rsync: failed to set times`, etc.) + +1\. Vérifiez que l'utilisateur a les permissions nécessaires + +Assurez-vous que l'utilisateur SSH configuré sur votre VPS a les droits d'écriture sur tout le répertoire Git (`/var/www/html`) et ses sous-répertoires : + +```bash +sudo chown -R :www-data /var/www/html +sudo chmod -R 775 /var/www/html +``` + +2\. Testez localement avec rsync + +Avant de relancer le workflow GitHub Actions, testez la commande `rsync` manuellement à partir de votre machine locale. Cela vous permettra de confirmer que les permissions sont correctement configurées : + +```bash +rsync -avz --no-times --exclude='.git*' -e "ssh -i ./deploy_key -o StrictHostKeyChecking=no" ./ @:/var/www/html/ +``` + +Si cette commande réussit, relancez ensuite le workflow sur GitHub. + +#### Déclenchement du workflow avec `git push` + +Lorsqu'un `git push` est effectué sur la branche `main` (ou toute autre branche spécifiée dans votre fichier `deploy.yml`), le workflow exécute les étapes définies dans le fichier `deploy.yml` : + +- Clonage du dépôt GitHub dans l'environnement GitHub Actions. +- Configuration de la clé SSH pour établir une connexion avec votre VPS. +- Synchronisztion des fichiers depuis le dépôt GitHub vers le répertoire `/var/www/html` de votre VPS via `rsync`. + +##### **Tester le workflow** + +1\. Clonez le dépôt GitHub dans un répertoire de test sur le VPS + +Créez un répertoire temporaire sur votre VPS pour simuler un autre environnement utilisateur. Par exemple : + +```bash +mkdir /home//test-github-actions +cd /home//test-github-actions +``` + +2\. Clonez le dépôt GitHub dans ce répertoire + +```bash +git clone git@github.com:/github-actions.git . +``` + +Si votre dépôt est déjà en HTTPS , mettez-le à jour pour utiliser SSH : + +```bash +git remote set-url origin git@github.com:/github-actions.git +``` + +3\. Effectuez une modification dans le dépôt de test + +Ajoutez un nouveau fichier ou modifiez un fichier existant dans le répertoire de test et faites un `git push` sur votre dépôt GitHub : + +```bash +echo "Test depuis l'utilisateur numéro 2 du VPS" >> testfile.txt +git add testfile.txt +git commit -m "Ajout d'un test depuis le VPS" +git push origin main +``` + +4\. Vérifiez l'exécution du workflow sur GitHub + +Rendez-vous dans l'onglet `Actions` de votre dépôt GitHub et vérifiez que le workflow a été déclenché automatiquement après le `git push`. Si le workflow réussit, les modifications seront synchronisées dans le dossier de votre site web (`/var/www/html`). + +5\. Confirmer la synchronisation dans `/var/www/html` + +Retournez dans votre répertoire principal de déploiement (`/var/www/html`) et vérifiez que le fichier `testfile.txt` est bien présent : + +```bash +ls /var/www/html +cat /var/www/html/testfile.txt +``` + +### Conclusion + +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. + +## Aller plus loin + +[Premiers pas avec un VPS](/pages/bare_metal_cloud/virtual_private_servers/starting_with_a_vps) + +[Sécuriser un VPS](/pages/bare_metal_cloud/virtual_private_servers/secure_your_vps) + +Pour des prestations spécialisées (référencement, développement, etc), contactez les [partenaires OVHcloud](/links/partner). + +Échangez avec notre [communauté d'utilisateurs](/links/community). \ No newline at end of file diff --git a/pages/bare_metal_cloud/virtual_private_servers/deploy-website-github-actions/guide.it-it.md b/pages/bare_metal_cloud/virtual_private_servers/deploy-website-github-actions/guide.it-it.md new file mode 100644 index 00000000000..2a3ef080c36 --- /dev/null +++ b/pages/bare_metal_cloud/virtual_private_servers/deploy-website-github-actions/guide.it-it.md @@ -0,0 +1,353 @@ +--- +title: "Automating the deployment of your website on your VPS via GitHub Actions" +excerpt: 'Find out how to deploy and automate your website’s code via GitHub Actions on an OVHcloud VPS' +updated: 2025-01-21 +--- + +## 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](/links/bare-metal/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) + +> [!warning] +> +> 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. + +## Instructions + +> [!primary] +> 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). + +**The main steps of the guide will be the following:** + +- [Configure SSH access for GitHub Actions](#configure-ssh) +- [Add the private key to GitHub](#add-private-key-github) +- [Initialize the GitHub repository (optional)](#init-github-repo) +- [Configure GitHub Actions for Automatic Deployment](#configure-github-actions) +- [Check and test the GitHub Actions workflow](#verify-workflow-github) +- [Conclusion](#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: + +```bash +ssh-keygen -t rsa -b 4096 -C "github-actions" -f /home//.ssh/deploy_key +``` + +Replace `` 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//.ssh/deploy_key`: private key +- `/home//.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: + +```bash +mkdir -p /home//.ssh +chmod 700 /home//.ssh +``` + +2\. Add the public key to the `authorized_keys` file: + +```bash +cat /home//.ssh/deploy_key.pub >> /home//.ssh/authorized_keys +chmod 600 /home//.ssh/authorized_keys +``` + +3\. Test the SSH connection + +Test the SSH connection with the private key to confirm that the access is functional: + +```bash +ssh -i /home//.ssh/deploy_key @ +``` + +Replace `` with the user configured to connect to your VPS and `` 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: + +```bash +cat /home//.ssh/deploy_key.pub +``` + +Follow the steps from the "Adding a new SSH key to your account" section of the [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. + +#### 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`: + +```bash +nano ~/.ssh/config +``` + +Add this configuration for GitHub access: + +```console +Host github.com + HostName github.com + User git + IdentityFile /home//.ssh/deploy_key +``` + +Save and exit the editor. + +Test the SSH connection with GitHub: + +```bash +ssh -T git@github.com +``` + +You should see a message like this: + +```console +Hi ! You've successfully authenticated, but GitHub does not provide shell access. +``` + +### Add the private key to GitHub + +Copy the content of the generated private key to your VPS with: + +```bash +cat /home//.ssh/deploy_key +``` + +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](https://docs.github.com/gb/en/actions/security-for-github-actions/security-guides/using-secrets-in-github-actions#creating-secrets-for-a-repository). + +### Initialize GitHub repository (optional) + +> [!primary] +> If you already have a GitHub repository containing your website’s code, go to the [next step](#configure-github-actions). + +#### Create a GitHub repository + +To create a GitHub repository, follow the steps from the "Create a repository" page of the [GitHub official documentation](https://docs.github.com/gb/en/repositories/creating-and-managing-repositories/creating-a-new-repository){.external}. + +#### Initialize the Git repository on the VPS + +1\. Log in to your VPS via SSH: + +```bash +ssh @ +``` + +2\. Install Git: + +```bash +sudo apt update && sudo apt install git -y +``` + +3\. Initialize a Git repository in your website directory: + +```bash +cd /var/www/html +sudo git init +sudo git remote add origin git@github.com:/.git +``` + +Replace `` with your GitHub username and `` with the name of your GitHub repository. + +4\. Add the files and make a first commit: + +```bash +git add . +git commit -m "Initial commit" +git branch -M main +git push -u origin main +``` + +### 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): + +```bash +cd /var/www/html +mkdir -p .github/workflows +``` + +2\. Create a workflow file + +Create a `deploy.yml` file in the `.github/workflows` directory: + +```bash +nano .github/workflows/deploy.yml +``` + +3\. Configure the `deploy.yml` file + +To configure the deployment pipeline, add the following content to the `deploy.yml` file: + +```yaml +name: Deploy to VPS + +on: + push: + branches: + - main + +jobs: + deploy: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Copy files to VPS + env: + SSH_PRIVATE_KEY: ${{ secrets.DEPLOY_KEY }} + run: | + mkdir -p ~/.ssh + echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa + chmod 600 ~/.ssh/id_rsa + ssh-keyscan -H >> ~/.ssh/known_hosts + rsync -avz --delete ./ @:/var/www/html/ +``` + +Replace the following: + +- ``: by the IP address of your VPS. +- ``: 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: + +```bash +git add .github/workflows/deploy.yml +git commit -m "Adding GitHub Actions workflow for deployment" +git push origin main +``` + +### 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: + +```bash +sudo chown -R :www-data /var/www/html +sudo chmod -R 775 /var/www/html +``` + +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: + +```bash +rsync -avz --no-times --exclude='.git*' -e "ssh -i ./deploy_key -o StrictHostKeyChecking=no" ./ @:/var/www/html/ +``` + +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/html` on your VPS via `rsync`. + +##### **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: + +```bash +mkdir /home//test-github-actions +cd /home//test-github-actions +``` + +2\. Clone the GitHub repository in this directory + +```bash +git clone git@github.com:/github-actions.git . +``` + +If your repository is already in HTTPS , update it to use SSH: + +```bash +git remote set-url origin git@github.com:/github-actions.git +``` + +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: + +```bash +echo "Test from VPS user number 2" >> testfile.txt +git add testfile.txt +git commit -m "Add a test from the VPS" +git push origin main +``` + +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: + +```bash +ls /var/www/html +cat /var/www/html/testfile.txt +``` + +### 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 + +[Getting started with a VPS](/pages/bare_metal_cloud/virtual_private_servers/starting_with_a_vps) + +[Securing a VPS](/pages/bare_metal_cloud/virtual_private_servers/secure_your_vps) + +For specialized services (SEO, development, etc.), contact the [OVHcloud partners](/links/partner). + +Join our [community of users](/links/community). \ No newline at end of file diff --git a/pages/bare_metal_cloud/virtual_private_servers/deploy-website-github-actions/guide.pl-pl.md b/pages/bare_metal_cloud/virtual_private_servers/deploy-website-github-actions/guide.pl-pl.md new file mode 100644 index 00000000000..2a3ef080c36 --- /dev/null +++ b/pages/bare_metal_cloud/virtual_private_servers/deploy-website-github-actions/guide.pl-pl.md @@ -0,0 +1,353 @@ +--- +title: "Automating the deployment of your website on your VPS via GitHub Actions" +excerpt: 'Find out how to deploy and automate your website’s code via GitHub Actions on an OVHcloud VPS' +updated: 2025-01-21 +--- + +## 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](/links/bare-metal/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) + +> [!warning] +> +> 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. + +## Instructions + +> [!primary] +> 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). + +**The main steps of the guide will be the following:** + +- [Configure SSH access for GitHub Actions](#configure-ssh) +- [Add the private key to GitHub](#add-private-key-github) +- [Initialize the GitHub repository (optional)](#init-github-repo) +- [Configure GitHub Actions for Automatic Deployment](#configure-github-actions) +- [Check and test the GitHub Actions workflow](#verify-workflow-github) +- [Conclusion](#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: + +```bash +ssh-keygen -t rsa -b 4096 -C "github-actions" -f /home//.ssh/deploy_key +``` + +Replace `` 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//.ssh/deploy_key`: private key +- `/home//.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: + +```bash +mkdir -p /home//.ssh +chmod 700 /home//.ssh +``` + +2\. Add the public key to the `authorized_keys` file: + +```bash +cat /home//.ssh/deploy_key.pub >> /home//.ssh/authorized_keys +chmod 600 /home//.ssh/authorized_keys +``` + +3\. Test the SSH connection + +Test the SSH connection with the private key to confirm that the access is functional: + +```bash +ssh -i /home//.ssh/deploy_key @ +``` + +Replace `` with the user configured to connect to your VPS and `` 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: + +```bash +cat /home//.ssh/deploy_key.pub +``` + +Follow the steps from the "Adding a new SSH key to your account" section of the [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. + +#### 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`: + +```bash +nano ~/.ssh/config +``` + +Add this configuration for GitHub access: + +```console +Host github.com + HostName github.com + User git + IdentityFile /home//.ssh/deploy_key +``` + +Save and exit the editor. + +Test the SSH connection with GitHub: + +```bash +ssh -T git@github.com +``` + +You should see a message like this: + +```console +Hi ! You've successfully authenticated, but GitHub does not provide shell access. +``` + +### Add the private key to GitHub + +Copy the content of the generated private key to your VPS with: + +```bash +cat /home//.ssh/deploy_key +``` + +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](https://docs.github.com/gb/en/actions/security-for-github-actions/security-guides/using-secrets-in-github-actions#creating-secrets-for-a-repository). + +### Initialize GitHub repository (optional) + +> [!primary] +> If you already have a GitHub repository containing your website’s code, go to the [next step](#configure-github-actions). + +#### Create a GitHub repository + +To create a GitHub repository, follow the steps from the "Create a repository" page of the [GitHub official documentation](https://docs.github.com/gb/en/repositories/creating-and-managing-repositories/creating-a-new-repository){.external}. + +#### Initialize the Git repository on the VPS + +1\. Log in to your VPS via SSH: + +```bash +ssh @ +``` + +2\. Install Git: + +```bash +sudo apt update && sudo apt install git -y +``` + +3\. Initialize a Git repository in your website directory: + +```bash +cd /var/www/html +sudo git init +sudo git remote add origin git@github.com:/.git +``` + +Replace `` with your GitHub username and `` with the name of your GitHub repository. + +4\. Add the files and make a first commit: + +```bash +git add . +git commit -m "Initial commit" +git branch -M main +git push -u origin main +``` + +### 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): + +```bash +cd /var/www/html +mkdir -p .github/workflows +``` + +2\. Create a workflow file + +Create a `deploy.yml` file in the `.github/workflows` directory: + +```bash +nano .github/workflows/deploy.yml +``` + +3\. Configure the `deploy.yml` file + +To configure the deployment pipeline, add the following content to the `deploy.yml` file: + +```yaml +name: Deploy to VPS + +on: + push: + branches: + - main + +jobs: + deploy: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Copy files to VPS + env: + SSH_PRIVATE_KEY: ${{ secrets.DEPLOY_KEY }} + run: | + mkdir -p ~/.ssh + echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa + chmod 600 ~/.ssh/id_rsa + ssh-keyscan -H >> ~/.ssh/known_hosts + rsync -avz --delete ./ @:/var/www/html/ +``` + +Replace the following: + +- ``: by the IP address of your VPS. +- ``: 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: + +```bash +git add .github/workflows/deploy.yml +git commit -m "Adding GitHub Actions workflow for deployment" +git push origin main +``` + +### 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: + +```bash +sudo chown -R :www-data /var/www/html +sudo chmod -R 775 /var/www/html +``` + +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: + +```bash +rsync -avz --no-times --exclude='.git*' -e "ssh -i ./deploy_key -o StrictHostKeyChecking=no" ./ @:/var/www/html/ +``` + +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/html` on your VPS via `rsync`. + +##### **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: + +```bash +mkdir /home//test-github-actions +cd /home//test-github-actions +``` + +2\. Clone the GitHub repository in this directory + +```bash +git clone git@github.com:/github-actions.git . +``` + +If your repository is already in HTTPS , update it to use SSH: + +```bash +git remote set-url origin git@github.com:/github-actions.git +``` + +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: + +```bash +echo "Test from VPS user number 2" >> testfile.txt +git add testfile.txt +git commit -m "Add a test from the VPS" +git push origin main +``` + +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: + +```bash +ls /var/www/html +cat /var/www/html/testfile.txt +``` + +### 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 + +[Getting started with a VPS](/pages/bare_metal_cloud/virtual_private_servers/starting_with_a_vps) + +[Securing a VPS](/pages/bare_metal_cloud/virtual_private_servers/secure_your_vps) + +For specialized services (SEO, development, etc.), contact the [OVHcloud partners](/links/partner). + +Join our [community of users](/links/community). \ No newline at end of file diff --git a/pages/bare_metal_cloud/virtual_private_servers/deploy-website-github-actions/guide.pt-pt.md b/pages/bare_metal_cloud/virtual_private_servers/deploy-website-github-actions/guide.pt-pt.md new file mode 100644 index 00000000000..2a3ef080c36 --- /dev/null +++ b/pages/bare_metal_cloud/virtual_private_servers/deploy-website-github-actions/guide.pt-pt.md @@ -0,0 +1,353 @@ +--- +title: "Automating the deployment of your website on your VPS via GitHub Actions" +excerpt: 'Find out how to deploy and automate your website’s code via GitHub Actions on an OVHcloud VPS' +updated: 2025-01-21 +--- + +## 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](/links/bare-metal/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) + +> [!warning] +> +> 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. + +## Instructions + +> [!primary] +> 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). + +**The main steps of the guide will be the following:** + +- [Configure SSH access for GitHub Actions](#configure-ssh) +- [Add the private key to GitHub](#add-private-key-github) +- [Initialize the GitHub repository (optional)](#init-github-repo) +- [Configure GitHub Actions for Automatic Deployment](#configure-github-actions) +- [Check and test the GitHub Actions workflow](#verify-workflow-github) +- [Conclusion](#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: + +```bash +ssh-keygen -t rsa -b 4096 -C "github-actions" -f /home//.ssh/deploy_key +``` + +Replace `` 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//.ssh/deploy_key`: private key +- `/home//.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: + +```bash +mkdir -p /home//.ssh +chmod 700 /home//.ssh +``` + +2\. Add the public key to the `authorized_keys` file: + +```bash +cat /home//.ssh/deploy_key.pub >> /home//.ssh/authorized_keys +chmod 600 /home//.ssh/authorized_keys +``` + +3\. Test the SSH connection + +Test the SSH connection with the private key to confirm that the access is functional: + +```bash +ssh -i /home//.ssh/deploy_key @ +``` + +Replace `` with the user configured to connect to your VPS and `` 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: + +```bash +cat /home//.ssh/deploy_key.pub +``` + +Follow the steps from the "Adding a new SSH key to your account" section of the [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. + +#### 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`: + +```bash +nano ~/.ssh/config +``` + +Add this configuration for GitHub access: + +```console +Host github.com + HostName github.com + User git + IdentityFile /home//.ssh/deploy_key +``` + +Save and exit the editor. + +Test the SSH connection with GitHub: + +```bash +ssh -T git@github.com +``` + +You should see a message like this: + +```console +Hi ! You've successfully authenticated, but GitHub does not provide shell access. +``` + +### Add the private key to GitHub + +Copy the content of the generated private key to your VPS with: + +```bash +cat /home//.ssh/deploy_key +``` + +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](https://docs.github.com/gb/en/actions/security-for-github-actions/security-guides/using-secrets-in-github-actions#creating-secrets-for-a-repository). + +### Initialize GitHub repository (optional) + +> [!primary] +> If you already have a GitHub repository containing your website’s code, go to the [next step](#configure-github-actions). + +#### Create a GitHub repository + +To create a GitHub repository, follow the steps from the "Create a repository" page of the [GitHub official documentation](https://docs.github.com/gb/en/repositories/creating-and-managing-repositories/creating-a-new-repository){.external}. + +#### Initialize the Git repository on the VPS + +1\. Log in to your VPS via SSH: + +```bash +ssh @ +``` + +2\. Install Git: + +```bash +sudo apt update && sudo apt install git -y +``` + +3\. Initialize a Git repository in your website directory: + +```bash +cd /var/www/html +sudo git init +sudo git remote add origin git@github.com:/.git +``` + +Replace `` with your GitHub username and `` with the name of your GitHub repository. + +4\. Add the files and make a first commit: + +```bash +git add . +git commit -m "Initial commit" +git branch -M main +git push -u origin main +``` + +### 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): + +```bash +cd /var/www/html +mkdir -p .github/workflows +``` + +2\. Create a workflow file + +Create a `deploy.yml` file in the `.github/workflows` directory: + +```bash +nano .github/workflows/deploy.yml +``` + +3\. Configure the `deploy.yml` file + +To configure the deployment pipeline, add the following content to the `deploy.yml` file: + +```yaml +name: Deploy to VPS + +on: + push: + branches: + - main + +jobs: + deploy: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Copy files to VPS + env: + SSH_PRIVATE_KEY: ${{ secrets.DEPLOY_KEY }} + run: | + mkdir -p ~/.ssh + echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa + chmod 600 ~/.ssh/id_rsa + ssh-keyscan -H >> ~/.ssh/known_hosts + rsync -avz --delete ./ @:/var/www/html/ +``` + +Replace the following: + +- ``: by the IP address of your VPS. +- ``: 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: + +```bash +git add .github/workflows/deploy.yml +git commit -m "Adding GitHub Actions workflow for deployment" +git push origin main +``` + +### 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: + +```bash +sudo chown -R :www-data /var/www/html +sudo chmod -R 775 /var/www/html +``` + +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: + +```bash +rsync -avz --no-times --exclude='.git*' -e "ssh -i ./deploy_key -o StrictHostKeyChecking=no" ./ @:/var/www/html/ +``` + +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/html` on your VPS via `rsync`. + +##### **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: + +```bash +mkdir /home//test-github-actions +cd /home//test-github-actions +``` + +2\. Clone the GitHub repository in this directory + +```bash +git clone git@github.com:/github-actions.git . +``` + +If your repository is already in HTTPS , update it to use SSH: + +```bash +git remote set-url origin git@github.com:/github-actions.git +``` + +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: + +```bash +echo "Test from VPS user number 2" >> testfile.txt +git add testfile.txt +git commit -m "Add a test from the VPS" +git push origin main +``` + +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: + +```bash +ls /var/www/html +cat /var/www/html/testfile.txt +``` + +### 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 + +[Getting started with a VPS](/pages/bare_metal_cloud/virtual_private_servers/starting_with_a_vps) + +[Securing a VPS](/pages/bare_metal_cloud/virtual_private_servers/secure_your_vps) + +For specialized services (SEO, development, etc.), contact the [OVHcloud partners](/links/partner). + +Join our [community of users](/links/community). \ No newline at end of file diff --git a/pages/bare_metal_cloud/virtual_private_servers/deploy-website-github-actions/meta.yaml b/pages/bare_metal_cloud/virtual_private_servers/deploy-website-github-actions/meta.yaml new file mode 100644 index 00000000000..08795da5724 --- /dev/null +++ b/pages/bare_metal_cloud/virtual_private_servers/deploy-website-github-actions/meta.yaml @@ -0,0 +1,2 @@ +id: 0a3da1c8-3ef0-4ee3-a9fe-86b28ef3eb2d +full_slug: vps-deploy-website-github-actions \ No newline at end of file diff --git a/pages/index.md b/pages/index.md index ad3f1e4e9f4..bafd67e53a7 100644 --- a/pages/index.md +++ b/pages/index.md @@ -268,6 +268,7 @@ + [How to install WordPress with Docker on a VPS or a dedicated server](bare_metal_cloud/virtual_private_servers/install_wordpress_docker_on_vps) + [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) + [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) + + [Automating the deployment of your website on your VPS via GitHub Actions](bare_metal_cloud/virtual_private_servers/deploy-website-github-actions) + [Managed Bare Metal](products/bare-metal-cloud-managed-bare-metal) + [OVHcloud services and options](bare-metal-cloud-managed-bare-metal-ovhcloud-services-and-options) + [Setting up a VPN for OVHcloud Zerto DRP](bare_metal_cloud/managed_bare_metal/zerto-virtual-replication-customer-to-ovhcloud)