Autodeploy listens for GitHub webhooks and deploys Docker Compose or systemd services running on the same host.
Since Autodeploy needs to run commands for other repositories on the host, it must run without Docker. This means you need to install Go and preferrebly run Autodeploy as a systemd service.
Below is a rough guide on how to set it up.
First, generate a secret:
$ openssl rand -hex 32 # use this for webhook_secret
Then, create a GitHub personal access token with the repo
scope. This will be github_token
.
This is where you define the repositories and their deployment commands. You need to create a config.yaml
(preferably at the repository root). Here's an example:
hostname: your-server
webhook_secret: your-webhook-secret
webhook_url_suffix: /postreceive
github_token: your-github-token
services:
service1:
repo: https://github.com/example/repo1
path: /path/to/service1
systemd_service: service1
healthcheck_url: http://localhost:8080/health
compose_service: false
build_command: make build
flow_timeout: 1m
service2:
repo: https://github.com/example/repo2
path: /path/to/service2
systemd_service: service2
healthcheck_url: http://localhost:9090/health
compose_service: false
build_command: go build ./...
flow_timeout: 2m
service3:
repo: https://github.com/example/repo3
path: /path/to/service3
healthcheck_url: http://localhost:3000/health
compose_service: true
flow_timeout: 20s
This is where you define the environment variables for Autodeploy. You need to create an autodeploy.env
file (preferably at the repository root). Here's an example:
AUTODEPLOY_CONFIG_PATH=/path/to/config.yaml
# optional
AUTODEPLOY_SLACK_CHANNEL=<optional>
AUTODEPLOY_SLACK_TOKEN=<optional>
$ pwd
/path/to/autodeploy
$ make autodeploy
Create /etc/systemd/system/autodeploy.service
and add the following:
[Unit]
Description=Autodeploy
After=network.target
[Service]
User=<your user>
WorkingDirectory=<your path>
ExecStart=<your path>/autodeploy --port 8090
EnvironmentFile=<your path>/autodeploy.env
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
Restart=on-failure
RestartSec=10
[Install]
WantedBy=multi-user.target
Now, start the service:
$ sudo systemctl daemon-reload
$ sudo systemctl enable autodeploy
$ sudo systemctl start autodeploy
$ sudo systemctl status autodeploy
Use Cloudflare tunnels or something like Caddy to serve Autodeploy behind a reverse proxy. This will be what the GitHub webhook calls.
Go to your repository settings, select Webhooks
-> Add webhook
. Set the fields as follows:
Save the webhook.
Now, you should be able to push to your repository and view the logs in the systemd service:
$ sudo journalctl -xeu autodeploy.service -f
Due to how much easier it is to work with GitHub API's over HTTPS rather than SSH, Autodeploy will add a new origin to your repository, called autodeploy
.
Autodeploy will verify that the existing HEAD of the repository that is getting deployed is equal to the BeforeSha
of the push event.
This could be changed in the future to allow for more flexibility.
The GitHub libraries in this project support nearly all events, but only push events are what I need.
Not only will sensitive tokens be in this file, but they also define arbitrary commands to be run on the host. Keep it secure!
This is a feature I would like to add in the future, but it is not a priority at the moment.