Skip to content

Commit a915cc8

Browse files
committed
feat: add workflow that automatically pulls upstream
1 parent dd0ead3 commit a915cc8

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
on:
2+
schedule:
3+
- cron: "10 10 * * *" # This gives mirror sync, which runs at 9:57, some time to complete.
4+
workflow_dispatch: {}
5+
6+
permissions:
7+
contents: write
8+
pull-requests: write
9+
10+
env:
11+
upstream: "https://github.com/mage-os/mirror-magento2.git"
12+
13+
jobs:
14+
pull-upstream:
15+
name: "Pull Upstream"
16+
runs-on: ubuntu-latest
17+
strategy:
18+
matrix:
19+
# We might support other branches in the future.
20+
branch: [ '2.4-develop' ]
21+
steps:
22+
- name: Checkout
23+
uses: actions/checkout@v3
24+
with:
25+
ref: ${{ matrix.branch }}
26+
fetch-depth: 0
27+
28+
# If there is already a 2.x-develop-upstream branch, we're in the process of handling a conflict.
29+
# Skip any subsequent steps.
30+
- name: Check if we're already resolving conflicts
31+
run: "! git ls-remote --exit-code origin ${{ matrix.branch }}-upstream"
32+
33+
# Add the upstream remote, in normal circumstances this will be mirror-magento2.
34+
- name: Set upstream
35+
run: git remote add upstream ${{ env.upstream }}
36+
37+
# These settings are required for the merge commit.
38+
- name: Configure git
39+
run: |
40+
git config user.email info@mage-os.org
41+
git config user.name "Mage-OS"
42+
43+
# Instead of doing a git pull, we do a fetch followed by a merge.
44+
# This is functionally equivalent, but allows us to capture the output of the merge specifically.
45+
- name: Attempt merge
46+
id: merge
47+
run: |
48+
git fetch upstream ${{ matrix.branch }}
49+
git merge --no-edit FETCH_HEAD 2>&1 | tee merge.log
50+
result_code=${PIPESTATUS[0]}
51+
echo "::set-output name=merge::$(cat merge.log)"
52+
exit $result_code
53+
54+
# When merge succeeds, simply push to the original branch.
55+
# If an upstream branch exists, we'll delete it.
56+
- name: Push
57+
run: |
58+
git push origin ${{ matrix.branch }}
59+
git push --delete origin ${{ matrix.branch }}-upstream || true
60+
61+
# If the merge failed, checkout the upstream branch and push it to our repo.
62+
- name: Create Upstream Branch
63+
id: create_branch
64+
if: failure() && steps.merge.outcome == 'failure'
65+
run: |
66+
git merge --abort
67+
git checkout -b ${{ matrix.branch }}-upstream FETCH_HEAD
68+
git push --set-upstream --force origin ${{ matrix.branch }}-upstream
69+
git remote remove upstream
70+
71+
# If the merge failed, and we successfully created an upstream branch, create a pull request.
72+
- name: Create Pull Request
73+
id: create_pr
74+
if: failure() && steps.merge.outcome == 'failure' && steps.create_branch.outcome == 'success'
75+
uses: devops-infra/action-pull-request@v0.5.0
76+
with:
77+
draft: true
78+
title: "Upstream Merge Conflict (${{ matrix.branch }})"
79+
body: "This PR was automatically generated: a human is required.\n\n${{ steps.merge.outputs.merge }}"
80+
github_token: ${{ secrets.GITHUB_TOKEN }}
81+
source_branch: ${{ matrix.branch }}-upstream
82+
target_branch: ${{ matrix.branch }}
83+
84+
# If the merge failed, and we successfully created a PR, send a message to discord.
85+
- name: Notify Discord
86+
if: failure() && steps.merge.outcome == 'failure' && steps.create_pr.outcome == 'success'
87+
uses: Ilshidur/action-discord@master
88+
env:
89+
DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK }}
90+
DISCORD_USERNAME: "Mage-OS"
91+
DISCORD_EMBEDS: '[{"title": "Upstream Merge Conflict", "description": "Pull Request: ${{ steps.create_pr.outputs.url }}\nAction: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}\n\nConflicts:\n ${{ steps.merge.outputs.merge }}"}]'

0 commit comments

Comments
 (0)