• Stars
    star
    205
  • Rank 191,264 (Top 4 %)
  • Language
    Go
  • License
    MIT License
  • Created over 9 years ago
  • Updated about 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

git middleware for Caddy

git

Middleware for Caddy.

Build Status

git clones a git repository into the site. This makes it possible to deploy your site with a simple git push.

The git directive starts a service routine that runs during the lifetime of the server. When the service starts, it clones the repository. While the server is still up, it pulls the latest every so often. You can also set up a webhook to pull immediately after a push. In regular git fashion, a pull only includes changes, so it is very efficient.

If a pull fails, the service will retry up to three times. If the pull was not successful by then, it won't try again until the next interval.

Requirements: This directive requires git to be installed. Also, private repositories may only be accessed from Linux or Mac systems. (Contributions are welcome that make private repositories work on Windows.)

Syntax

git repo [path]
  • repo is the URL to the repository; SSH and HTTPS URLs are supported
  • path is the path, relative to site root, to clone the repository into; default is site root

This simplified syntax pulls from master every 3600 seconds (1 hour) and only works for public repositories.

For more control or to use a private repository, use the following syntax:

git [repo path] {
	repo        repo
	path        path
	branch      branch
	key         key
	interval    interval
	clone_args  args
	pull_args   args
	hook        path secret
	hook_type   type
	then        command [args...]
	then_long   command [args...]
}
  • repo is the URL to the repository; SSH and HTTPS URLs are supported.
  • path is the path to clone the repository into; default is site root. It can be absolute or relative (to site root).
  • branch is the branch or tag to pull; default is master branch. {latest} is a placeholder for latest tag which ensures the most recent tag is always pulled.
  • key is the path to the SSH private key; only required for private repositories.
  • interval is the number of seconds between pulls; default is 3600 (1 hour), minimum 5. An interval of -1 disables periodic pull.
  • clone_args is the additional cli args to pass to git clone e.g. --depth=1. git clone is called when the source is being fetched the first time.
  • pull_args is the additional cli args to pass to git pull e.g. -s recursive -X theirs. git pull is used when the source is being updated.
  • path and secret are used to create a webhook which pulls the latest right after a push. This is limited to the supported webhooks. secret is currently supported for GitHub, Gitlab and Travis hooks only.
  • type is webhook type to use. The webhook type is auto detected by default but it can be explicitly set to one of the supported webhooks. This is a requirement for generic webhook.
  • command is a command to execute after successful pull; followed by args which are any arguments to pass to the command. You can have multiple lines of this for multiple commands. then_long is for long executing commands that should run in background.

Each property in the block is optional. The path and repo may be specified on the first line, as in the first syntax, or they may be specified in the block with other values.

Webhooks

A webhook is an interface between a git repository and an external server. On Github, the simplest webhook makes a request to a 3rd-party URL when the repository is pushed to. You can set up a Github webhook at github.com/[username]/[repository]/settings/hooks, and a Travis webhook in your .travis.yml. Make sure your webhooks are set to deliver JSON data!

The JSON payload should include at least a ref key, but all the default supported webhooks will handle this for you.

The hook URL is the URL Caddy will watch for requests on; if your url is, for example /__github_webhook__ and Caddy is hosting https://example.com, when a request is made to https://example.com/__github_webhook__ Caddy will intercept this request and check that the secret in the request (configured wherever you configure your webhooks) and the secret in your Caddyfile match. If the request is valid, Caddy will git pull its local copy of the repo to update your site as soon as you push new data. It may be useful to then use a post-merge script or another git hook to rebuild any needed files (updating SASS styles and regenerating Hugo sites are common use-cases), although the then parameter can also be used for simpler cases.

Note that because the hook URL is used as an API endpoint, you shouldn't have any content / files at its corresponding location in your website.

Supported Webhooks

Examples

Public repository pulled into site root every hour:

git github.com/user/myproject

Public repository pulled into the "subfolder" directory in the site root:

git github.com/user/myproject subfolder

Private repository pulled into the "subfolder" directory with tag v1.0 once per day:

