GitHub Action deploy-on-vps: publishing your site to a VPS over SSH

API & DocumentationInfrastructure & DevOps·· 124 views· 4 min read

This GitHub Action publishes your site on your MozOut VPS. It asks our API which server to use and what its IP is, checks that it is running, and copies the files with rsync over SSH.

What it does

With a single active VPS on the account, it uses that one. With several, you choose one with server-id (the id of its service) or server-name (its hostname or IP).

If the VPS is starting or restarting, the action waits. A suspended VPS stops the deploy with a clear message. With start-if-stopped, a stopped VPS is started through the API.

The copy takes exclude (paths to leave out), delete (delete files on the VPS that are not in source), dry-run and commands run on the VPS before and after the copy (pre-command and post-command). .git/ and .github/ never go.

What you need

A VPS on your account, a Linux or macOS runner (they have rsync, ssh and Python 3, and nothing is downloaded) and an API key created in the API Center of My MozOut.

Installing, step by step

1 Create the API key with the vps:read permission and save it in your repository as the secret MOZOUT_API_KEY.
2 Make a key pair just for deploys (ssh-keygen -t ed25519 -f deploy_key -N ""), add deploy_key.pub to ~/.ssh/authorized_keys of the user on the VPS, and save the private key as the secret VPS_SSH_KEY.
3 Recommended: run ssh-keyscan -p 22 YOUR_VPS_IP and save the output as the secret VPS_KNOWN_HOSTS. Without it, the action trusts the key the server shows at deploy time.
4 Create .github/workflows/deploy.yml as in the example, with the folder to publish in source and the folder on the VPS in target.

name: Deploy

on:
push:
branches: [main]

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- uses: mozout-com/deploy-on-vps@v1
with:
api-key: ${{ secrets.MOZOUT_API_KEY }}
ssh-private-key: ${{ secrets.VPS_SSH_KEY }}
known-hosts: ${{ secrets.VPS_KNOWN_HOSTS }}
source: ./public
target: /var/www/html
delete: true
exclude: |
.env
uploads/
post-command: systemctl reload nginx

If your site needs a build, do it in a step before the action. The full list of inputs and the releases are in the action repository, and the latest release tells you what changed.

The API key and its permissions

The key needs vps:read. Add vps:write only if you want to use start-if-stopped.

If your key only accepts some IP addresses, remember that GitHub runners change IP. Use a self-hosted runner or leave the IP list empty for this key.

Testing without spending

With a sandbox_ key the API calls are simulated: starting a stopped server does nothing. The copy itself is real, because it goes over SSH. To see what would be copied without copying, use dry-run.

Good to know

Careful with delete: with delete: true, files on the VPS that are not in source are deleted. Put what must stay in exclude, such as .env or the uploads/ folder.

User and port: by default it connects as root on port 22. Change them with ssh-user and ssh-port. To connect through an additional IP, use host.

Outputs: server-id, server-name, ip and power-state.

Help

If the deploy fails with an error you do not understand, open a support ticket and copy the message from the workflow log. The servers part of the API is described in servers, in the API documentation. The code is in the action repository, under the MIT license.

Ready to publish your site on every push?

Download the latest release

SEE ALSO

Creating an SSH key

Copying files with rsync

Controlling a VPS through the API

The repository on MozOut Git

SDKs and integrations, in the API documentation

RECOMMENDED PRODUCT

VPS server with root access

Resources of your own, the OS you choose, reinstall whenever you like. from ₦12,600.00/mo (3-year plan, with coupon)

See plans
Was this answer helpful? Your answer helps us improve this article.