|
| 1 | +# Netlify requires requirements.txt (humans do not) |
| 2 | +name: Sync requirements.txt with pyproject.toml |
| 3 | + |
| 4 | +on: |
| 5 | + pull_request: |
| 6 | + paths: ['pyproject.toml'] |
| 7 | + types: [opened, synchronize, reopened] |
| 8 | + |
| 9 | +permissions: |
| 10 | + contents: write |
| 11 | + |
| 12 | +jobs: |
| 13 | + detect-requirements-delta: |
| 14 | + runs-on: ubuntu-latest |
| 15 | + outputs: |
| 16 | + has_change: ${{ steps.detect.outputs.has_change }} |
| 17 | + |
| 18 | + # Skip if the last commit was from the bot (prevent infinite loops) |
| 19 | + if: github.event.head_commit.author.name != 'github-actions[bot]' |
| 20 | + |
| 21 | + steps: |
| 22 | + - name: Checkout code |
| 23 | + uses: actions/checkout@v4 |
| 24 | + with: |
| 25 | + ref: ${{ github.head_ref || github.ref_name }} |
| 26 | + |
| 27 | + - name: Set up Python |
| 28 | + uses: actions/setup-python@v5 |
| 29 | + with: |
| 30 | + python-version-file: 'pyproject.toml' |
| 31 | + |
| 32 | + - name: Install Poetry |
| 33 | + run: pip install poetry |
| 34 | + |
| 35 | + - name: Detect whether requirements.txt has change |
| 36 | + id: detect |
| 37 | + run: | |
| 38 | + output=$(make requirements.txt 2>&1) |
| 39 | + echo "$output" |
| 40 | +
|
| 41 | + # Check whether Make output suggests file is up to date |
| 42 | + if echo "$output" | grep -qi "up to date\|up-to-date\|already up to date"; then |
| 43 | + echo "has_change=false" >> $GITHUB_OUTPUT |
| 44 | + echo "::notice::requirements.txt seems up to date" |
| 45 | + else |
| 46 | + echo "has_change=true" >> $GITHUB_OUTPUT |
| 47 | + fi |
| 48 | +
|
| 49 | + commit-requirements-delta: |
| 50 | + runs-on: ubuntu-latest |
| 51 | + needs: detect-requirements-delta |
| 52 | + if: needs.detect-requirements-delta.outputs.has_change == 'true' |
| 53 | + |
| 54 | + steps: |
| 55 | + - name: Checkout code |
| 56 | + uses: actions/checkout@v4 |
| 57 | + with: |
| 58 | + ref: ${{ github.head_ref || github.ref_name }} |
| 59 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 60 | + |
| 61 | + - name: Set up Python |
| 62 | + uses: actions/setup-python@v5 |
| 63 | + with: |
| 64 | + python-version-file: 'pyproject.toml' |
| 65 | + |
| 66 | + - name: Install Poetry |
| 67 | + run: pip install poetry |
| 68 | + |
| 69 | + - name: Generate requirements.txt |
| 70 | + run: make requirements.txt |
| 71 | + |
| 72 | + - name: Configure Git |
| 73 | + run: | |
| 74 | + git config user.name "github-actions[bot]" |
| 75 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 76 | +
|
| 77 | + - name: Commit requirements.txt if changed |
| 78 | + run: | |
| 79 | + git add -f requirements.txt |
| 80 | + if git diff --staged --quiet; then |
| 81 | + echo "No changes to requirements.txt" |
| 82 | + else |
| 83 | + git commit -m "chore: auto-update requirements.txt [bot]" |
| 84 | + git push |
| 85 | + fi |
0 commit comments