|
| 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: echo "::set-output name=changed::$(git status --porcelain | wc -l)" |
| 27 | + |
| 28 | + - name: apply additional changes and fixes |
| 29 | + if: steps.changes.outputs.changed > 0 |
| 30 | + run: | |
| 31 | + poetry lock --no-update # add new dependencies |
| 32 | + poetry install |
| 33 | + poetry run pre-commit run -a || true # we have to fix other issues manually |
| 34 | +
|
| 35 | + - name: Get template versions |
| 36 | + id: get_versions |
| 37 | + if: steps.changes.outputs.changed > 0 |
| 38 | + shell: bash |
| 39 | + run: | |
| 40 | + CURRENT_VERSION=$(git show HEAD:.cruft.json | jello -r "_['commit'][:8]") |
| 41 | + NEXT_VERSION=$(jello -r "_['commit'][:8]" < .cruft.json) |
| 42 | + echo ::set-output name="current_version::$CURRENT_VERSION" |
| 43 | + echo ::set-output name="next_version::$NEXT_VERSION" |
| 44 | +
|
| 45 | + - name: Get changelog |
| 46 | + id: get_changelog |
| 47 | + if: steps.changes.outputs.changed > 0 |
| 48 | + shell: bash |
| 49 | + run: | |
| 50 | + TEMPLATE=$(jello -r "_['template']" < .cruft.json) |
| 51 | + git clone "$TEMPLATE" /tmp/template |
| 52 | + cd /tmp/template |
| 53 | + 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 ';' -) |
| 54 | + body=$(cat <<EOF |
| 55 | + Changes from $TEMPLATE |
| 56 | +
|
| 57 | + $body |
| 58 | + EOF |
| 59 | + ) |
| 60 | + body="${body//'%'/'%25'}" |
| 61 | + body="${body//$'\n'/'%0A'}" |
| 62 | + body="${body//$'\r'/'%0D'}" |
| 63 | + echo ::set-output name="changelog::$body" |
| 64 | +
|
| 65 | + # behaviour if PR already exists: https://github.com/marketplace/actions/create-pull-request#action-behaviour |
| 66 | + - name: Create Pull Request |
| 67 | + env: |
| 68 | + # a PAT is required to be able to update workflows |
| 69 | + GITHUB_TOKEN: ${{ secrets.AUTO_UPDATE_GITHUB_TOKEN }} |
| 70 | + if: ${{ steps.changes.outputs.changed > 0 && env.GITHUB_TOKEN != 0 }} |
| 71 | + uses: peter-evans/create-pull-request@v3 |
| 72 | + with: |
| 73 | + token: ${{ env.GITHUB_TOKEN }} |
| 74 | + commit-message: >- |
| 75 | + chore: update project structure to ${{ steps.get_versions.outputs.next_version }} |
| 76 | + title: "[Actions] Auto-Update cookiecutter template" |
| 77 | + body: ${{ steps.get_changelog.outputs.changelog }} |
| 78 | + branch: chore/auto-update-project-from-template |
| 79 | + delete-branch: true |
0 commit comments