Repository navigation
80 lines (71 loc) · 3.4 KB
/
Copy pathsync-main-to-develop.yml
File metadata and controls
80 lines (71 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
name: Sync Main back to Develop
on:
workflow_run:
workflows: ["Production Environment"]
types:
- completed
jobs:
create-sync-pr:
name: Create Sync Pull Request
if: github.event.workflow_run.conclusion == 'success'
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
- name: Checkout develop branch
uses: actions/checkout@v4
with:
ref: develop
- name: Configure Git User
run: |
git config --global user.name "mrdeveloper-bot"
git config --global user.email "bot@mrdeveloper.dev"
- name: Merge main into sync branch and push
id: merge
run: |
echo "Starting sync process..."
git fetch origin main develop
if git merge-base --is-ancestor origin/main origin/develop; then
echo "main is already merged into develop. No sync needed."
echo "sync_needed=false" >> $GITHUB_OUTPUT
exit 0
fi
echo "main has new commits. Proceeding with sync..."
echo "sync_needed=true" >> $GITHUB_OUTPUT
BRANCH_NAME="sync/main-to-develop"
echo "branch_name=$BRANCH_NAME" >> $GITHUB_OUTPUT
git checkout -B $BRANCH_NAME origin/develop
# Tenta fazer o merge da 'main' na branch de sincronização.
# A estrutura 'if/else' trata o código de saída, então o passo não falhará em caso de conflitos.
if git merge origin/main --allow-unrelated-histories --no-edit -m "chore: sync main back to develop"; then
# Se o merge for bem-sucedido e não houver conflitos.
echo "Merge da main para a branch de sync foi bem-sucedido, sem conflitos."
else
# Se o merge falhar (geralmente por conflitos).
echo "::warning::O merge falhou, provavelmente devido a conflitos. Arquivos com marcadores de conflito serão commitados para resolução manual."
# Após uma falha de merge, os arquivos com conflitos estão no diretório de trabalho.
# Adicionamos e commitamos esses arquivos para que possam ser resolvidos no Pull Request.
git add .
git commit -m "chore: Sincroniza main com conflitos para resolução manual"
fi
# Faz o push do resultado para o repositório, seja um merge limpo ou com conflitos.
git push --force https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${GITHUB_REPOSITORY}.git HEAD:$BRANCH_NAME
- name: Create or Update Pull Request
if: steps.merge.outputs.sync_needed == 'true'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
BRANCH_NAME: ${{ steps.merge.outputs.branch_name }}
run: |
EXISTING_PR_URL=$(gh pr list --base develop --head "$BRANCH_NAME" --state open --json url --jq '.[0].url')
if [ -n "$EXISTING_PR_URL" ]; then
echo "PR already exists: $EXISTING_PR_URL"
gh pr comment "$EXISTING_PR_URL" --body "🔄 The sync branch has been updated with the latest changes from \`main\`."
else
echo "Creating new PR..."
gh pr create \
--base develop \
--head "$BRANCH_NAME" \
--title "🔄 [Auto-Sync] Merge main back into develop" \
--body "This PR automatically syncs the latest production release from \`main\` back into \`develop\`."
fi