Automatically deploy websites to Plesk using GitHub Actions or GitLab CI
Tags: github, gitlab, plesk, continuousdeployment
Published: 2021-04-07 13:45:43

[Plesk](https://www.plesk.com/) is a commonly used webspace control panel, and thanks to the free [Plesk Git extension](https://www.plesk.com/extensions/git/), it is possible to automatically deploy all your changes from any CI.

> Dreaming about a setup where you just push your changes, and they will be immateriality live?

It’s possible! I use Hugo and NPM to build my static websites and deploy them automatically from GitHub/GitLab to my Plesk webspace.

So how is this possible? The secret is the [Plesk Git extension](https://www.plesk.com/extensions/git/), which can pull any git repository as soon as a webhook is called (this extension has to be installed by your provider, but with most providers, it is available). For that we have to push our built artifact to another branch (similar to how github-pages works), which is then deployed to the webspace.

## Step 1: Modify your CI

Here it depends what on CI you use. I will provide examples of how it’s done with GitHub Actions and GitLab CI. Generally, the steps are:

1. Create an ssh key (deploy key) and put it into a CI variable to grant the CI push access to the repo
1. Use an image with git or install git and set up the key
1. Create a new branch, e.g., `deploy`
1. Clone the deploy branch and delete everything besides the `.git` folder
1. Move the built artifact from the previous build step to the cloned (and now empty) folder
1. add, commit, push

### Instructions: GitHub

First, generate an ssh key. With GitHub you can then create a deploy key under `Settings->Deploy keys` by inserting the public key. Then make a repository secret named `SSH_PRIVATE_KEY` under `Settings->Secrets` and insert the private key.

Your Action definition then might look like this (don’t forget to change the repository URL):
{%  gist https://gist.github.com/michidk/47460dc594c441061a5b82fe1415aac7 file=.gitlab-ci.yml %}

### Instructions: GitLab

First, generate an ssh key. With GitLab you can then create a deploy key under `Settings-> Repository->Deploy keys` by inserting the public key. Then make a protected variable named `SSH_PRIVATE_KEY` under `Settings->CI/CD->Variables` and insert the private key.

Your `.gitlab-ci.yml` CI definition then might look like this (don’t forget to change the repository URL):
{% gist https://gist.github.com/michidk/7e5becebb0ab64f325582749f2289f56 file=deploy.yml %}

## Step 2: Create website in Plesk

Next, create the website in Plesk and add a Git repository. If the repository is private, you have to add the public key given by Plesk to GitHub/GitLab as the deploy key for the SSH connection. Don’t forget to select the deploy branch and correct folder on your webspace! Instruction can be found here.

Plesk always clones the main branch for the first time before you switched to the other branch. When switching, the old files won’t get deleted, so you might have to clean up once and remove some of the old files from the webspace folder.

## Step 3: Configure the webhook

Plesk will give you a webhook-URL under the repository settings of the git extension in Plesk. Then configure GitHub/GitLab to call that webhook URL every time a push happens.

With GitHub and GitLab, this can be done under `Settings->Webhooks` . Select push events and test it.
Step 4: Profit

That’s it! This should basically work with every CI that has access to your repository and the git Plesk extension.

Happy coding!