DEV Community

Michael Lohr
Michael Lohr

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

1

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

stages:
- build
- deploy
build:
stage: build
image: node:11
before_script:
- npm install --progress=false --unsafe-perm
script:
- npm run build
artifacts:
expire_in: 100 yrs
paths:
- public
cache:
paths:
- node_modules/
deploy:
stage: deploy
only:
- master
image: debian:stable
before_script:
- apt-get update
- apt-get install -y git
- mkdir -p ~/.ssh
- echo "$SSH_PRIVATE_KEY" | tr -d '\r' > ~/.ssh/id_rsa
- chmod 600 ~/.ssh/id_rsa
- ssh-keyscan -H gitlab.com >> ~/.ssh/known_hosts
- git config --global user.email "gitlab-ci@doesnotmatter.com"
- git config --global user.name "GitLab CI"
script:
- git clone --depth 1 --single-branch -b deploy git@gitlab.com:yourname/yourrepo.git deploy
- mv deploy/.git deploy-.git
- rm -rf deploy
- mkdir deploy
- mv deploy-.git deploy/.git
- cd deploy
- cp -r ../public/* .
- git add .
- git diff --staged --quiet || git commit -m "Update pages"
- git push
view raw .gitlab-ci.yml hosted with ❤ by GitHub

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

name: Deploy LaTeX document
on:
push:
branches:
- master
jobs:
build_resume:
runs-on: ubuntu-latest
steps:
- name: Setup SSH
uses: webfactory/ssh-agent@v0.5.1
with:
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}
- name: Setup Git Client
run: |
ssh-keyscan -H github.com >> ~/.ssh/known_hosts
git config --global user.email "github-actions@doesnotmatter.dev"
git config --global user.name "GitHub Actions"
- name: Set up Git repository
uses: actions/checkout@v1
- name: Compile LaTeX document
uses: xu-cheng/latex-action@master
with:
root_file: resume.tex
args: -xelatex -file-line-error -interaction=nonstopmode -outdir=out
- name: Collect artifacts
run: |
mkdir dist
cp out/*.pdf dist/
- name: Push artifacts
run: |
git clone --depth 1 --single-branch -b deploy git@github.com:yourname/yourrepo.git deploy
mv deploy/.git deploy-.git
rm -rf deploy
mkdir deploy
mv deploy-.git deploy/.git
cd deploy
cp -R ../dist/* .
git add .
git diff --staged --quiet || git commit -m "Update Artifacts"
git push
view raw deploy.yml hosted with ❤ by GitHub

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!

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (0)

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay