-
Notifications
You must be signed in to change notification settings - Fork 0
209 lines (168 loc) · 6.93 KB
/
Copy pathpr-preview-release.yml
File metadata and controls
209 lines (168 loc) · 6.93 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
name: PR Preview Release
on:
pull_request:
types: [labeled, opened, synchronize]
issue_comment:
types: [created]
permissions:
contents: write
pull-requests: write
issues: write
jobs:
preview-release:
# Only run if PR has 'preview-release' label or comment contains '/preview-release'
if: |
(github.event_name == 'pull_request' && contains(github.event.pull_request.labels.*.name, 'preview-release')) ||
(github.event_name == 'issue_comment' &&
github.event.issue.pull_request &&
contains(github.event.comment.body, '/preview-release'))
runs-on: ubuntu-latest
steps:
- name: Get PR details
id: pr
uses: actions/github-script@v9
with:
script: |
let pr;
if (context.eventName === 'pull_request') {
pr = context.payload.pull_request;
} else {
// Get PR from issue comment
const issue = await github.rest.issues.get({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number
});
const prNumber = context.issue.number;
const prData = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber
});
pr = prData.data;
}
core.setOutput('number', pr.number);
core.setOutput('head_ref', pr.head.ref);
core.setOutput('head_sha', pr.head.sha);
core.setOutput('title', pr.title);
return pr;
- name: Checkout PR branch
uses: actions/checkout@v7
with:
ref: ${{ steps.pr.outputs.head_sha }}
fetch-depth: 0
- name: Generate preview version
id: version
run: |
# Get current version from manifest
CURRENT_VERSION=$(python -c "import json; print(json.load(open('custom_components/minibrew/manifest.json'))['version'])")
# Create preview version: current-version-preview.pr-number.short-sha
PR_NUMBER="${{ steps.pr.outputs.number }}"
SHORT_SHA="${{ steps.pr.outputs.head_sha }}"
SHORT_SHA="${SHORT_SHA:0:7}"
PREVIEW_VERSION="${CURRENT_VERSION}-preview.${PR_NUMBER}.${SHORT_SHA}"
echo "version=${PREVIEW_VERSION}" >> $GITHUB_OUTPUT
echo "Preview version: ${PREVIEW_VERSION}"
- name: Update manifest version
run: |
VERSION="${{ steps.version.outputs.version }}"
python -c "
import json
with open('custom_components/minibrew/manifest.json', 'r+') as f:
data = json.load(f)
data['version'] = '${VERSION}'
f.seek(0)
json.dump(data, f, indent=2)
f.truncate()
"
- name: Generate changelog
id: changelog
run: |
PR_TITLE="${{ steps.pr.outputs.title }}"
PR_NUMBER="${{ steps.pr.outputs.number }}"
PR_BRANCH="${{ steps.pr.outputs.head_ref }}"
# Get commits in this PR
git log origin/main..HEAD --pretty=format:"- %s (%h)" --no-merges > commits.txt
# Get contributors in this PR
CONTRIBUTORS=$(git log origin/main..HEAD --pretty=format:"%an" --no-merges | sort -u)
# Build changelog
cat > changelog.md << EOF
## Preview Release from PR #${PR_NUMBER}
**PR Title:** ${PR_TITLE}
**Branch:** ${PR_BRANCH}
### Changes in this Preview
$(cat commits.txt)
### Contributors
$(echo "$CONTRIBUTORS" | while read -r contributor; do echo "- $contributor"; done)
---
**WARNING: This is a preview release for testing purposes only.**
**To test this preview:**
1. Download the release assets
2. Extract to your Home Assistant \`custom_components\` directory
3. Restart Home Assistant
4. Test the changes
5. Report back on PR #${PR_NUMBER}
**Do not use in production!**
EOF
cat changelog.md
- name: Create archive
run: |
mkdir -p release
cp -r custom_components/minibrew release/
cd release
zip -r ../minibrew-${{ steps.version.outputs.version }}.zip minibrew/
- name: Create preview release
id: create_release
uses: softprops/action-gh-release@v3
with:
tag_name: v${{ steps.version.outputs.version }}
name: Preview ${{ steps.version.outputs.version }} (PR #${{ steps.pr.outputs.number }})
body_path: changelog.md
draft: false
prerelease: true
files: |
minibrew-${{ steps.version.outputs.version }}.zip
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Comment on PR
uses: actions/github-script@v9
with:
script: |
const releaseUrl = '${{ steps.create_release.outputs.url }}';
const version = '${{ steps.version.outputs.version }}';
const comment = `## Preview Release Created
A preview release has been created for testing this PR.
**Version:** \`${version}\`
**Release:** ${releaseUrl}
### How to Test
1. Go to the [draft release](${releaseUrl})
2. Download \`minibrew-${version}.zip\`
3. Extract to your Home Assistant \`custom_components\` directory
4. Restart Home Assistant
5. Test the changes and report back here
### Or install directly via HACS:
\`\`\`
# Add custom repository in HACS:
https://github.com/${{ github.repository }}
# Then select version: ${version}
\`\`\`
---
**WARNING: This is a preview release - do not use in production!**
Once testing is complete and approved, this PR can be merged for an official release.`;
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: ${{ steps.pr.outputs.number }},
body: comment
});
- name: Add preview-released label
if: success()
uses: actions/github-script@v9
with:
script: |
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: ${{ steps.pr.outputs.number }},
labels: ['preview-released']
});