Running Gogs (Go Git Service) as a service on macOS X server

favicon.icoThis guides explains how you can run Gogs git server as a service on your Mac using launchd.

Command line tools

First you need to install Xcode from the Mac App Store. Once installed, you also need the Command Line Tools, you can initiate the download process by executing $ xcode-select --install in the Terminal.

The command line tools include Git.

MySQL or PostgreSQL server

You also need a database server for Gogs. You can install MySQL using MacPorts. A good tutorial can be found here.

You can alternatively run MariaDB instead of MySQL. My blogposts about MAMP (using MacPorts) includes a section about MariaDB.

Installing and configuring Gogs

I could try to write a good tutorial here, but the developers of Gogs already made a very nice tutorial about how you can install and configure Gogs. Read it here.

Launchd config

Create a launchd config file ~/Library/LaunchAgents/io.gogs.web.plist

<?xml version=”1.0" encoding=”UTF-8"?>
<!DOCTYPE plist PUBLIC “-//Apple Computer//DTD PLIST 1.0//EN” “http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version=”1.0">
    <dict>
        <key>Label</key>
        <string>io.gogs.web</string>
        <key>EnvironmentVariables</key>
        <dict>
            <key>HOME</key>
            <string>/Users/mathias</string>
        </dict>
        <key>ProgramArguments</key>
        <array>
            <string>sh</string>
            <string>-c</string>
            <string>cd /Users/mathias/Sites/gogs; ./gogs web</string>
        </array>
        <key>UserName</key>
        <string>mathias</string>
        <key>RunAtLoad</key>
        <true/>
        <key>KeepAlive</key>
        <true/>
        <key>WorkingDirectory</key>
        <string>/Users/mathias</string>
        <key>StandardOutPath</key>
        <string>/Users/mathias/Sites/gogs/launchd.log</string>
        <key>StandardErrorPath</key>
        <string>/Users/mathias/Sites/gogs/launchd_error.log</string>
    </dict>
</plist>

You can now start and stop Gogs using the following commands:

$ launchctl load ~/Library/LaunchAgents/io.gogs.web.plist
$ launchctl unload ~/Library/LaunchAgents/io.gogs.web.plist

HTTPS proxy

I then run Caddy in front of Gogs to use Caddy’s built-in automated HTTPS support. My git subdomain is then a reverse proxy to Gogs:

git.my-domain.com {
    proxy / http://localhost:3000
}

More info about running Caddy as a service can be found in this blogpost: Running Caddy as a service on macOS X server.