|
| 1 | +name: Autoupdate project structure |
| 2 | +on: |
| 3 | + workflow_dispatch: |
| 4 | + schedule: |
| 5 | + - cron: "0 0 * * *" # at the end of every day |
| 6 | + |
| 7 | +jobs: |
| 8 | + auto-update-project: |
| 9 | + runs-on: ubuntu-latest |
| 10 | + steps: |
| 11 | + - uses: actions/checkout@v4 |
| 12 | + - name: Set up Python |
| 13 | + uses: actions/setup-python@v5 |
| 14 | + with: |
| 15 | + python-version: "3.12" |
| 16 | + |
| 17 | + - name: Install dependencies |
| 18 | + run: python -m pip install cruft poetry jello tabulate |
| 19 | + |
| 20 | + - name: Update project structure |
| 21 | + run: | |
| 22 | + cruft update -y |
| 23 | +
|
| 24 | + - name: Check if there are changes |
| 25 | + id: changes |
| 26 | + run: | |
| 27 | + git status --porcelain | wc -l |
| 28 | + echo "changed=$(git status --porcelain | wc -l)" >> "$GITHUB_OUTPUT" |
| 29 | +
|
| 30 | +
|
| 31 | + - name: apply additional changes and fixes |
| 32 | + if: steps.changes.outputs.changed > 0 |
| 33 | + run: | |
| 34 | + poetry lock # add new dependencies |
| 35 | + poetry install |
| 36 | + poetry run pre-commit run -a || true # we have to fix other issues manually |
| 37 | +
|
| 38 | + - name: Get template versions |
| 39 | + id: get_versions |
| 40 | + if: steps.changes.outputs.changed > 0 |
| 41 | + shell: bash |
| 42 | + run: | |
| 43 | + CURRENT_VERSION=$(git show HEAD:.cruft.json | jello -r "_['commit'][:8]") |
| 44 | + NEXT_VERSION=$(jello -r "_['commit'][:8]" < .cruft.json) |
| 45 | + echo "current_version=$CURRENT_VERSION next_version=$NEXT_VERSION" |
| 46 | + echo "current_version=$CURRENT_VERSION" >> "$GITHUB_OUTPUT" |
| 47 | + echo "next_version=$NEXT_VERSION" >> "$GITHUB_OUTPUT" |
| 48 | +
|
| 49 | + - name: Get changelog |
| 50 | + id: get_changelog |
| 51 | + if: steps.changes.outputs.changed > 0 |
| 52 | + shell: bash |
| 53 | + run: | |
| 54 | + TEMPLATE=$(jello -r "_['template']" < .cruft.json) |
| 55 | + git clone "$TEMPLATE" /tmp/template |
| 56 | + cd /tmp/template |
| 57 | + body=$( (echo "Date;Change;Hash"; git log --pretty=format:"%as;%s;%h" ${{ steps.get_versions.outputs.current_version }}..${{ steps.get_versions.outputs.next_version }}) | tabulate --header --format github -s ';' -) |
| 58 | + body=$(cat <<EOF |
| 59 | + Changes from $TEMPLATE |
| 60 | +
|
| 61 | + $body |
| 62 | + EOF |
| 63 | + ) |
| 64 | +
|
| 65 | + EOF=$(dd if=/dev/urandom bs=15 count=1 status=none | base64) |
| 66 | + { |
| 67 | + echo "changelog<<$EOF" |
| 68 | + echo "$body" |
| 69 | + echo "$EOF" |
| 70 | + } >> "$GITHUB_OUTPUT" |
| 71 | + echo "$body" |
| 72 | +
|
| 73 | + # behaviour if PR already exists: https://github.com/marketplace/actions/create-pull-request#action-behaviour |
| 74 | + - name: Create Pull Request |
| 75 | + env: |
| 76 | + # a PAT is required to be able to update workflows |
| 77 | + GITHUB_TOKEN: ${{ secrets.GH_TOKEN }} |
| 78 | + if: ${{ steps.changes.outputs.changed > 0 && env.GITHUB_TOKEN != 0 }} |
| 79 | + uses: peter-evans/create-pull-request@v5 |
| 80 | + with: |
| 81 | + token: ${{ env.GITHUB_TOKEN }} |
| 82 | + commit-message: >- |
| 83 | + chore: update project structure to ${{ steps.get_versions.outputs.next_version }} |
| 84 | + title: "[Actions] Auto-Update cookiecutter template" |
| 85 | + body: ${{ steps.get_changelog.outputs.changelog }} |
| 86 | + branch: chore/auto-update-project-from-template |
| 87 | + delete-branch: true |
0 commit comments