DEV Community

Michael Lohr
Michael Lohr

Posted on • Edited on • Originally published at blog.lohr.dev

Automatically deploy websites to Plesk using GitHub Actions or GitLab CI

Plesk is a commonly used webspace control panel, and thanks to the free Plesk Git extension, 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, 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
  2. Use an image with git or install git and set up the key
  3. Create a new branch, e.g., deploy
  4. Clone the deploy branch and delete everything besides the .git folder
  5. Move the built artifact from the previous build step to the cloned (and now empty) folder
  6. 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):

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):

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!

Top comments (0)