Skip to content

Commit

Permalink
Add sync-upstream.yml workflow (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed Jun 20, 2024
1 parent 12653c7 commit b0a1e8e
Show file tree
Hide file tree
Showing 5 changed files with 264 additions and 0 deletions.
72 changes: 72 additions & 0 deletions .github/workflows/sync-upstream-change.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
name: Sync with Upstream (change)

on:
pull_request_review:
types: [edited, submitted]

jobs:
sync:
runs-on: ubuntu-latest
if: startsWith(github.event.pull_request.head.ref, 'sync-upstream-') && github.event.review.state == 'changes_requested' && github.event.review.body == 'bot change'
steps:
- name: Checkout the repository
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
with:
fetch-depth: 0
ref: ${{ github.event.pull_request.head.ref }}

- name: Reset branch to main
run: |
git reset --hard origin/main
- name: Configure Git
run: |
git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
#TODO: Remove this step once the workflow is ready
- name: Merge the feature branch for the workflow
run: |
git merge --squash origin/gc/sync-upstream
git commit -m "Add sync-upstream.yml workflow (#7)"
- name: Pull changes from upstream and merge
id: merge
run: |
git remote add upstream https://github.com/actions/runner-images.git
git fetch upstream
SHA="$(git rev-parse upstream/main)"
echo "upstream-sha=$SHA" >> $GITHUB_OUTPUT
git merge $SHA || echo "conflicts=true" >> $GITHUB_OUTPUT
- name: Push changes to branch
run: |
git push -f origin ${{ github.event.pull_request.head.ref }}
- name: Set PR body
id: pr-body
run: |
export GITHUB_SERVER_URL='${{ github.server_url }}'
export GITHUB_REPOSITORY='${{ github.repository }}'
export GITHUB_RUN_ID='${{ github.run_id }}'
export GITHUB_RUN_NUMBER='${{ github.run_number }}'
export UPSTREAM_SHA='${{ steps.merge.outputs.upstream-sha }}'
PR_BODY_NO_CONFLICTS_TEMPLATE='.github/workflows/sync-upstream/templates/pr-body-no-conflicts.txt'
PR_BODY_WITH_CONFLICTS_TEMPLATE='.github/workflows/sync-upstream/templates/pr-body-with-conflicts.txt'
echo 'body<<EOF' >> $GITHUB_OUTPUT
if [ "${{ steps.merge.outputs.conflicts }}" == "true" ]; then
envsubst < $PR_BODY_WITH_CONFLICTS_TEMPLATE >> $GITHUB_OUTPUT
else
envsubst < $PR_BODY_NO_CONFLICTS_TEMPLATE >> $GITHUB_OUTPUT
fi
echo EOF >> $GITHUB_OUTPUT
- name: Edit pull request
env:
GH_TOKEN: ${{ github.token }}
run: |
gh pr edit sync-upstream-${{ github.run_number }} \
--repo ${{ github.repository }} \
--body '${{ steps.pr-body.outputs.body }}'
96 changes: 96 additions & 0 deletions .github/workflows/sync-upstream-create.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
name: Sync with Upstream (create)

on:
schedule:
- cron: "0 0 1 * *" # Runs at 00:00 on the first day of every month
workflow_dispatch:
# TODO: Remove this trigger once the workflow is ready
pull_request:
branches:
- main

permissions:
contents: write
pull-requests: write

jobs:
sync:
runs-on: ubuntu-latest
# TODO: Remove this condition once the workflow is ready
if: github.event.pull_request.user.login != 'github-actions[bot]'
steps:
- name: Checkout the repository
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
with:
fetch-depth: 0
ref: main

- name: Create new branch from main
run: |
git checkout -b sync-upstream-${{ github.run_number }}
- name: Configure Git
run: |
git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
#TODO: Remove this step once the workflow is ready
- name: Merge the feature branch for the workflow
run: |
git merge --squash origin/gc/sync-upstream
git commit -m "Add sync-upstream.yml workflow (#7)"
- name: Pull changes from upstream and merge
id: merge
run: |
git remote add upstream https://github.com/actions/runner-images.git
git fetch upstream
SHA="$(git rev-parse upstream/main)"
echo "upstream-sha=$SHA" >> $GITHUB_OUTPUT
if ! git merge -m "Merge commit '$SHA' from actions/runner-images" "${SHA}"; then
echo "conflicts=true" >> $GITHUB_OUTPUT
fi
- name: Handle conflicts with empty commit
if: steps.merge.outputs.conflicts == 'true'
run: |
git merge --abort
git commit --allow-empty -m "Empty commit due to merge conflicts"
- name: Push changes to new branch
run: |
git push --force-with-lease origin sync-upstream-${{ github.run_number }}
- name: Set PR body
id: pr-body
run: |
export GITHUB_SERVER_URL='${{ github.server_url }}'
export GITHUB_REPOSITORY='${{ github.repository }}'
export GITHUB_RUN_ID='${{ github.run_id }}'
export GITHUB_RUN_NUMBER='${{ github.run_number }}'
export UPSTREAM_SHA='${{ steps.merge.outputs.upstream-sha }}'
PR_BODY_NO_CONFLICTS_TEMPLATE='.github/workflows/sync-upstream/templates/pr-body-no-conflicts.txt'
PR_BODY_WITH_CONFLICTS_TEMPLATE='.github/workflows/sync-upstream/templates/pr-body-with-conflicts.txt'
echo 'body<<EOF' >> $GITHUB_OUTPUT
if [ "${{ steps.merge.outputs.conflicts }}" == "true" ]; then
envsubst < $PR_BODY_WITH_CONFLICTS_TEMPLATE >> $GITHUB_OUTPUT
else
envsubst < $PR_BODY_NO_CONFLICTS_TEMPLATE >> $GITHUB_OUTPUT
fi
echo EOF >> $GITHUB_OUTPUT
# TODO: Change base branch once the workflow is ready
- name: Create pull request
env:
GH_TOKEN: ${{ github.token }}
run: |
gh pr create --title 'Update from upstream' \
--body '${{ steps.pr-body.outputs.body }}' \
--base gc/test --head sync-upstream-${{ github.run_number }} \
--repo ${{ github.repository }} \
|| \
gh pr edit sync-upstream-${{ github.run_number }} \
--repo ${{ github.repository }} \
--body '${{ steps.pr-body.outputs.body }}'
51 changes: 51 additions & 0 deletions .github/workflows/sync-upstream-merge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name: Sync with Upstream (merge)

on:
pull_request_review:
types: [edited, submitted]

permissions:
contents: write
pull-requests: write

jobs:
sync:
runs-on: ubuntu-latest
if: startsWith(github.event.pull_request.head.ref, 'sync-upstream-') && github.event.review.state == 'approved' && github.event.review.body == 'bot merge'
steps:
- name: Checkout the repository
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
with:
fetch-depth: 0
ref: ${{ github.event.pull_request.head.ref }}

- name: Configure Git
run: |
git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
# TODO: Change target branch to main once the workflow is ready
- name: Attempt to push changes
id: push
run: |
if ! output=$(git push origin HEAD:gc/test); then
echo "output=$output" >> $GITHUB_OUTPUT
exit 1
fi
- name: Delete branch on success
if: ${{ success() }}
run: |
git push -d origin ${{ github.event.pull_request.head.ref }}
- name: Comment error on failure
if: ${{ failure() }}
env:
GH_TOKEN: ${{ github.token }}
run: |
gh pr comment ${{ github.event.pull_request.number }} --body 'Failed to push changes to main.
Output:
```
${{ steps.push.outputs.output }}
```'
13 changes: 13 additions & 0 deletions .github/workflows/sync-upstream/templates/pr-body-no-conflicts.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# :warning: UPSTREAM CHANGES. DO NOT MERGE MANUALLY.

This automated pull request was created by the [Sync from Upstream (create)]($GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID) workflow.

Updates from upstream were able to be merged successfully without conflicts so no manual interaction should be necessary.

Please review and approve this pull request. The bot will then handle merging the changes to `main`. Do not merge manually.

If you need the bot to update the pull request and redo the merge process, you can do so by leaving a review requesting changes with the comment `bot change`.

When you are ready for the bot to merge these changes, you can do so by leaving a review approving the changes with the comment `bot merge`.

### :white_check_mark: NO CONFLICTS DURING MERGE.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# :warning: UPSTREAM CHANGES. DO NOT MERGE MANUALLY.

This automated pull request was created by the [Sync from Upstream (create)]($GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID) workflow.

There are updates from upstream to be merged, but our local changes conflict with the upstream changes so we need to resolve the merge manually.

To merge the changes manually, use the following commands:
```
# Checkout the branch
git checkout sync-upstream-$GITHUB_RUN_NUMBER
# Reset the branch to main
git reset --hard origin/main
# Pull changes from upstream
git remote add upstream https://github.com/actions/runner-images.git
git fetch upstream
# Merge the changes
git merge -m "Merge commit '$UPSTREAM_SHA' from actions/runner-images" $UPSTREAM_SHA

# Solve the conflicts and continue the merge
git merge --continue

# Push the changes to the branch
git push --force-with-lease origin sync-upstream-$GITHUB_RUN_NUMBER
```

Once you have merged the changes successfully, please get somebody else to review and approve this pull request. The bot will then handle merging the changes to `main`. Do not merge manually.

If you need the bot to update the pull request and redo the merge process, you can do so by leaving a review requesting changes with the comment `bot change`.

When you are ready for the bot to merge these changes, you can do so by leaving a review approving the changes with the comment `bot merge`.

### :x: CONFLICTS DURING MERGE. PLEASE HANDLE THE CONFLICTS APPROPRIATELY.

0 comments on commit b0a1e8e

Please sign in to comment.