Deploy Publish Site to Server Using Git

Deploy Publish Site to Server Using Git

Publish by Sundell provides two deploy methods: gitHub(_: branch: useSSH:) and git(_: branch: ), one for GitHub and the other for generic git servers.

It is quite straight forward to deploy using GitHub (Setup a repository on GitHub), but deploying to generic git servers require some additional steps.

This is not a tutorial for using Git. So I would like to assume you already have your Git installed and setup (with SSH key) on both of your local OS and server. If you haven't, the official documentation is a good point to start: Git Book

Create a New Git Repository on Server

Create a folder on your server, at a location you that have write permission and initiate the repository. This can be your home folder, the home folder of git user, or any location you like.

1 2 3 4
cd /path/to/folder mkdir MyPublishSite.git cd MyPublishSite.git git init --bare

Where /path/to/folder is the location you want the repository to be, and MyPublishSite.git is the name of git repository.

Add a Post-Receive Hook

Create a file named post-receive in the hooks directory of the new created repository.

1 2
cd hooks vim post-receive

Put the following content in the file:

1 2
#!/bin/sh GIT_WORK_TREE=/path/to/website/root git checkout -f

Where /path/to/website/root is the root directory of your website. On some systems, this may be /var/www/example.com/public_html

Then, make the file executable.

1
chmod +x post-receive

Setup Deploy Pipeline Step in Publish

Add a deploy step using git in your pipeline:

1 2 3 4
try MyPublishSite().publish(using: [ ... .deploy(using: .git("username@yourserver:/path/to/folder/MyPublishSite.git/", branch: "master")) }

Where username is the user name on server (your user name or git, depends on your setup). yourserver is the server name or address, and /path/to/folder/MyPublishSite.git/ is the git repository you just created.

Deploy

Run the package to generate the website (without --deploy flag), and then run the package to deploy website to server, with --deploy flag.

Done.

Tagged with: