Web Trigger is a Go service that listens for authenticated requests to trigger server commands on demand.
Copy the binary to a server folder like /usr/local/bin
.
Create a config file like the following and adapt it to suit your tasks, tokens and commands.
port: 5000
triggers:
- id: my-action-1
token: my-token-1
command: /home/brickpop/deploy-prod.sh --param-1
timeout: 20 # seconds
- id: my-action-2
token: my-token-2
command: /home/brickpop/deploy-dev.sh "CLI arguments go here"
# ...
tls: # optional
certificate: ./fullchain.pem
key: ./privkey.pem
Create the scripts for your triggers and make sure that they are executable.
Start the service:
$ webtrigger my-config.yaml
Following the example config from above:
Trigger a task by performing a POST
request to its path with the Authorization
header including the appropriate token.
$ curl -X POST -H "Authorization: Bearer my-token-1" http://localhost:5000/my-action-1
OK
$ curl -X POST -H "Authorization: Bearer my-token-2" http://localhost:5000/my-action-2
OK
$ curl -X POST -H "Authorization: Bearer bad-token" http://localhost:5000/my-action-2
Invalid token
$ curl -X POST -H "Authorization: Bearer my-token-2" http://localhost:5000/does-not-exist
Not found
Note: invoking a task that is already running will wait to start it again until the current execution has completed
A task can be in 4 different states:
$ curl -H "Authorization: Bearer my-token-1" http://localhost:5000/my-action-1
{"id":"my-action-1","status":"unstarted"}
$ curl -H "Authorization: Bearer my-token-1" http://localhost:5000/my-action-1
{"id":"my-action-1","status":"running"}
$ curl -H "Authorization: Bearer my-token-1" http://localhost:5000/my-action-1
{"id":"my-action-1","status":"done"}
$ curl -H "Authorization: Bearer my-token-1" http://localhost:5000/my-action-1
{"id":"my-action-1","status":"failed"}
To make the service a system-wide daemon, create /etc/systemd/system/webtrigger.service
[Unit]
Description=Web Trigger service to allow running scripts from CI/CD jobs
After=network.target
[Service]
ExecStart=/usr/local/bin/webtrigger /path/to/config.yaml
# Required on some systems
#WorkingDirectory=/usr/local/bin
Restart=always
# Restart service after 10 seconds if the service crashes
RestartSec=10
# Output to syslog
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=webtrigger
Type=simple
#User=<alternate user>
#Group=<alternate group>
Environment=
[Install]
WantedBy=multi-user.target
- Specify
User
andGroup
to droproot
privileges
Reload Systemd's config:
$ sudo systemctl daemon-reload
Enable the service:
$ sudo systemctl enable webtrigger.service
Start the service:
$ sudo systemctl start webtrigger.service
- Handle timeouts
- Allow TLS certificates
- Prevent blocking 3+ requests while still ongoing