Skip to content

Commit 456eba1

Browse files
authored
Add workflow to create main to live PR twice a month (#3488)
1 parent 0fcec4c commit 456eba1

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
name: Merge main to live
2+
3+
on:
4+
schedule:
5+
# Runs on the 1st and 15th of each month at 00:00 UTC
6+
- cron: '0 0 1,15 * *'
7+
workflow_dispatch: # Allow manual trigger
8+
9+
permissions:
10+
contents: read
11+
pull-requests: write
12+
13+
jobs:
14+
check-and-create-pr:
15+
runs-on: ubuntu-latest
16+
# don't run in forks
17+
if: github.repository == 'NuGet/docs.microsoft.com-nuget'
18+
steps:
19+
- name: Check if live is behind main
20+
id: check
21+
env:
22+
GH_TOKEN: ${{ github.token }}
23+
run: |
24+
# Use GitHub API to compare branches
25+
COMPARISON=$(gh api repos/${{ github.repository }}/compare/live...main --template '{"status": "{{.status}}", "ahead_by": {{.ahead_by}}, "behind_by": {{.behind_by}}}')
26+
27+
echo "Comparison result: $COMPARISON"
28+
29+
STATUS=$(echo $COMPARISON | jq -r '.status')
30+
AHEAD_BY=$(echo $COMPARISON | jq -r '.ahead_by')
31+
BEHIND_BY=$(echo $COMPARISON | jq -r '.behind_by')
32+
33+
echo "Status: $STATUS"
34+
echo "Main is ahead by: $AHEAD_BY commits"
35+
echo "Main is behind by: $BEHIND_BY commits"
36+
37+
echo "ahead_by=$AHEAD_BY" >> $GITHUB_OUTPUT
38+
39+
# If status is not 'identical', live is behind main
40+
if [ "$STATUS" != "identical" ] && [ "$AHEAD_BY" -gt 0 ]; then
41+
echo "Live branch is behind main by $AHEAD_BY commits"
42+
echo "needs_pr=true" >> $GITHUB_OUTPUT
43+
else
44+
echo "Live branch is up to date with main"
45+
echo "needs_pr=false" >> $GITHUB_OUTPUT
46+
fi
47+
48+
- name: Check if PR already exists
49+
if: steps.check.outputs.needs_pr == 'true'
50+
id: check_pr
51+
env:
52+
GH_TOKEN: ${{ github.token }}
53+
run: |
54+
# Check for existing open PR from main to live
55+
EXISTING_PR=$(gh pr list --base live --head main --state open --json number --template '{{range .}}{{.number}}{{end}}')
56+
57+
if [ -n "$EXISTING_PR" ] && [ "$EXISTING_PR" != "null" ]; then
58+
echo "PR already exists: #$EXISTING_PR"
59+
echo "pr_exists=true" >> $GITHUB_OUTPUT
60+
echo "pr_number=$EXISTING_PR" >> $GITHUB_OUTPUT
61+
else
62+
echo "No existing PR found"
63+
echo "pr_exists=false" >> $GITHUB_OUTPUT
64+
fi
65+
66+
- name: Create pull request
67+
if: steps.check.outputs.needs_pr == 'true' && steps.check_pr.outputs.pr_exists == 'false'
68+
env:
69+
GH_TOKEN: ${{ github.token }}
70+
run: |
71+
# Create PR to merge main into live
72+
gh pr create \
73+
--base live \
74+
--head main \
75+
--title "Merge main to live - $(date +%Y-%m-%d)" \
76+
--body "This is an automated pull request to merge the latest changes from main into live.
77+
78+
**Scheduled merge**: This PR was automatically created on $(date +%Y-%m-%d) as part of the bi-monthly sync process.
79+
80+
Please review the changes and merge when ready."
81+
82+
- name: Summary
83+
run: |
84+
if [ "${{ steps.check.outputs.needs_pr }}" == "true" ]; then
85+
if [ "${{ steps.check_pr.outputs.pr_exists }}" == "true" ]; then
86+
echo "✅ Live branch is behind main, but PR #${{ steps.check_pr.outputs.pr_number }} already exists"
87+
else
88+
echo "✅ Live branch is behind main by ${{ steps.check.outputs.ahead_by }} commits. Pull request created successfully"
89+
fi
90+
else
91+
echo "✅ Live branch is up to date with main. No action needed"
92+
fi

0 commit comments

Comments
 (0)