Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automate cargo-generate branch updates #263

Merged
merged 18 commits into from
Aug 15, 2024
Merged
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 110 additions & 0 deletions .github/workflows/cargo-generate.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
# This workflow is used to automatically update the `cargo-generate` branch. If you copied the
# template, you can safely delete this. :)
#
# BE VERY CAREFUL about what this workflow runs, since IT HAS THE ABILITY TO DELETE THE ENTIRE
# REPOSITORY'S CONTENTS. For reason, stick to pre-installed tools and trusted actions (such as
BD103 marked this conversation as resolved.
Show resolved Hide resolved
# those hosted from the `actions` organization.)

name: Update `cargo-generate` branch

on:
# Update `cargo-generate` from `main` every time `main` is updated.
push:
branches: main
# Allow this workflow to be manually triggered.
workflow_dispatch:

# This workflows also has write-access for content and workflow, but it is through
# CARGO_GENERATE_PAT.
permissions:
# We need write permissions to create new issues.
issues: write

jobs:
update:
runs-on: ubuntu-latest
# Only run this job on the source repository, skipping forks.
if: ${{ github.repository == 'TheBevyFlock/bevy_quickstart' }}
benfrankel marked this conversation as resolved.
Show resolved Hide resolved
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
# Fetch entire history, since we need it all to successfully merge.
fetch-depth: 0
# Specify a personal access token, since the default one does not let workflows modify
# the `.github/workflows` folder. This PAT needs write permissions for Contents and
# Workflows. I recommend creating a scoped token that only has access to this repository.
token: ${{ secrets.CARGO_GENERATE_PAT }}

- name: Configure Git user
env:
# From https://github.com/actions/checkout/pull/1707.
NAME: github-actions[bot]
EMAIL: 41898282+github-actions[bot]@users.noreply.github.com
run: |
git config user.name $NAME
git config user.email $EMAIL
janhohenheim marked this conversation as resolved.
Show resolved Hide resolved

- name: Switch to `cargo-generate` branch
run: git checkout cargo-generate

- name: Merge `main` branch into `cargo-generate`
id: merge
# We use `--no-edit` to prevent any editor from being opened, since we can't close it.
run: git merge main --no-edit --verbose

- name: Handle merge conflict
# Only run this if merging failed.
if: ${{ failure() && steps.merge.outcome == 'failure' }}
env:
TITLE: Failed to auto-update `cargo-generate`
BODY: |
There were merge conflicts while trying to update `cargo-generate` from `main`. You can
do it manually by running:

```bash
# Update your local copy of the `main` branch.
git switch main
git pull
# Checkout the `cargo-generate` branch.
git checkout cargo-generate
# Merge changes from `main` to `cargo-generate`. You will have to fix merge conflicts.
git merge main
```

<details>
<summary><code>git status</code></summary>

```
GIT_STATUS_IDENTIFIER
```
</details>

<details>
<summary><code>git diff --diff-filter=U</code></summary>

```diff
GIT_DIFF_IDENTIFIER
```
</details>

_This is an automated message created using `cargo-generate.yaml`._
janhohenheim marked this conversation as resolved.
Show resolved Hide resolved
# This is required to use the `gh` CLI.
GH_TOKEN: ${{ github.token }}
run: |
GIT_STATUS=$(git status)

# Filter the diff to only include files with merge conflicts.
GIT_DIFF=$(git diff --diff-filter=U)

# Replace `GIT_STATUS_IDENTIFIER` and `GIT_DIFF_IDENTIFIER` with their outputs.
BODY=${BODY/GIT_STATUS_IDENTIFIER/"$GIT_STATUS"}
BODY=${BODY/GIT_DIFF_IDENTIFIER/"$GIT_DIFF"}

# Create the issue. The text must be in quotes, since it contains spaces.
gh issue create --title "$TITLE" --body "$BODY"
janhohenheim marked this conversation as resolved.
Show resolved Hide resolved

- name: Push changes
# Only run if merging succeeded.
if: ${{ success() && steps.merge.outcome == 'success' }}
run: git push