Skip to content

Commit 1c23d72

Browse files
author
gha-runner-images-updater-test[bot]
committed
Add sync-upstream.yml workflow (#7)
1 parent 233c309 commit 1c23d72

File tree

3 files changed

+219
-0
lines changed

3 files changed

+219
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: Sync with Upstream (change)
2+
3+
on:
4+
pull_request_review:
5+
types: [submitted]
6+
7+
jobs:
8+
sync:
9+
runs-on: ubuntu-latest
10+
if: github.event.review.state != 'approved' && github.event.pull_request.user.login == 'gha-runner-images-updater-test[bot]'
11+
steps:
12+
- name: Generate GitHub App token
13+
id: generate-token
14+
uses: actions/create-github-app-token@v1
15+
with:
16+
app-id: ${{ secrets.GH_APP_ID }}
17+
private-key: ${{ secrets.GH_APP_KEY }}
18+
19+
- name: Checkout the repository
20+
uses: actions/checkout@v4
21+
with:
22+
fetch-depth: 0
23+
token: ${{ steps.generate-token.outputs.token }}
24+
ref: ${{ github.event.pull_request.head.ref }}
25+
26+
- name: Reset branch to main
27+
run: |
28+
git reset --hard origin/main
29+
30+
- name: Configure Git
31+
run: |
32+
git config user.name 'gha-runner-images-updater-test[bot]'
33+
git config user.email '172421971+gha-runner-images-updater-test[bot]@[email protected]'
34+
35+
- name: Pull changes from upstream and rebase
36+
id: rebase
37+
run: |
38+
git remote add upstream https://github.com/actions/runner-images.git
39+
git fetch upstream
40+
git rebase upstream/main || echo "conflict=true" >> $GITHUB_OUTPUT
41+
42+
- name: Push changes to branch
43+
run: |
44+
git push -f origin ${{ github.event.pull_request.head.ref }}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
name: Sync with Upstream (create)
2+
3+
on:
4+
schedule:
5+
- cron: "0 0 1 * *" # Runs at 00:00 on the first day of every month
6+
workflow_dispatch:
7+
# TODO: Remove this trigger once the workflow is ready
8+
pull_request:
9+
branches:
10+
- main
11+
12+
jobs:
13+
sync:
14+
runs-on: ubuntu-latest
15+
if: github.event.pull_request.user.login != 'gha-runner-images-updater-test[bot]'
16+
steps:
17+
- name: Generate GitHub App token
18+
id: generate-token
19+
uses: actions/create-github-app-token@v1
20+
with:
21+
app-id: ${{ secrets.GH_APP_ID }}
22+
private-key: ${{ secrets.GH_APP_KEY }}
23+
24+
- name: Checkout the repository
25+
uses: actions/checkout@v4
26+
with:
27+
fetch-depth: 0
28+
token: ${{ steps.generate-token.outputs.token }}
29+
ref: main
30+
31+
- name: Create new branch from main
32+
run: |
33+
git checkout -b update-from-upstream-${{ github.run_number }}
34+
35+
- name: Configure Git
36+
run: |
37+
git config user.name 'gha-runner-images-updater-test[bot]'
38+
git config user.email '172421971+gha-runner-images-updater-test[bot]@[email protected]'
39+
40+
#TODO: Remove this step once the workflow is ready
41+
- name: Merge the feature branch for the workflow
42+
run: |
43+
git merge --squash origin/gc/sync-upstream
44+
git commit -m "Add sync-upstream.yml workflow (#7)"
45+
46+
- name: Pull changes from upstream and rebase
47+
id: rebase
48+
run: |
49+
git remote add upstream https://github.com/actions/runner-images.git
50+
git fetch upstream
51+
git rebase upstream/main || echo "conflicts=true" >> $GITHUB_OUTPUT
52+
53+
- name: Handle conflicts with empty commit
54+
if: steps.rebase.outputs.conflicts == 'true'
55+
run: |
56+
git rebase --abort
57+
git commit --allow-empty -m "Empty commit due to rebase conflicts"
58+
59+
- name: Push changes to new branch
60+
run: |
61+
git push -f origin update-from-upstream-${{ github.run_number }}
62+
63+
- name: Set PR body
64+
id: pr-body
65+
run: |
66+
PR_BODY_CONFLICTS="# :warning: PR WITH REBASE CHANGES. DO NOT MERGE MANUALLY.
67+
68+
This is an automated PR to rebase updates from upstream. Please review the changes and approve. The bot will be responsible for pushing the changes to main and closing the PR.
69+
70+
This pull request was created by the [Sync from Upstream (create)](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}) workflow.
71+
72+
To see the changes in this PR use \`git range-diff origin/main...origin/update-from-upstream-${{ github.run_number }}\`.
73+
74+
This PR contains an empty commit due to conflicts during rebase. To rebase the changes manually, use the following commands:
75+
\`\`\`
76+
# Checkout the branch
77+
git checkout ${{ github.event.pull_request.head.ref }}
78+
# Reset the branch to main
79+
git reset --hard origin/main
80+
# Pull changes from upstream
81+
git remote add upstream https://github.com/actions/runner-images.git
82+
git fetch upstream
83+
# Rebase the changes
84+
git rebase upstream/main
85+
86+
# Solve the conflcits and continue the rebase
87+
git rebase --continue
88+
89+
# Push the changes to the branch
90+
git push -f origin ${{ github.event.pull_request.head.ref }}
91+
\`\`\`
92+
93+
### :x: CONFLICTS DURING REBASE. PLEASE HANDLE THE CONFLICTS APPROPRIATELY."
94+
PR_BODY_NO_CONFLICTS="# :warning: PR WITH REBASE CHANGES. DO NOT MERGE MANUALLY.
95+
96+
This is an automated PR to rebase updates from upstream. Please review the changes and approve. The bot will be responsible for pushing the changes to main and closing the PR.
97+
98+
This pull request was created by the [Sync from Upstream (create)](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}) workflow.
99+
100+
To see the changes in this PR use \`git range-diff origin/main...origin/update-from-upstream-${{ github.run_number }}\`.
101+
102+
### :white_check_mark: NO CONFLICTS DURING REBASE."
103+
echo 'body<<EOF' >> $GITHUB_OUTPUT
104+
if [ "$conflicts" == "true" ]; then
105+
echo "$PR_BODY_CONFLICTS" >> $GITHUB_OUTPUT
106+
else
107+
echo "$PR_BODY_NO_CONFLICTS" >> $GITHUB_OUTPUT
108+
fi
109+
echo EOF >> $GITHUB_OUTPUT
110+
111+
- name: Create pull request
112+
env:
113+
GH_TOKEN: ${{ steps.generate-token.outputs.token }}
114+
run: |
115+
gh pr create --title "Update from upstream" \
116+
--body "${{ steps.pr-body.outputs.body }}" \
117+
--base main --head update-from-upstream-${{ github.run_number }} \
118+
--repo ${{ github.repository }} \
119+
|| \
120+
gh pr edit update-from-upstream-${{ github.run_number }} \
121+
--repo ${{ github.repository }} \
122+
--body "${{ steps.pr-body.outputs.body }}"
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
name: Sync with Upstream (push)
2+
3+
on:
4+
pull_request_review:
5+
types: [submitted]
6+
7+
jobs:
8+
sync:
9+
runs-on: ubuntu-latest
10+
if: github.event.review.state == 'approved' && github.event.pull_request.user.login == 'gha-runner-images-updater-test[bot]'
11+
steps:
12+
- name: Generate GitHub App token
13+
id: generate-token
14+
uses: actions/create-github-app-token@v1
15+
with:
16+
app-id: ${{ secrets.GH_APP_ID }}
17+
private-key: ${{ secrets.GH_APP_KEY }}
18+
19+
- name: Checkout the repository
20+
uses: actions/checkout@v4
21+
with:
22+
fetch-depth: 0
23+
token: ${{ steps.generate-token.outputs.token }}
24+
25+
- name: Configure Git
26+
run: |
27+
git config user.name 'gha-runner-images-updater-test[bot]'
28+
git config user.email '172421971+gha-runner-images-updater-test[bot]@[email protected]'
29+
30+
- name: Check if changes will be removed from main
31+
id: check-changes
32+
run: |
33+
git fetch origin
34+
changest_removed=$(git range-diff origin/main...origin/${{ github.event.pull_request.head.ref }} | grep -q '<')
35+
if [ -n "$changes_removed" ]; then
36+
echo "Commits will be removed from main, this shouldn't happen. Please request changes on the pull request."
37+
exit 1
38+
else
39+
echo "Commits will not be removed from main."
40+
exit 0
41+
fi
42+
43+
- name: Push changes to main
44+
run: |
45+
git push -f origin main
46+
47+
- name: Close pull request
48+
env:
49+
GH_TOKEN: ${{ steps.generate-token.outputs.token }}
50+
run: |
51+
gh pr close ${{ github.event.pull_request.number }}\
52+
--repo ${{ github.repository }}\
53+
--comment "This PR was automatically closed by the bot after pushing the changes to main."

0 commit comments

Comments
 (0)