Gate
Hold an assigned job until a command you choose says go.
TUKANG_GATE='flock /locks/heavy sh -c "echo go; cat >/dev/null"'With that gate, a job starts only once it holds a lock on /locks/heavy, and keeps the lock until it ends. Anything else on the host that takes the same lock (a deploy, a backup) never runs at the same time as a job.
The contract
The gate runs once per job, when GitHub has assigned the job and the runner's hook has reported it.
- Tukang starts the command with
sh -cinside its own container. - When the job may start, the command prints a line
goon stdout, then keeps running. The job holds whatever the command holds, for as long as the command lives. - When the job ends, Tukang closes the command's stdin. It waits up to 10 s for the command to exit, then kills it and everything it started.
- A command that exits before it prints
gorefuses the job. The job fails, and its log shows the command's last stderr line. - A command that hasn't printed
goafterTUKANG_GATE_WAIT(50 min) is killed, and the job is refused.
The command gets two extra variables:
| Variable | Value |
|---|---|
TUKANG_GATE_LABEL | CI owner/name, handy as a lock holder's name |
TUKANG_REPO | owner/name |
While a job waits, the dashboard shows it as "Waiting for the gate", and the job's log says "Tukang: waiting for the gate to open…". If the gate command dies while the job runs, the job keeps running and the dashboard logs a warning.
Without a gate, a job starts as soon as it's assigned. To run only one job at a time, you don't need a gate: set TUKANG_MAX_JOBS=1.
Wait for free memory
#!/bin/sh
# gate-memory.sh: start a job only when 3 GB of memory is available.
while :; do
free_kb=$(awk '/MemAvailable/ {print $2}' /proc/meminfo)
[ "$free_kb" -ge 3145728 ] && break
sleep 5
done
echo go
cat >/dev/nullMount it into serve's container and set TUKANG_GATE=/gates/gate-memory.sh. /proc/meminfo inside a container shows the Docker host's (or Docker VM's) memory.
Refuse when the disk is full
#!/bin/sh
# gate-disk.sh: refuse a job when less than 5 GB is free.
free_gb=$(df -P -BG / | awk 'NR==2 {gsub("G", "", $4); print $4}')
if [ "$free_gb" -lt 5 ]; then
echo "only ${free_gb} GB free, need 5" >&2
exit 1
fi
echo go
cat >/dev/nullThe job fails with "tukang refused the job: only 3 GB free, need 5". To make jobs wait on GitHub instead of failing, use TUKANG_MIN_FREE_GB: Tukang then doesn't start a runner at all until there's room.
Hold a lock on another machine
When the lock lives outside Docker (on the Docker host itself, or another box), hold it over SSH. The image has an SSH client.
On the machine with the lock, add a key to ~/.ssh/authorized_keys that can only run the lock command:
command="flock /var/lock/heavy sh -c 'echo go; cat >/dev/null'",no-pty,no-port-forwarding,no-agent-forwarding,no-X11-forwarding ssh-ed25519 AAAA... tukang-gateGive serve's container the private half and a known_hosts entry, then:
TUKANG_GATE='ssh -i /run/secrets/gate_key -o UserKnownHostsFile=/run/secrets/gate_known_hosts lock@build-host'ssh passes stdin through, so when Tukang closes it, the remote cat sees the end of input, the remote shell exits and the lock is released. If serve's container dies, the SSH connection drops and the lock goes with it.
Next
- Configuration:
TUKANG_GATEandTUKANG_GATE_WAIT. - Troubleshooting: a job stuck on "Waiting for the gate".