Running cog automatically against GitHub pull requests#
I really like Cog (previously) as a tool for automating aspects of my Python project documentation - things like the SQL schemas shown on the LLM logging page.
When using cog
in this way it’s important to remember to run cog -r
to update those generated files before pushing a commit.
I’ve previously been enforcing this using GitHub Actions - as part of my tests I run cog --check *.md
so that my test suite fails if the generated files are out of date.
This morning I switched to a friendlier version. I now run a GitHub Actions workflow against any created or modified pull requests and, if cog
needs to be run, the workflow runs it and then helpfully commits the files back to that repo.
I got Claude to write the initial workflow, and then iterated on it to get it working.
This goes in .github/workflows/cog.yml
:
1name: Run Cog2
3on:4 pull_request:5 types: [opened, synchronize]6
7permissions:8 contents: write9 pull-requests: write10
11jobs:12 run-cog:13 runs-on: ubuntu-latest14 steps:15 - uses: actions/checkout@v416 with:17 ref: ${{ github.head_ref }}18 - name: Set up Python 3.1119 uses: actions/setup-python@v520 with:21 python-version: '3.11'22 - name: Install dependencies23 run: pip install -e '.[test]'24 - name: Run cog25 run: |26 cog -r docs/**/*.md docs/*.md27 - name: Check for changes28 id: check-changes29 run: |30 if [ -n "$(git diff)" ]; then31 echo "changes=true" >> $GITHUB_OUTPUT32 else33 echo "changes=false" >> $GITHUB_OUTPUT34 fi35 - name: Commit and push if changed36 if: steps.check-changes.outputs.changes == 'true'37 run: |38 git config --local user.email "github-actions[bot]@users.noreply.github.com"39 git config --local user.name "github-actions[bot]"40 git add -A41 git commit -m "Ran cog"42 git push
The [opened, synchronize]
trigger means that the workflow will run whenever a pull request is either created or updated in some way.
Using that user.email
and user.name
causes the bot to get a nice icon of its own:
Squash-merging the PR causes the bot to be credited as a co-author.