Skip to content

Commit

Permalink
Add scheduled workflow for cruft updates
Browse files Browse the repository at this point in the history
  • Loading branch information
olzhasar-reef committed Nov 30, 2024
1 parent c2504ec commit 8083301
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,24 @@ pip install cruft
More on cruft:
<https://github.com/cruft/cruft>

## Automatic cruft updates

This template ships with a GitHub Actions workflow that will periodically (once a week) monitor changes in the template and automatically create a pull request with updates using `cruft`.

### Setup

The workflow requires permissions to create pull requests. You can enable it by going to Repository Settings -> Actions -> General -> Allow GitHub Actions to create and approve pull requests.

### Slack notifications (optional)

The bot can send notifications to a Slack channel when a new pull request with updates is created.

To enable this, you need to set two secrets in your repository:
- `SLACK_BOT_TOKEN` (the token of your Slack app)
- `SLACK_CHANNEL_ID` (the ID of the channel where you want to receive notifications)

If you don't have a Slack app, follow the [instructions here](https://github.com/slackapi/slack-github-action?tab=readme-ov-file#technique-2-slack-api-method) to create one.

## License

This project is licensed under the terms of the [BSD-3 License](/LICENSE)
Expand Down
87 changes: 87 additions & 0 deletions {{cookiecutter.repostory_name}}/.github/workflows/cruft.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
name: Update repository with Cruft
permissions:
contents: write
pull-requests: write
on:
schedule:
- cron: "0 2 * * 1" # Every Monday at 2am
workflow_dispatch:

jobs:
update:
runs-on: ubuntu-latest
env:
SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}
SLACK_CHANNEL_ID: ${{ secrets.SLACK_CHANNEL_ID }}
steps:
- uses: actions/checkout@v4

- uses: actions/setup-python@v5
with:
python-version: "3.11"

- name: Install Cruft
run: pip3 install cruft

- name: Check if update is available
continue-on-error: false
id: check
run: |
CHANGES=0
if [ -f .cruft.json ]; then
if ! cruft check; then
CHANGES=1
fi
else
echo "No .cruft.json file"
fi
echo "has_changes=$CHANGES" >> "$GITHUB_OUTPUT"
- name: Run update if available
if: steps.check.outputs.has_changes == '1'
run: |
git config --global user.email "[email protected]"
git config --global user.name "Cruft Updater"
cruft update --skip-apply-ask --refresh-private-variables
git restore --staged .
- name: Check for .rej files
if: steps.check.outputs.has_changes == '1'
id: check_rej
run: |
REJ_FILES=$(find . -name "*.rej")
if [ -n "$REJ_FILES" ]; then
echo "has_rej_files=1" >> "$GITHUB_OUTPUT"
else
echo "has_rej_files=0" >> "$GITHUB_OUTPUT"
fi
- name: Create pull request
if: steps.check.outputs.has_changes == '1'
id: create_pr
uses: peter-evans/create-pull-request@v7
with:
token: ${{ secrets.GITHUB_TOKEN }}
add-paths: .
commit-message: "Apply new Cruft update"
branch: cruft/update
delete-branch: true
title: Cruft cookiecutter update
body: |
This is an autogenerated PR.
${{ steps.check_rej.outputs.has_rej_files == '1' && 'IMPORTANT: One or more `.rej` files have been detected, which means that some changes could not be applied automatically. Please RESOLVE them manually' || 'No conflicts detected' }}
- name: Post to a Slack channel
if: steps.create_pr.outputs.pull-request-number && env.SLACK_BOT_TOKEN
uses: slackapi/[email protected]
with:
method: chat.postMessage
token: ${{ env.SLACK_BOT_TOKEN }}
payload: |
channel: ${{ env.SLACK_CHANNEL_ID }}
text: |
<!here> cruft updates for `${{ github.repository }}`:
${{ steps.create_pr.outputs.pull-request-url }}

0 comments on commit 8083301

Please sign in to comment.