-
Notifications
You must be signed in to change notification settings - Fork 60
141 lines (120 loc) · 5.28 KB
/
create-pre-release-pr.yml
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
name: Create Pre-Release PR
on:
push:
branches:
- main
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
permissions:
contents: write
pull-requests: write
env:
NODE_VERSION: 20.14.0
RELEASE_BRANCH: release/bumps
BASE_BRANCH: main
jobs:
check-releasable:
name: Check if Releasable
runs-on: ubuntu-latest
outputs:
is-releasable: ${{ steps.check-commit.outputs.is-releasable }}
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: Check Last Commit
id: check-commit
run: |
last_commit=$(git log -1 --pretty=%B)
if [[ $last_commit == "chore(release): update versions and changelogs" ]]; then
echo "is-releasable=false" >> $GITHUB_OUTPUT
else
echo "is-releasable=true" >> $GITHUB_OUTPUT
fi
create-release-pr:
name: Create Release PR
needs: check-releasable
if: needs.check-releasable.outputs.is-releasable == 'true'
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
- name: Checkout Repository
uses: actions/checkout@v4
with:
fetch-depth: 0
ref: ${{ env.BASE_BRANCH }}
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
- name: Restore Dependencies Cache
uses: actions/cache@v4
id: deps-cache
with:
path: |
node_modules
packages/releaser/node_modules
key: deps-${{ runner.os }}-${{ hashFiles('package-lock.json') }}
- name: Install Dependencies
if: steps.deps-cache.outputs.cache-hit != 'true'
run: npm ci -w packages/releaser
- name: Generate Release Changes
run: |
npm run release -w apps/api -- --verbose --ci
npm run release -w apps/deploy-web -- --verbose --ci
npm run release -w apps/provider-proxy -- --verbose --ci
- name: Get Base Branch SHA
id: get-base-sha
run: |
echo "Getting base branch SHA..."
SHA=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
"https://api.github.com/repos/${{ github.repository }}/git/refs/heads/${{ env.BASE_BRANCH }}" | \
jq -r .object.sha)
echo "base_sha=$SHA" >> $GITHUB_ENV
- name: Check Existing PR
id: check-pr
run: |
echo "Checking for existing PR..."
PR_NUMBER=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
"https://api.github.com/repos/${{ github.repository }}/pulls?head=${{ github.repository_owner }}:${{ env.RELEASE_BRANCH }}&state=open" | \
jq '.[0].number')
echo "pr_number=$PR_NUMBER" >> $GITHUB_ENV
- name: Commit Changes
run: |
echo "Creating release commit..."
FILES_CHANGED=$(git status --porcelain | awk '{print $2}')
TREE_ITEMS="["
COMMA=""
for file in $FILES_CHANGED; do
MODE=$(git ls-files --stage "$file" | awk '{print $1}' || echo "100644")
CONTENT=$(cat "$file" | jq -Rs .)
TREE_ITEMS="${TREE_ITEMS}${COMMA}{\"path\":\"$file\",\"mode\":\"${MODE}\",\"type\":\"blob\",\"content\":${CONTENT}}"
COMMA=","
done
TREE_ITEMS="${TREE_ITEMS}]"
TREE_SHA=$(curl -s -X POST -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
-d "{\"base_tree\":\"${{ env.base_sha }}\",\"tree\":${TREE_ITEMS}}" \
"https://api.github.com/repos/${{ github.repository }}/git/trees" | jq -r .sha)
COMMIT_SHA=$(curl -s -X POST -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
-d "{\"message\":\"chore(release): update versions and changelogs\",\"tree\":\"${TREE_SHA}\",\"parents\":[\"${{ env.base_sha }}\"]}" \
"https://api.github.com/repos/${{ github.repository }}/git/commits" | jq -r .sha)
BRANCH_RESPONSE=$(curl -s -X PATCH -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
-d "{\"sha\":\"${COMMIT_SHA}\",\"force\":true}" \
"https://api.github.com/repos/${{ github.repository }}/git/refs/heads/${{ env.RELEASE_BRANCH }}")
if [[ $(echo "$BRANCH_RESPONSE" | jq -r '.message // empty') == "Reference does not exist" ]]; then
echo "Creating release branch..."
curl -s -X POST -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
-d "{\"ref\":\"refs/heads/${{ env.RELEASE_BRANCH }}\",\"sha\":\"${COMMIT_SHA}\"}" \
"https://api.github.com/repos/${{ github.repository }}/git/refs"
else
echo "Updated release branch"
fi
echo "commit_sha=$COMMIT_SHA" >> $GITHUB_ENV
- name: Create Pull Request
if: env.pr_number == 'null'
run: |
echo "Creating release PR..."
curl -s -X POST -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
-d "{\"title\":\"chore(release): update versions and changelogs\", \"head\":\"${{ env.RELEASE_BRANCH }}\", \"base\":\"${{ env.BASE_BRANCH }}\", \"body\":\"This PR updates versions and changelogs for the next release. Merging will trigger release workflows.\"}" \
"https://api.github.com/repos/${{ github.repository }}/pulls"