DEV Community

loading...
Cover image for Stateful Action: keep the state between runs

Stateful Action: keep the state between runs

neuralhacker profile image Jorge Barata ・1 min read

This is my entry for the GitHub Actions Hackathon on DEV:

💾 Stateful Action persists the data into a branch after every run, letting the actions keep a state between runs.

I use this utility to periodically run reports, crawlers, and bots that live entirely as a GitHub repository. They keep the state of the progress in a branch. Some of them store the information in a SQLite database file. I also the action for publishing the results in the GitHub pages branch.

The action squashes the oldest commits, so the repository size doesn't grow wildly.

How does it work?

The action stores the information in a folder that is also a branch, using a git worktree.

It wraps the run like this:

  1. state branch is checked out as a worktree under a folder named .state (which must be ignored by .gitignore). Please check scripts/main.sh.
  2. The application writes its state into the .state folder.
  3. Changes in state branch are commited and pushed to origin. Please check scripts/post.sh:
    • Changes are commited and pushed.
    • Old commits on state branch are squashed and force-pushed.
    • .state worktree is removed.

For more details, please check the repository.

Discussion (0)

pic
Editor guide