|
| 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 }}" |
0 commit comments