-
Notifications
You must be signed in to change notification settings - Fork 9
119 lines (99 loc) · 4.23 KB
/
Copy pathmirror-pr-to-build-deploy.yml
File metadata and controls
119 lines (99 loc) · 4.23 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
name: Mirror PR to production for Preview
on:
pull_request:
types: [opened, synchronize, closed]
branches: [main]
permissions:
contents: read
pull-requests: write
jobs:
mirror-pr:
runs-on: ubuntu-latest
if: github.event.action != 'closed'
steps:
- name: Generate GitHub App token
id: app-token
uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0
with:
app-id: ${{ secrets.PROBE_APP_ID }}
private-key: ${{ secrets.PROBE_APP_PRIVATE_KEY }}
owner: TykTechnologies
- name: Checkout
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
fetch-depth: 0
token: ${{ steps.app-token.outputs.token }}
- name: Setup GitHub CLI
run: gh --version
- name: Create or update mirror PR to production
run: |
# Check if mirror PR already exists
MIRROR_PR=$(gh pr list --base production --head ${{ github.head_ref }} --json number --jq '.[0].number // empty')
if [ -z "$MIRROR_PR" ]; then
# Create new mirror PR
echo "Creating new mirror PR..."
# Write PR body to file; template passed via env var to keep YAML valid
printf '%s\n' "$PR_TEMPLATE" "$PR_BODY" > pr_body.txt
# Escape the title properly to handle special characters and spaces
ESCAPED_TITLE=$(printf '%s' "🔄 Preview: ${{ github.event.pull_request.title }}" | sed 's/"/\\"/g')
gh pr create \
--base production \
--head "${{ github.head_ref }}" \
--title "$ESCAPED_TITLE" \
--body-file pr_body.txt \
--draft
echo "✅ Mirror PR created successfully"
else
echo "🔄 Mirror PR #$MIRROR_PR already exists and will be auto-updated"
fi
env:
GH_TOKEN: ${{ steps.app-token.outputs.token }}
PR_BODY: ${{ github.event.pull_request.body }}
PR_TEMPLATE: |
**🔗 Auto-generated mirror PR for Mintlify preview**
**Original PR:** #${{ github.event.number }}
**Author:** @${{ github.event.pull_request.user.login }}
## Purpose
This PR provides a Mintlify preview link for reviewing documentation changes.
## Preview Link
The Mintlify preview will be available once this PR is processed.
## ⚠️ Important Notes
- **Do not merge this PR directly**
- This PR will be auto-merged when the original PR #${{ github.event.number }} is merged
- Make all comments and reviews on the original PR #${{ github.event.number }}
## Changes
cleanup-mirror-pr:
runs-on: ubuntu-latest
if: github.event.action == 'closed'
steps:
- name: Generate GitHub App token
id: app-token
uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0
with:
app-id: ${{ secrets.PROBE_APP_ID }}
private-key: ${{ secrets.PROBE_APP_PRIVATE_KEY }}
owner: TykTechnologies
- name: Checkout
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
fetch-depth: 0
token: ${{ steps.app-token.outputs.token }}
- name: Handle mirror PR when original is closed
run: |
# Find the mirror PR
MIRROR_PR=$(gh pr list --base production --head ${{ github.head_ref }} --json number --jq '.[0].number // empty')
if [ -n "$MIRROR_PR" ]; then
if [ "${{ github.event.pull_request.merged }}" = "true" ]; then
echo "Original PR was merged, closing mirror PR #$MIRROR_PR (no need to merge)..."
gh pr close $MIRROR_PR
echo "✅ Mirror PR #$MIRROR_PR closed"
else
echo "Original PR was closed without merging, closing mirror PR #$MIRROR_PR..."
gh pr close $MIRROR_PR
echo "✅ Mirror PR #$MIRROR_PR closed"
fi
else
echo "No mirror PR found for branch ${{ github.head_ref }}"
fi
env:
GH_TOKEN: ${{ steps.app-token.outputs.token }}