I have 10 bots working on my dev workflow right now. Here is what each one does.
I was mid-deploy when my terminal froze for three seconds. It continued on the wrong branch. A scheduled automation had spotted a new PR comment, run git checkout in my working directory, and switched my branch while the deploy was still in flight. Silently. I watched files land on the device from the wrong build and sat there for a moment not understanding what had just happened. That incident made me rebuild everything. While I was rebuilding, I wrote down every automation I run. This post is that document.
Short version: the fix was git worktree. Every automation now gets its own folder, so nothing can ever touch the directory I am typing in. The 10 workflows below all sit on that one idea.
How to read this post
- The bug and the fix. Why shared directories break, and the one git command that solves it.
- The shared pattern. All 10 workflows have the same 5-step skeleton.
- The 10 workflows. What each one does, when it fires, and what surprised me.
- Build it yourself. A working starter kit you can clone and run today.
One note before the code: the snippets in section 3 are simplified sketches. Names like send-notification and fetch-unprocessed-comments stand in for glue I am not going to make you read twice. They show the shape of each workflow, not something you can paste into a terminal. Section 4 has the real, runnable versions. If you just want working code, skip to the end.
Part 1: Why it broke, and the one git command that fixed it
Every automation I had built started the same way:
cd ~/projects/my-repo
git fetch origin
git checkout <some-branch>
# do the work
That cd was the problem. Every script operated in the same folder I was sitting in. Two firing at once meant a collision. One firing while I was deploying meant chaos.
I had already added a mutex so two automations could not run at the same time. That stopped them fighting each other. It never stopped one fighting me, because the lock only asked "is another automation running?" It never asked "is the human using this folder right now?" Detection cannot reliably answer that question. Separation makes it unnecessary.
The problem: one folder, multiple writers
All three bots converge on ~/projects/my-repo. When one runs git checkout while you are mid-deploy, the deploy picks up the wrong branch's files. No error, no warning.
The fix: give every bot its own folder
git worktree add creates a second folder attached to the same repository but checked out to its own branch:
git worktree add ~/projects/my-repo-automation feature/pr-comment-fix
No copying. No cloning. Both folders share one .git object store, so this is close to instant even on a large repo. The important property: checking out a branch in one worktree has no effect on any other. Each automation gets a disposable folder, does its work, and the folder is deleted. My directory is never involved.
Requires git 2.5 or newer, which in practice means any git you have.
Part 2: The pattern every workflow follows
All 10 use the same 5-step skeleton.
- Trigger - A schedule or a webhook fires
- Isolate - Create a fresh worktree
- Work - Do the actual task in there
- Verify - Run the build or tests
- Notify and stop - Tell me. Do not commit. Clean up.
In code:
TICKET_ID = " $1 "
BRANCH = " $2 "
WORKTREE = " $HOME /.automation/worktrees/ $TICKET_ID "
# 2. Isolate
git -C ~/projects/my-repo worktree add " $WORKTREE " " $BRANCH "
cd " $WORKTREE "
# 3 and 4: work and verify happen here
# 5. Notify, then stop
git worktree remove " $WORKTREE "
Step 5 is the one I never bend. Automation does the assembly. I keep the judgment.
Part 3: The 10 workflows
Reminder: these are sketches. Runnable code is in Part 4.
Comments
No comments yet. Start the discussion.