GitHub Actions Workflow for Running Cog

November 6, 2024 ยท View on GitHub

human (Nov 6, 2024, 06:01 AM)

GitHub actions workflow that triggers on any PR - it does pip install -e '.[test]' using Python 3.11 and then runs "just cog"

If anything has changed it commits the file with a commit message of "Ran cog" and pushes the result back to that PR branch

Don't forget to request write permission

assistant (Nov 6, 2024, 06:01 AM)

This is a good use case for an artifact as it's a complete GitHub Actions workflow configuration that would be reused and referenced.

GitHub Actions workflow for running cog

name: Run Cog

on:
  pull_request:
    types: [opened, synchronize]

permissions:
  contents: write
  pull-requests: write

jobs:
  run-cog:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4
        with:
          ref: ${{ github.head_ref }}

      - name: Set up Python 3.11
        uses: actions/setup-python@v5
        with:
          python-version: '3.11'

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -e '.[test]'
          pip install just

      - name: Run cog
        run: just cog

      - name: Check for changes
        id: check-changes
        run: |
          if [ -n "$(git status --porcelain)" ]; then
            echo "changes=true" >> $GITHUB_OUTPUT
          else
            echo "changes=false" >> $GITHUB_OUTPUT
          fi

      - name: Commit and push if changed
        if: steps.check-changes.outputs.changes == 'true'
        run: |
          git config --local user.email "github-actions[bot]@users.noreply.github.com"
          git config --local user.name "github-actions[bot]"
          git add -A
          git commit -m "Ran cog"
          git push

This workflow will:

  1. Trigger on any PR being opened or updated
  2. Request write permissions for both contents and PRs
  3. Check out the PR branch
  4. Set up Python 3.11
  5. Install your package in editable mode with test dependencies
  6. Run the "just cog" command
  7. Check if any files were modified
  8. If changes were detected, commit them with "Ran cog" message and push back to the PR branch

The workflow uses the PR branch reference (github.head_ref) to ensure changes are pushed to the correct branch. Save this as .github/workflows/cog.yml in your repository.

human (Nov 6, 2024, 06:02 AM)

What is synchronized?

assistant (Nov 6, 2024, 06:02 AM)

In the context of GitHub Actions pull_request triggers, synchronize is an event that occurs whenever the PR branch is updated with new commits. This includes:

  1. Pushing new commits to the branch
  2. Force pushing to update existing commits
  3. Rebasing the branch

So by having both opened and synchronize in the types list:

on:
  pull_request:
    types: [opened, synchronize]

The workflow will run both when:

  • A new PR is first created (opened)
  • Any new commits are pushed to the PR branch (synchronize)

This ensures your cog check runs not just on PR creation but also whenever the PR's code is updated. Without synchronize, you'd only catch issues when the PR is first opened, which wouldn't be very useful as people often update their PRs with changes.

human (Nov 6, 2024, 06:48 AM)

Instead of git status --porcelain I want a command which detects if anything has changed accourding to git diff

assistant (Nov 6, 2024, 06:48 AM)

Here's the modified workflow using git diff instead:

update cog-workflow

Find this:

      - name: Check for changes
        id: check-changes
        run: |
          if [ -n "$(git status --porcelain)" ]; then
            echo "changes=true" >> $GITHUB_OUTPUT
          else
            echo "changes=false" >> $GITHUB_OUTPUT
          fi

Replace with this:

      - name: Check for changes
        id: check-changes
        run: |
          if [ -n "$(git diff)" ]; then
            echo "changes=true" >> $GITHUB_OUTPUT
          else
            echo "changes=false" >> $GITHUB_OUTPUT
          fi

This will now check if there are any differences between the working directory and the last commit using git diff. The command git diff shows changes in the working directory that haven't been staged for commit yet.

The key differences from git status --porcelain:

  • git diff shows actual content changes
  • git status --porcelain shows file status in a machine-readable format

Both would work for detecting changes, but git diff is more directly looking at content differences rather than file status.

human (Nov 6, 2024, 06:49 AM)

install just using this:

  • uses: extractions/setup-just@v2

assistant (Nov 6, 2024, 06:49 AM)

I'll update the workflow to use the setup-just action instead of installing via pip.

update cog-workflow

Find this:

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -e '.[test]'
          pip install just

Replace with this:

      - uses: extractions/setup-just@v2

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -e '.[test]'

This change:

  1. Removes pip install just since we'll install it using the dedicated action
  2. Adds the setup-just action before the dependencies step
  3. Keeps the rest of the pip installations the same

The workflow will now use the official setup-just action which is the recommended way to install just in GitHub Actions.

human (Nov 6, 2024, 06:49 AM)

don't bother upgrading pip

assistant (Nov 6, 2024, 06:49 AM)

I'll remove the pip upgrade step.

update cog-workflow

Find this:

      - uses: extractions/setup-just@v2

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -e '.[test]'

Replace with this:

      - uses: extractions/setup-just@v2

      - name: Install dependencies
        run: pip install -e '.[test]'

Now it just directly installs your package with test dependencies, skipping the pip upgrade step.