git {
	repo     [email protected]:user/myproject
	branch   v1.0
	key      /home/user/.ssh/id_rsa
	path     subfolder
	interval 86400
}

Generate a static site with Hugo after each pull:

git github.com/user/site {
	path  ../
	then  hugo --destination=/home/user/hugosite/public
}

Part of a Caddyfile for a PHP site that gets changes from a private repo:

git [email protected]:user/myphpsite {
	key /home/user/.ssh/id_rsa
}
fastcgi / 127.0.0.1:9000 php

Specifying a webhook:

git [email protected]:user/site {
	hook /webhook secret-password
}

You might need quotes "secret-password" around your secret if it contains any special characters, or you get an error.

Generic webhook payload: <branch> is branch name e.g. master.

{
	"ref" : "refs/heads/<branch>"
}

More Repositories

1

colima

Container runtimes on macOS (and Linux) with minimal setup
Go
18,415
star
2

ishell

Library for creating interactive cli applications.
Go
1,671
star
3

caddy-docker

Docker container for Caddy
Shell
768
star
4

dockward

Port forwarding tool for Docker containers
Go
137
star
5

caddy-json-schema

JSON schema generator for Caddy v2
Go
130
star
6

caddy-exec

Caddy v2 module for running one-off commands
Go
102
star
7

DevReload

Auto reload for ASP.NET core development
C#
61
star
8

autoplank

automatically move Plank to the active monitor
Go
51
star
9

semaphore

Wrapping sync.Mutex for familiar semaphore api
Go
44
star
10

runcmd

Wrapper for cli apps. Write less commands.
Go
43
star
11

caddyplug

Experimental Caddy plugin manager using Go plugins
Go
40
star
12

river

River is a simple and lightweight REST server
Go
37
star
13

gocalc

Simple Go Calculator
Go
34
star
14

dotfiles

dotfiles. zsh, neovim, tmux, i3 e.t.c.
Vim Script
33
star
15

caddy-yaml

Alternative Caddy YAML config adapter with extra features
Go
27
star
16

crostini-docker

Docker for Crostini with fixed binariest that work
Shell
25
star
17

caddy-json-parse

Caddy v2 module for parsing json request
Go
18
star
18

parent

A minimal process wrapper
Go
15
star
19

caddy-named-routes

named routes support for Caddy v2
Go
14
star
20

lineprefix

io.Writer wrapper with line prefix and color customizations.
Go
13
star
21

launchar

minimal GTK application launcher
Vala
11
star
22

hello-elementary

quick start elementary OS application
Vala
10
star
23

caddy-hmac

Caddy v2 hmac signature validation middleware
Go
8
star
24

cmdplay

Prototype shell session recorder and player
Go
5
star
25

bgrun

Run processes in background
Python
5
star
26

hello-caddy

Sample external middleware for Caddy
Go
4
star
27

colima-core

Dependencies for Colima
Shell
4
star
28

caddyserver.com

Source of the Caddy website
HTML
4
star
29

forloopui2017

Code used for forloop UI 2017 talk
Go
3
star
30

webtest

Basically web repo to test servers
HTML
3
star
31

atom-go-imports

Auto import Go packages via the goimports tool
CoffeeScript
3
star
32

errs

convenience wrapper for chaining multiple error returning functions.
Go
3
star
33

injekt

Pluggable service injector
Go
3
star
34

hcl2json

HCL to JSON converter
Go
3
star
35

servefile

File Server for any directory or file
Go
2
star
36

buildsrv

The Caddy build server
Go
2
star
37

gistdl

Download github gists as files
Python
2
star
38

ghrelease

A tool to download latest release archive for a GitHub project using GitHub API.
Go
2
star
39

node-graceful

Gracefully shutdown your node app. Graceful interrupts SIGTERM and SIGINT signals.
JavaScript
2
star
40

codementorhannah

Mentorship codes
Go
1
star
41

lexicon

CSC 332 Assignment
JavaScript
1
star
42

gopages-sample

Sample blog using gopages and mgo
1
star
43

structures

CSC 341 assignment
Go
1
star
44

stack

Simple Stack
Go
1
star
45

goutils

A set of utilities for Go
Go
1
star