forked from notum-cz/strapi-next-monorepo-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
188 lines (160 loc) · 6.51 KB
/
Copy pathauto-pr.yml
File metadata and controls
188 lines (160 loc) · 6.51 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
name: Auto Create Pull Requests
on:
push:
branches: [dev]
schedule:
# Run daily at 9 AM UTC to check for differences
- cron: "0 9 * * *"
workflow_dispatch: # Allow manual trigger
permissions:
contents: read
pull-requests: write
issues: write
jobs:
auto-pr-dev-to-main:
if: github.ref == 'refs/heads/dev' || github.event_name == 'schedule' || github.event_name == 'workflow_dispatch'
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}
- name: Configure git
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
- name: Check if main branch exists
id: check-main
run: |
if git show-ref --verify --quiet refs/remotes/origin/main; then
echo "exists=true" >> $GITHUB_OUTPUT
else
echo "exists=false" >> $GITHUB_OUTPUT
echo "ERROR: main branch does not exist!"
exit 1
fi
- name: Check for differences between dev and main
id: check-diff
run: |
git fetch origin main
if git diff --quiet origin/dev origin/main; then
echo "has-changes=false" >> $GITHUB_OUTPUT
else
echo "has-changes=true" >> $GITHUB_OUTPUT
fi
- name: Check if PR already exists
id: check-pr
if: steps.check-diff.outputs.has-changes == 'true'
run: |
PR_NUMBER=$(gh pr list --head dev --base main --state open --json number --jq '.[0].number // empty')
if [ -n "$PR_NUMBER" ]; then
echo "exists=true" >> $GITHUB_OUTPUT
echo "number=$PR_NUMBER" >> $GITHUB_OUTPUT
else
echo "exists=false" >> $GITHUB_OUTPUT
echo "number=" >> $GITHUB_OUTPUT
fi
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Extract environment variables from commit messages
id: extract-env-vars
if: steps.check-diff.outputs.has-changes == 'true'
run: |
# Extract environment variables from commit bodies between main and dev
# Uses %b (body only) to skip subject line which may contain false positives
# Look for patterns like env.VARIABLE_NAME or VARIABLE_NAME (CONSTANT_CASE)
git log origin/main..origin/dev --format="%b" --no-merges | \
grep -oE '(env\.)?[A-Z_][A-Z0-9_]{2,}' | \
sed -E 's/^env\.//' | \
grep -E '^[A-Z_][A-Z0-9_]{2,}$' | \
sed 's/^/- `env./' | \
sed 's/$/`/' | \
sort -u > env_vars_list.txt
- name: Create Pull Request dev → main
if: steps.check-diff.outputs.has-changes == 'true' && steps.check-pr.outputs.exists == 'false'
run: |
COMMITS=$(git log origin/main..origin/dev --pretty=format:"%h - %s (%an, %ar)" --no-merges | head -10)
FILES_CHANGED=$(git diff --name-only origin/main origin/dev | head -20)
cat > pr_body.md << 'EOF'
## Development to Production Sync
**Auto-generated PR**: Syncing latest development changes to production environment.
### Recent Commits
```
EOF
echo "$COMMITS" >> pr_body.md
cat >> pr_body.md << 'EOF'
```
### Files Modified
EOF
echo "$FILES_CHANGED" | sed 's/^/- `/' | sed 's/$/`/' >> pr_body.md
# Only add env vars section if variables were found
if [ -s env_vars_list.txt ]; then
cat >> pr_body.md << 'EOF'
### Required Environment Variables
EOF
cat env_vars_list.txt >> pr_body.md
fi
cat >> pr_body.md << 'EOF'
### Pre-Merge Checklist
- [ ] All CI/CD checks are passing
- [ ] Test environment testing completed
- [ ] Breaking changes documented (if any)
- [ ] All Jira issues are linked and moved to specific column in Jira
- [ ] All collections required to backup are set in the repository variables
### Merge Instructions
- **IMPORTANT: Use "Create a merge commit" when merging**
- Do NOT use squash or rebase merge
---
*This PR was created automatically by GitHub Actions*
EOF
gh pr create \
--title "Sync: dev → main" \
--body-file pr_body.md \
--head dev \
--base main \
--label "auto-generated" \
--assignee "${{ github.actor }}"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Update existing Pull Request dev → main
if: steps.check-diff.outputs.has-changes == 'true' && steps.check-pr.outputs.exists == 'true'
run: |
COMMITS=$(git log origin/main..origin/dev --pretty=format:"%h - %s (%an, %ar)" --no-merges | head -10)
FILES_CHANGED=$(git diff --name-only origin/main origin/dev | head -20)
cat > pr_body.md << 'EOF'
## Development to Production Sync
**Auto-generated PR**: Syncing latest development changes to production environment.
### Recent Commits
```
EOF
echo "$COMMITS" >> pr_body.md
cat >> pr_body.md << 'EOF'
```
### Files Modified
EOF
echo "$FILES_CHANGED" | sed 's/^/- `/' | sed 's/$/`/' >> pr_body.md
# Only add env vars section if variables were found
if [ -s env_vars_list.txt ]; then
cat >> pr_body.md << 'EOF'
### Required Environment Variables
EOF
cat env_vars_list.txt >> pr_body.md
fi
cat >> pr_body.md << 'EOF'
### Pre-Merge Checklist
- [ ] All CI/CD checks are passing
- [ ] Test environment testing completed
- [ ] Breaking changes documented (if any)
- [ ] All Jira issues are linked and moved to specific column in Jira
- [ ] All collections required to backup are set in the repository variables
### Merge Instructions
- **IMPORTANT: Use "Create a merge commit" when merging**
- Do NOT use squash or rebase merge
---
*This PR was created automatically by GitHub Actions*
EOF
gh pr edit ${{ steps.check-pr.outputs.number }} \
--body-file pr_body.md
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}