forked from locainin/UnixNotis
-
Notifications
You must be signed in to change notification settings - Fork 0
129 lines (109 loc) · 4.3 KB
/
sync-dev-from-master.yml
File metadata and controls
129 lines (109 loc) · 4.3 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
name: Sync dev from master
on:
push:
branches:
- master
workflow_dispatch:
permissions:
contents: write
pull-requests: write
issues: write
jobs:
sync-dev:
runs-on: ubuntu-latest
steps:
- name: Check out repository history
uses: actions/checkout@v5
with:
fetch-depth: 0
- name: Set Git identity
run: |
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
- name: Fetch master and dev
run: |
git fetch origin master dev
- name: Fast-forward dev to master
id: sync
continue-on-error: true
run: |
set -euo pipefail
# Start from the tracked remote branch so each run sees the real dev head.
git checkout -B dev origin/dev
# Release merges should already contain dev. If they do, keep dev as a
# simple fast-forward of master instead of creating an extra merge commit.
if ! git merge-base --is-ancestor origin/dev origin/master; then
echo "origin/dev is not an ancestor of origin/master"
exit 1
fi
git merge --ff-only origin/master
- name: Push updated dev
id: push
if: steps.sync.outcome == 'success'
continue-on-error: true
run: |
set -euo pipefail
git push origin HEAD:dev
- name: Mark successful sync in workflow summary
if: steps.sync.outcome == 'success' && steps.push.outcome == 'success'
run: |
{
echo "## dev sync"
echo
echo "dev was fast-forwarded to master and pushed successfully"
} >> "$GITHUB_STEP_SUMMARY"
- name: Open sync PR or issue when sync fails
if: steps.sync.outcome != 'success' || steps.push.outcome != 'success'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SYNC_OUTCOME: ${{ steps.sync.outcome }}
WORKFLOW_NAME: ${{ github.workflow }}
RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
MASTER_SHA: ${{ github.sha }}
REPOSITORY: ${{ github.repository }}
run: |
set -euo pipefail
# Keep the failure reason short so the fallback PR or issue is readable
if [ "${SYNC_OUTCOME}" != "success" ]; then
reason="automatic fast-forward from master into dev failed"
else
reason="automatic push of synced dev branch failed"
fi
body_file="$(mktemp)"
{
echo "## dev sync needs attention"
echo
echo "${reason}"
echo
echo "- workflow: ${WORKFLOW_NAME}"
echo "- run: ${RUN_URL}"
echo "- master sha: ${MASTER_SHA}"
echo
echo "This was triggered by a push to \`master\` and did not complete the expected fast-forward \`master -> dev\` sync"
} > "${body_file}"
existing_pr="$(gh pr list --base dev --head master --state open --json number --jq '.[0].number // empty')"
if [ -n "${existing_pr}" ]; then
gh pr comment "${existing_pr}" --body-file "${body_file}" || true
echo "Opened work already exists as PR #${existing_pr}" >> "$GITHUB_STEP_SUMMARY"
exit 1
fi
if gh pr create \
--base dev \
--head master \
--title "Sync dev from master" \
--body-file "${body_file}"
then
echo "Opened a sync PR from master into dev" >> "$GITHUB_STEP_SUMMARY"
exit 1
fi
existing_issue="$(gh issue list --state open --search "in:title \"Sync dev from master\" repo:${REPOSITORY}" --json number --jq '.[0].number // empty')"
if [ -n "${existing_issue}" ]; then
gh issue comment "${existing_issue}" --body-file "${body_file}" || true
echo "Opened work already exists as issue #${existing_issue}" >> "$GITHUB_STEP_SUMMARY"
exit 1
fi
gh issue create \
--title "Sync dev from master failed" \
--body-file "${body_file}"
echo "Opened a sync issue because the PR path was not available" >> "$GITHUB_STEP_SUMMARY"
exit 1