From the README at v0.7.8, linked from the repository
shep-deploy
A deploy dog for shep.
Watches a git branch, builds a release in an isolated directory, swaps to it, reloads the sheep, and rolls back on its own if the new release does not come up.
It is an external dog, the same shape as shep-log-rotate: an ordinary binary you adopt, talking to the daemon over the socket the CLI already uses.
Install
cargo install shep-deploy
shep adopt shep-deploy
Upgrading later wants --force:
cargo install shep-deploy --force
shep restart deploy
That flag is not politeness. This dog links shep-client, and its own version
does not change when that does, so after you upgrade shep the rebuild you need
is exactly the one cargo declines to do: it prints already installed, use --force to override, builds nothing, and exits 0. It looks like it worked.
shep adopt registers it with the shepherd, which supervises it from then on.
shep dogs lists what you have adopted.
Adopting runs the binary twice before anything is recorded. shep-deploy --version answers with the crate version and the shep protocol number this
build speaks, which is what shep checks against its own floor: a dog built
below it is refused there and then, rather than at its first handshake.
shep-deploy --schema answers with a JSON Schema for this dog’s settings: the
[deploy] section below, which sets a value for every sheep this dog serves,
and each sheep’s [app.dogs.deploy] table, which overrides it.
shep restart deploy asks the first question again, to see whether the
binary on disk has moved ahead of the process still running.
Telling it how to build
The build command is a setting of the sheep, in the [app.dogs.deploy] table
of its config, or in the dog’s [deploy] section for every sheep at once (see
Running as a dog). shep stores that table with the sheep
when you register it and hands it to this dog, which asks the shepherd for it
once per deploy:
[[app]]
name = "web"
script = "server.js"
[app.dogs.deploy]
command = "npm ci && npm run build"
artifacts = ["dist/bundle.js"]
commandruns throughsh -cinside the release. Nocommand, or no table at all, is no build.artifactsare paths, relative to the release, to copy back in after the build.- An unknown key is refused by name, with the sheep’s: a typo would otherwise be a build that never runs.
The release’s own Flockfile is not consulted for any of this. Whoever can land
a commit on the tracked branch chooses what is in it, so a [dog.deploy.build]
there is ignored, and so is [dog.deploy.reload], [dog.deploy.release] and
[dog.deploy.after]. The same Flockfile can be several sheep, each with its own
table.
Running a migration before the swap
A release hook is a command that runs once per deploy, after the build and its
artifacts and before current moves. It exists for a database migration, which
otherwise has to end the build command: its password then reaches every script
the build runs, and its output sits unlabelled in the build log.
[app.dogs.deploy.release]
command = "supabase db push"
env = { SUPABASE_DB_PASSWORD = "..." }
- It is a key of the sheep’s
[app.dogs.deploy]table, or of the dog’s[deploy]section, both of which are yours: the hook’senvholds your secrets, and a commit must not choose the command that receives them. A release’s own Flockfile cannot set it.[app.dogs.deploy.after]is read the same way. - It runs in the release, as the app’s
user, throughsh -c, with the build’s cleared environment: the base set, the dog’spassthrough, then the hook’s ownenv. A{{secret:KEY}}in thatenvis replaced with the secret before the hook runs. The dog setsSHEP_DEPLOY_SHEEP,SHEP_DEPLOY_SHAandSHEP_DEPLOY_RELEASE(an absolute path) last, so the hook’senvcannot override them. - It is bounded by
build_timeoutand killed as a process group, like the build. The log gets one line,shep-deploy: <sheep>: running the release hook, before it starts. - A non-zero exit, a signal or a timeout stops the deploy.
currentdoes not move, the old release keeps serving, and the sha is held as for a failed build. initruns no hook. A cutover does not touch the app’s database.
A rollback does not undo a migration. If the new release fails verification
after the hook ran, the old release comes back against the migrated database.
A held sha that you retry with shep deploy <sheep> runs the hook again, so
write the command to be idempotent.
Deploying only what a push touches
Several services can share one repository, each its own target. Every push to the branch then reloads all of them, so a change to one restarts the rest. A target that lists the paths it runs from is left alone by a push that changes none of them:
[app.dogs.deploy]
reload = ["cmd/voting", "internal/voting", "pkg", "go.mod"]
- Entries are git pathspecs. A directory name matches everything under it, and
a glob such as
assets/*.pngworks too. - Set per sheep, each has its own list, so two sheep built from one repository
are judged separately by the same push. Set in
[deploy], every sheep this dog serves shares one list. Flockfile.tomlis not a path unless you list it.- No
reload, orreload = [], deploys every new sha, as before. - A skipped push costs a fetch and a diff: no checkout, build or reload, and
deploy.tomlis not written. The log says<sheep> skipped <sha>, nothing under its reload paths changed. It is said once, then again about once an hour while the same push stays skipped, and at once when the branch moves. - The next push is compared with the last sha that was deployed, not with the one skipped, so a skipped push’s changes still count toward it.
- If the dog cannot tell (the deployed sha is gone from its clone, a pathspec git refuses, git fails) it deploys and says why.
shep-deploy deploy <sheep>by hand never skips. It is you asking for a deploy now.
Skipping a reload when the build produced the same binary
A commit that changes a comment still recompiles a Go app to the same machine
code, and the sheep gets restarted for nothing. reload_script lets the poll
loop leave that commit alone: the build runs, and the file its script names is
compared with the digest deploy.toml recorded when this sheep last landed a
release. Equal bytes mean no swap, no reload and no record.
# the sheep's own config
[[app]]
name = "api"
script = "./app"
[app.dogs.deploy]
command = "go build -buildvcs=false -o app ./cmd/app"
reload_script = true
- Both sides of the compare are digests, each taken inside the repository’s
build turn, and neither is the live release. A release’s
targetis a symlink at that cache, so ascriptunder it is one file every release of the remote resolves to, and whichever sheep built last owns its bytes. The second sheep of a repository would read the first one’s binary through its own live release and skip the reload it needs, and the first would do the same a moment after it let go of the build turn, to a hand deploy of the other.deploy.tomlcarriesserved_script, the digest of the script this sheep’s own build produced the last time it landed a release. Every landing writes one, whether or not the key is set, and an ending that landed nothing drops it. - A sheep with nothing recorded yet deploys. A record written before the key, or one the cutover wrote and no deploy has landed since, has no digest to match.
deploy <sheep> --promoterecords none. It builds nothing and holds no build turn, so it has no bytes it can call its own, and the next deploy records one.- It fits a compiled app, and it is exact for one: the file the sheep is
launched from is the file the build produced. An interpreted app’s
scriptis one file and the ones it imports are not compared, so a change to any of them reads as unchanged. - Go stamps the commit and the build time into every binary it builds in a git
checkout, so the bytes differ on every commit unless the build passes
-buildvcs=false. The dog never touches your build flags. - The build still runs. This skips the reload, not the build, because nothing
but running it says what it produces. A release built and skipped is reclaimed
by
retentionlike any other. - A skip runs no
[release]hook, swaps nothing and leavesdeploy.tomlas it was, so the next comparison is still against the sha actually serving. - Off by default, and per sheep. In
[deploy], every sheep this dog serves takes it. shep-deploy deploy <sheep>by hand never skips on the script.- The log says
<sheep> skipped <sha>, its built script is unchanged, once, then again about once an hour while it stays skipped. The exit status is 0. - If the dog cannot compare, because
scriptis an absolute path or starts with~, or either file cannot be read, it deploys and says why.
Purging a cache after the swap
An after hook is a command that runs once per deploy, after the new release is swapped in, verified and recorded. A build cannot do this work: it runs before the swap, so a purge from the build refills from the old release.
[app.dogs.deploy.after]
command = "nginx -s reload && ./purge-cloudflare.sh"
env = { CLOUDFLARE_API_TOKEN = "...", CLOUDFLARE_ZONE_ID = "..." }
- It is set in the sheep’s
[app.dogs.deploy]table or the dog’s[deploy]section, and read from nowhere else, as the release hook is. - It runs in the new release, as the app’s
user, throughsh -c, with the release hook’s environment.SHEP_DEPLOY_SHEEP,SHEP_DEPLOY_SHAandSHEP_DEPLOY_RELEASEname the sheep, the new sha and the release’s absolute path.currentalready points at that release anddeploy.tomlalready names the sha. - It is bounded by
build_timeoutand killed as a process group. The log gets one line,shep-deploy: <sheep>: running the after hook, before it starts. - It runs inside the deploy, so no other deploy of that target starts until it ends.
- It runs only if the record write succeeded. If
deploy.tomlcannot be written, the log says the hook was skipped. - It never runs for a deploy that is rolled back, that fails before the swap,
or that finds nothing new, and
initruns none. - A failure changes nothing. A non-zero exit, a signal or a timeout is logged
as
shep-deploy: <sheep>: deployed <sha>, but the after hook failed: .... The release stays live, the outcome is still deployed and the sha is not held. The hook is not retried; run the purge by hand if it matters.
What one deploy does
- Fetch into a bare clone, and compare the branch head to the last deployed sha.
- Take the build turn for the repository, then
git worktree addthe new sha into its shared release directory, sharing the object store. A sha and build key that already have a completion marker are left as they are: nothing is checked out and nothing is built. - Symlink the shared files in: whatever git ignores and
.shepignoredoes not. A.shepignoreline is a bare name, matched at any depth, or a path with a/in it, anchored to the checkout. A leading/anchors too, a leading./and a trailing/are dropped, and\!namenames a file that begins with!, as in.gitignore. A glob, a bare!negation, any other backslash escape or a..is refused by name rather than matched against nothing. A linked path that has anode_modules,.output,distor.nextcomponent, or that overlaps anartifactsentry, is named in a warning on stderr each deploy; add it to.shepignoreto stop linking it. A linked file whose length or modified time, or a linked directory whose own modified time, changed during the build is named too, because the build may have written through the link into your checkout. A file the running app writes, a database or a log, is named as well; leave those linked. - Run the build, as the app’s
userif it sets one. - Run the release hook, if the sheep’s table sets one.
rename(2)currentonto the new release.Reloadthe sheep.- Verify. On failure, put
currentback, reload again, and verify that the old release came up. A rollback that does not come up is an error, not a rollback. - Record the sha in
deploy.toml, then run the after hook, if the sheep’s table sets one. Its failure is logged and does not undo the deploy.
Steps 1 to 5 never touch the running app. A build that fails costs a directory, not an outage. A hook acts on whatever it was written to act on, which is why a rollback cannot undo it.
Verification waits for a new process to reach Online, not for any process
to be Online. shep answers a reload before it has finished one, and keeps
the old instance when the replacement never becomes ready, so “something is
online” is true throughout a deploy that failed.
Layout
Each sheep has a directory of its own under $SHEP_HOME/deploy/<sheep>/:
git the bare clone, shared by every release: a link to the
shared clone below, or a directory of its own on a tree
set up before clones were shared
current -> a symlink into the shared releases below
deploy.toml remote, deployed sha, held sha, staged sha, and the app as it was
before adoption (owner-only: it holds env)
What it serves is not inside it. Releases are the repository’s, at
deploy/repos/<key>/releases/<sha>-<build key>/, one worktree per sha per
build, with their completion markers beside them at
repos/<key>/complete/<sha>-<build key>. The build key is a hash of the
command, the build user, the operator checkout the record names and the
resolved build_env, so three sheep of one repository that build one sha the
same way land the same built release: the first one checks it out and builds it,
and the other two swap to it. Change one of the four and you get a directory of
your own, because a release built with somebody else’s environment is not yours.
The checkout is one of them because a release links the ignored files of the
checkout it was built from: two checkouts of one remote are two builds, or the
second sheep’s local.json would find its name in the release taken by a link
aimed at the first. A marker means checked out AND built, and a deploy that
finds one for its sha and its key skips the build.
Sheep deployed from the same remote share one clone, at
$SHEP_HOME/deploy/repos/<key>/git. init makes it when no sheep with that
remote has, and links the tree’s git to it when one has. The key is the
remote with surrounding whitespace, trailing slashes, a trailing .git and
the case of its scheme and host folded away, so https://host/x and
https://host/x.git are one clone; the path’s case is kept, and
git@host:x against https://host/x stays two. A sheep cannot be called
repos. Fetches into one clone take turns on git.lock beside it.
The same key holds the build cache: deploy/repos/<key>/cache/target,
symlinked into every release of every sheep that deploys that remote as
target. Sheep off one repository compile it once between them instead of
once each, and a build that hardcodes ./target/release/koji still resolves.
Because it is one directory, builds of one repository take turns too, on
git.build.lock beside that clone: two sheep of one remote build one after the
other, a --stage build or a first init build queues behind them the same
way, and a reload never queues at all. A tree that had its own cache/ keeps
the directory; nothing removes it, and the next build warms the one under
repos/ from scratch. A release checked out before the move has its target
relinked at the new cache when it is built again, so a retry of it still builds.
Removing the dog lets go of the link of each sheep it puts back, and of each
whose cutover never landed; the clone goes with the last link, and the build
cache goes with the clone. One exception, and it is a question about the
repository rather than about the sheep being removed: a tree that owns a clone
of its own keeps that build while any sheep of the repository names it, either
from its record - the remote the cache is keyed by - or from its current,
which resolves through a release linked at it with target. Either one means a
live release would be left reading and building through a dangling link. A sheep
that owns no clone and links to none has nothing to build with, so it keeps
nothing. A sheep left running from its tree keeps its link, and so the clone. A
deploy whose recorded remote is not the one its tree’s link was made for is
refused, so a hand edit
cannot fetch one repository into a clone other sheep deploy from; run init
again to relink. A tree set up before this keeps the clone it has until you
run init again: that links it to the shared one and removes the clone it
owned, once no other tree links to it. init is refused on a target already
cut over, so what it moves is a tree whose first setup never finished. A
deploy never changes which clone a tree uses.
branch, watch and verify are not in deploy.toml. They are keys of the
sheep’s [app.dogs.deploy] table, or of the dog’s [deploy] section for every
sheep at once, which init, --watch and --branch write to the sheep’s own
table and shep lookout edits at either level; the dog reads them again every
tick. A deploy.toml
that still carries one of them is refused, naming the key.
The sheep’s cwd is current, permanently. Set it explicitly when you
register the app: a Flockfile cwd left to default is resolved at
registration, which pins the sheep to one release and makes every later swap
invisible to it.
Commands
shep-deploy deploy <sheep>
shep-deploy deploy <sheep> --watch auto|manual
shep-deploy deploy <sheep> --branch <name>
shep-deploy deploy <sheep> --branch refs/pull/1/head
shep-deploy deploy <sheep> --stage
shep-deploy deploy <sheep> --promote
shep-deploy init <sheep>
shep-deploy init <sheep> --verify probed|alive
shep-deploy survey
shep-deploy on-remove
shep-deploy help
--watch changes the setting and returns without deploying. --branch does
the same for the branch a target tracks, which init read from the checkout
once; it clears a held sha and refuses a target init never finished. A name
the remote lacks is reported by the next tick. --branch also takes a full ref
beginning refs/, such as refs/pull/1/head, which the target then follows on
every push like a branch. Only that ref is fetched, not every pull ref the
remote has. A closed pull request’s ref stays on GitHub, so the target keeps
serving its last head. --stage fetches, builds the branch head and records it
in deploy.toml as staged, then returns: it never swaps current, reloads
the sheep, or changes deployed or failed. It is refused on a watch = auto
target, because the next tick would deploy that head anyway; set --watch manual first. Staging the head that is already deployed, or already staged with
its release still built, does nothing; a staged release that is gone is built
again. Retention spares the staged release, and a deploy that lands clears
staged. While a sha is staged, a shep-deploy from before the field refuses to
read the record. --promote lands what was
staged: it runs the release hook, swaps current, reloads the sheep, verifies
it and runs the after hook, with the rollback a deploy has, and builds and
fetches nothing. It lands the staged sha even if the branch has moved since,
and it is refused when nothing is staged or the staged release is gone, with
--stage as the way out. A promote that does not come up is rolled back and
leaves staged set, so it can be run again. It ignores a held sha, as a
deploy asked for by name does. --verify on
init sets how the sheep’s deploys are verified in its [app.dogs.deploy],
probed when it is left out. survey reports
where every registered sheep stands and starts, registers and writes nothing;
a manual target with a staged sha reads main@a1b2c3, deploys only when asked, staged d4e5f6.
help, -h and --help print usage on stdout. A sheep named help is reached
with shep deploy deploy help.
Running as a dog
Adopted as a dog, shep-deploy takes no arguments and polls instead, but only
when shep starts it. Every 30 seconds by default, it deploys any
watch = "auto" target whose branch has moved. Configure it in
$SHEP_HOME/dogs.toml:
[deploy]
interval = "30s"
retention = 5
git_timeout = "5m"
build_timeout = "1h"
passthrough = ["CARGO_HOME"]
All five are read again at the top of every tick, so changing any takes
effect on the next tick, with no restart. Not within one interval: a tick
that is running a deploy finishes it first, and only then sleeps the
interval it read before the edit. A section that stops parsing
while the dog runs leaves it polling on the last one that did, and it says
so; the same mistake at startup stops the dog instead, because there is
nothing else for it to run on then. A shep older than 0.1.32 read the same
keys from [dog.deploy] in shep.toml; a newer one moves that section into
dogs.toml the next time the shepherd boots.
shep lookout renders a form over this section: press s, then e on the
dog’s row. It has one because of the --schema answer above. None of the five
keys is a credential, so the pane redacts nothing. passthrough names
variables and never carries their values.
Setting a key once, for every sheep
[deploy] holds more than those five keys. branch, watch, verify,
command, artifacts, reload, reload_script, release and after can be
set there too, and every sheep this dog serves takes the value:
# $SHEP_HOME/dogs.toml
[deploy]
interval = "30s"
branch = "main"
command = "npm ci && npm run build"
A sheep’s own [app.dogs.deploy] overrides any key that is not dog-wide, and
only the keys it names:
# the sheep's own config
[[app]]
name = "api"
script = "server.js"
[app.dogs.deploy]
branch = "release"
api follows release and builds with the shared command. A sheep with no
table of its own follows main. A nested table layers key by key, so a sheep
can add one entry to the env of a [deploy] hook every sheep shares.
The five dog-wide keys are the exception, and a sheep that sets one is refused, by name:
web: `[app.dogs.deploy]` sets interval, which is dog-wide. Set it in `[deploy]`
in dogs.toml, where every sheep this dog serves shares it
The pane draws both levels, and marks the five as dog-wide where it offers
them. A hook’s env is marked secret, so the pane shows <set> there and
never the value it holds.
A build’s environment is the sheep’s build_env, a map of name to value, and a
value of {{secret:KEY}} reaches the build as the secret’s value:
[app.dogs.deploy]
command = "bun install && bun run build"
build_env = { VITE_SUPABASE_ANON_KEY = "{{secret:SUPABASE_ANON}}" }
build_env is marked secret, so the pane shows <set> there and Debug prints
a count. A key the sheep’s environment has no secret for refuses the deploy,
naming the key.
A {{secret:KEY}} in a sheep’s table is resolved by the shepherd before the
dog reads the table, and that read is one the shepherd gates: secrets.read,
scoped to the environment that sheep resolves in. shep adopt writes the
secrets.read:production this dog’s manifest asks for into
[daemon.dog_grants.deploy] in shep.toml. production is the shepherd’s
own [daemon] environment, so a flock that changed it, or a sheep with an
environment of its own, needs that scope named in the same list:
# $SHEP_HOME/shep.toml
[daemon.dog_grants.deploy]
granted = ["flock.read", "flock.control", "flock.edit", "secrets.read:staging"]
Without the scope the dog is refused its read of that sheep’s table, names the scope it needs in its own log, and deploys every other sheep as before.
retention is how many of the newest releases a deploy leaves in the repository
it builds into. Two more are spared whatever their age: the one current points
at, and the one deploy.toml names. They are the same release except after a
deploy died between its swap and its record write, so a target holds retention
directories in the ordinary case and up to two more after that. It cannot be
below 2: the release a failed deploy rolls back to is the second newest, so
anything lower would silently disable rollback, and it is refused rather than
clamped.
The count is asked of the repository, because its releases are shared: every
sheep that deploys one remote builds into one releases/ directory, so one
sheep’s prune is a sweep over directories another sheep may be serving from. It
takes the repository’s build turn for the whole sweep, and reads every tree under
deploy/ before it removes anything, whether or not that tree links this clone.
A release any of them names is not a candidate - from its current, from its
record’s deployed or staged, or from the current.claim file a deploy writes
under that turn for the window between letting go of it and swapping. A record
names a sha and no build key, so it spares every build variant of that sha: a
stale variant costs disk, and a live one removed costs a running service its
working directory. A rollback reads a recorded sha the same way, and takes the
newest directory of it under releases/ rather than only the one the current
settings name. The number itself is
whichever deploying sheep’s settings say, so two sheep of one repository with
different retention prune the same directory to different depths. The clone
and the build cache beside it are reclaimed the same way, and never while any
sheep of that repository names it from its current or its record. See
Layout.
interval, git_timeout and build_timeout are refused under a second. A
bare number is milliseconds, so interval = "30" is thirty milliseconds and a
dog fetching thirty times a second; write "30s".
git_timeout bounds the fetch, which is the only git call that talks to a network. The rest operate on local directories and cannot hang on a remote. Targets are deployed one at a
time, so without it a remote that drops packets rather than refusing them stops
every target and every smit refresh, with no error and no log line. Five
minutes by default, which is generous enough for a cold clone of a large
repository.
build_timeout bounds the build command and both hooks, against the same failure and for the
same reason. A build that never finishes holds that same one-at-a-time loop, so
no other target deploys and nothing is logged, because from the loop’s point of
view nothing has gone wrong. An hour by default, which no honest build should
reach: it exists to turn a build that will never finish into an ordinary
per-target failure, not to put a schedule on slow work. A build past it is
killed as a process group, so whatever the build command started goes with it.
passthrough names environment variables a build may keep from the dog’s own
environment. See Security below for why that list starts empty. An entry that
is not a variable name, or is listed twice, is refused; one that names a
variable the dog was not started with is said at build time, once per build.
One target’s failure never stops the others, and never stops the dog. Each
target’s outcome is reported on its own and the loop carries on. up to date
prints nothing at all, which is the answer to almost every tick of almost
every target, and a line that repeats is said once rather than every interval.
A tick never begins while the previous one is still running, so a push landing during a build is deployed on the next tick rather than aborting the build in flight.
Nor does a second process. Each deploy takes an exclusive flock on its own
tree for as long as it runs, so shep-deploy deploy web typed while the dog is
mid-tick on web is refused in one sentence rather than colliding somewhere
inside git. The lock is per sheep, so other targets are unaffected, and the
kernel releases it if the holder dies, so a killed dog leaves nothing behind
holding a sheep hostage.
Every tick also paints each target’s smit, so shep flock shows which branch
and sha it is on without a second command:
▲ main@a1b2c3 watched
⏸ main@f6e5d4 manual
⏸ main@f6e5d4 +a1b2c3 manual, with a1b2c3 staged
Republished every tick rather than on change: shep holds a smit in memory only for as long as the connection that painted it stays open, so a dog that only published on change would show nothing at all after a daemon restart until its next deploy. A refused smit is logged and otherwise ignored, since it is cosmetic and never worth failing a deploy over.
A deploy in the dog’s own tick repaints the smit as soon as it lands. A smit
belongs to the connection that painted it, so a deploy, init or --watch
run by hand cannot leave one: the dog paints the new state on its next tick,
which starts interval after the current one finishes.
How it holds its connection
Adopted, the dog announces itself by the name shep gave it, taken from
SHEP_DOG_NAME. That is what shep records as a handshake. A dog that does not
send one connects, serves every request correctly, and is still shown as
silent, restarted once, and then written off as stale.
The connection survives a handover as well, so shep upgrade does not leave
the dog running against a socket nobody is listening on.
A refusal it does not survive. When a newer shepherd rejects the handshake on
protocol skew, no amount of reconnecting fixes it, so the dog reports the
shepherd’s version and its refusal message, then exits 5 rather than ticking
on against a connection that will never answer. It cannot name its own protocol
number, because LinkState::Refused does not carry one. Upgrade whichever side
is behind and shep starts it again.
Run by hand, shep-deploy deploy web announces no name at all. It is not a
dog, and a command claiming to be one would have shep restart the real dog when
the command’s own handshake was refused.
A bare shep-deploy run by hand prints usage and exits 2 rather than
polling, because a second loop beside the adopted dog would deploy the same
targets twice. shep deploy typed with nothing after it prints the same usage
and exits 2: shep sets SHEP_DOG_NAME for that command too, and the dog is
told apart from it by SHEP_NAME, which only the daemon puts into a process it
spawns. Only the shepherd runs the loop.
A commit that fails is held
A commit that does not land is left alone until the branch moves. It is
written into deploy.toml as failed, because otherwise the branch head and
the deployed sha stay different and every tick runs the whole sequence again:
fetch, full rebuild, swap, reload, wait out the verification budget, roll back,
reload again. Two reloads of a live app and a build every thirty seconds, from
one bad commit, until somebody notices.
Pushing a fix clears it, which is what CI does with a red commit; so does
shep deploy <sheep>, which retries the same commit deliberately. The tradeoff
is deliberate too: a deploy that failed on a network blip rather than on the
commit waits for one of those two, rather than being retried on the next tick.
shep-deploy survey shows such a target as held, naming the commit it is
holding, so a target stuck since yesterday does not read like one with nothing
to do. Survey reads the record, never the remote, so a hold that a push already
cleared still shows until the next tick.
Taking a sheep over
init takes a sheep over: it builds the tree, fetches the repository, links
the shared files in, builds the first release, and re-registers the sheep with
its cwd set to current. setup is its old name; it still works for now and
prints a line on stderr saying so. A Flockfile that names no interpreter keeps the
one the sheep already ran under: shep start fills that in from shep.toml’s
[interpreters], and this dog cannot read that map.
An app with neither a readiness_probe nor wait_ready, a bot with no port
say, is refused before anything is built, because its first deploy would be
refused for the same reason. Take it over with shep-deploy init <sheep> --verify alive, which sets verify = "alive" in its [app.dogs.deploy]. A
refused init leaves a directory with the fetched repository and the staged
release but no deploy.toml and no current, which is not a deploy target, so
running it again is a retry.
The first cutover is the one deploy that may have downtime. It runs two
instances at once, so an app that does not bind with SO_REUSEPORT cannot take
its own port while the original still holds it, and the new instance is then
removed and the original left serving. Every deploy after the first replaces
the instance rather than joining it, and does not meet this.
Most apps cannot set SO_REUSEPORT, and some cannot even be made to: Node
refuses reusePort on macOS outright. For those, stop the sheep before the
cutover and start it afterwards:
shep stop web
shep-deploy init web
With the port free the newcomer binds and the cutover lands. It costs the downtime this section already warns about, and it is the difference between a setup that works and one that cannot. Measured 2026-08-28: the same app that failed the cutover while running completed it once stopped.
A cutover that was abandoned leaves the tree behind, and a sheep is not a
deploy target until a cutover lands. shep-deploy deploy refuses such a target
before anything at all happens, because its record names no deployed release.
That refusal is load-bearing rather than tidy. Without it the deploy would not stop: it would build, swap, reload the sheep at its own checkout, see a real turnover, and report success for a release nothing served.
Remove its tree and run init again once the cause is fixed. Both init and
the failure message say this, and both print the resolved path to remove rather
than a $SHEP_HOME you would have to expand yourself. init leaves it
watch = manual until the cutover lands, and --watch auto on one is refused
for the same reason: it would ask for that deploy once every interval,
unattended.
Verification
verify = "probed" (the default) needs the app to have a readiness_probe or
wait_ready; without one, shep reports a process Online for not having died
yet, so there is nothing to verify against and the deploy is refused.
verify = "alive" is the deliberate downgrade: a new process, still running
ten seconds later. An app that cannot have a gate, one with no port, is taken
over with shep-deploy init <sheep> --verify alive; init refuses it under
probed before it builds anything.
Both modes watch the flock after the turnover. probed requires the same
processes, all Online, at every look for ten seconds; alive sleeps ten
seconds and then polls for Online until the reload budget runs out, or one
more ten seconds, whichever is longer. Online is a verdict shep gives once:
a replacement that passes its probe and dies moments later is respawned under
a new pid with the reload already committed, and a replacement whose second
probe fails is put back to starting and left there. The pid and the status
are the evidence of either, and only inside the dwell. An app that sets
reuse_port with a probe gets a longer one, its own listen_timeout, because
shep re-runs its probe after the old instance drains and can demote it any
time up to that.
The first cutover is also the one deploy that is not verified against the
readiness probe. shep reports a freshly started process Online once its
listen_timeout elapses, whatever the probe said, and only aborts a reload
whose replacement was not ready. So init checks what it can: a new process
started and was still the same process, not errored and not restarted, ten
seconds later. A release that starts, stays up and serves nothing passes that.
Every deploy after the first is verified against a new process reaching
Online.
That used to be weaker than it sounds, and the gap was shep’s rather than
this crate’s. Measured 2026-08-28 against a real shepherd: a sheep with an
HTTP readiness_probe that never passes was marked Online about a second
into a reload, well inside its listen_timeout, and stayed there. The cause
was the reload’s overlap. Both instances were up when the replacement’s first
probe landed, the outgoing one answered it, and shep took that as the incoming
one proving itself. A release that started and never became ready was verified,
recorded as deployed, and not rolled back, with the app down while the record
said otherwise.
shep fixed it the same day. A probed app is now reloaded serially: the old
instance drains first, so the only process that can answer the replacement’s
probe is the replacement. An app that sets reuse_port keeps the overlap it
asks for and gets a second probe once the drained instance is gone. Either way
a replacement that never answers is left starting rather than online, which
is what this crate reads, so verify = "probed" now means what it says.
One thing to size correctly. A reuse_port app’s reload costs one more
listen_timeout than it used to, for that second probe, and the budget in
window.rs is listen_timeout + graceful_timeout + slack per instance. For
that one combination the budget can expire mid-check and roll back a release
that was fine. A false rollback rather than a false success, which is the right
direction to fail, but it wants a wider budget.
Rollback works for a release that crashes and for one that starts without serving. Both are caught.
The rollback is verified the same way. The shepherd accepting its reload is not
enough: a previous release whose binary is gone is dropped from the flock after
the reload is accepted. A rollback is reported as one only once the old release
is seen to come up, within the restored release’s own listen_timeout and
graceful_timeout rather than the rejected one’s; otherwise nothing is known
to be serving, and the error says so and points at shep flock and shep describe <sheep>.
Removing it
shep-deploy on-remove is the lifecycle hook: shep runs it before forgetting
the dog, and it puts every sheep back as it ran before the dog took over: the
whole definition init recorded, env, instances and probes included. A
record written before 2026-09-04 carries only the old cwd and script, and
those go back over the shepherd’s current definition. A
sheep the dog bootstrapped has nowhere to go back to, so it is left running
from current and the report says exactly that, with the path.
The deploy tree is never deleted. It is not the dog’s to delete, and in the bootstrap case a running app is still pointing into it.
Exit codes
Follows shep’s own
taxonomy:
0 deployed or already up to date, 2 bad arguments, 4 bad configuration,
5 no daemon answered or one refused this dog’s handshake, 1 anything else.
1 includes a rollback that did not come up: the previous release was put back
on disk, and nothing is known to be serving.
Two are this dog’s own:
| code | means | the flock is |
|---|---|---|
12 |
the deploy was rejected, the previous release was put back and came up | healthy, on the old release |
13 |
a first cutover landed and then could not tidy up | healthy, on the new release |
A script that treats any nonzero code as “the deploy broke” will be wrong about both, because in each case the flock is serving.
Security
A deploy runs the build command from the repository being deployed. That is
the point of it, and it is also the whole of the risk: bun install’s
postinstall scripts and make build are arbitrary code, chosen by whoever can
land a commit on the branch you track.
That build runs as this process’s uid unless the app sets user. Supervised as
a dog, this process is the shepherd’s child and shares its uid, and shep’s own
docs recommend running the shepherd as root so it can drop privileges per app.
So the default arrangement is a repository’s build script running as root, once
per deploy.
The build command, the hooks and the reload paths are not in the release at
all: they come from the sheep’s [app.dogs.deploy] table or the dog’s
[deploy] section, both of which are yours rather than the repository’s, so a
commit cannot choose them. Nothing is read from a release’s Flockfile.toml
either: the app config is the one the shepherd holds for the sheep.
Set user on the app and the build drops to that user’s uid and primary group,
with the shepherd’s supplementary groups cleared, before it runs anything. A
compromised build then gets that app’s privileges and nothing more.
Under a root shepherd the build user owns its release and the build cache
directory of the repository it deploys (target, the directory only, not what
is in it) and nothing else. That cache is shared by every sheep deploying that
remote, so what is inside it belongs to whichever build wrote it last.
The dog hands those over just before the build, or before the release hook (or the
after hook) when the app has no build command, through open directory handles
and never through paths, and skips every symlink, every file with a second
hard link, and the .git file at the release root. The build can still replace
that file, since it owns the directory, so the dog writes it back after the
build. git, the shared clones under repos/, deploy.toml, the lock files,
current and repos/<key>/releases/ stay root’s. Two sheep never share a
release across build users, because user is one of the four inputs of the
build key, so a release is handed over at most once, to the one user its build
ran as. The user also needs search permission on every directory down to
the release: a $SHEP_HOME of mode 700 refuses the build at its first step.
Put $SHEP_HOME where that user can search, or run the shepherd as that user.
shep-deploy warns when it is about to run a build as root with no user set.
It does not refuse. Whether an app runs without a user is shep’s call and the
operator’s, not a deploy dog’s.
A build starts from a cleared environment, not this process’s. It gets PATH,
HOME, LANG, LC_ALL and TZ, then whatever passthrough names in
[deploy] in dogs.toml, then the sheep’s build_env, which wins a name an
earlier source also set. Nothing else. Dropping uid and gid bounds what a build can touch; it does
nothing about what it can read out of its own environment, because those values
are copied in before the drop happens. A dog started with a registry token in
its environment would otherwise hand it to every build it runs.
SSH_AUTH_SOCK is not in that base set, on purpose. A forwarded agent reaching
a build lets the build authenticate as you anywhere that agent is trusted.
Fetching happens in the dog’s own process and keeps its own environment, so a
private repository still clones; only the build loses the socket. Name it in
passthrough if a build genuinely needs it.
artifacts may only name paths that really land inside the release or
the build cache of the repository being deployed, resolved rather than
spelled. A .., an absolute
path, a committed symlink, or a CARGO_TARGET_DIR pointing elsewhere are all
refused. That is narrower than it was: a build whose output genuinely lands
outside both is no longer copyable, because the copy runs in the dog’s own
process at its own uid.
What the allowlist does not cover, and cannot. Your project’s own secrets
are not in the dog’s environment, they are in your repository’s working tree. A
.env file is gitignored, which is exactly the rule that makes shep-deploy
symlink it into every release, so the build reads it and so does the app at
runtime. That is the intended behaviour and the reason shared files exist. It
does mean a build command can read every secret the app itself can read, no
matter what this allowlist says. The bound worth having there is user, so
that a build and the app it builds are confined to the same one account.
Nothing else here handles credentials. Git auth is inherited from the user the dog runs as, so a private repository works as it does in that user’s own shell, and no token passes through any URL or argument this crate builds.
Platform
Unix only. This is deliberate, not a gap waiting to be filled by accident: the
deploy model is rename(2) over a symlink, the build’s privilege drop is a uid
and a gid, and both are Unix concepts the code uses directly rather than
through a portability layer. Building on Windows fails with one sentence saying
so. Windows support is planned and will be scoped on its own. Releases ship
prebuilt binaries for Linux and macOS only.
Status
Working: the deploy sequence, the operator commands, opt-in, the poll loop, retention, and restore on removal. Tested against a real shepherd.
Not built: Windows.
See docs/writing-plans/plans/2026-08-26-deploy-engine.md for what was built, and the design spec for what it is for.
License
MIT OR Apache-2.0, at your option.