-
Notifications
You must be signed in to change notification settings - Fork 3
394 lines (331 loc) · 14.4 KB
/
Copy pathbuild.yml
File metadata and controls
394 lines (331 loc) · 14.4 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
name: Build Package
on:
push:
branches: [main, dev]
pull_request:
types: [opened, synchronize, closed]
branches: [main, dev]
env:
PYTHON_VERSION: "3.12"
jobs:
build:
name: Build Package
runs-on: ubuntu-latest
if: |
github.event_name == 'push' ||
(github.event_name == 'pull_request' && github.event.action != 'closed') ||
(github.event_name == 'pull_request' && github.event.action == 'closed' && github.event.pull_request.merged == true)
outputs:
version: ${{ steps.get_version.outputs.version }}
has_artifacts: ${{ steps.check_artifacts.outputs.has_artifacts }}
is_merge_commit: ${{ steps.check_merge.outputs.is_merge_commit }}
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
# NEW: Detect if this is a merge commit
- name: Check if merge commit
id: check_merge
run: |
# Check if commit message indicates a PR merge
COMMIT_MSG=$(git log -1 --pretty=%s)
echo "Commit message: $COMMIT_MSG"
if [[ "$COMMIT_MSG" =~ ^Merge\ pull\ request\ #[0-9]+\ from\ .+ ]]; then
echo "is_merge_commit=true" >> $GITHUB_OUTPUT
echo "This is a PR merge commit"
else
echo "is_merge_commit=false" >> $GITHUB_OUTPUT
echo "This is not a PR merge commit"
fi
# ... (all your existing build steps remain the same) ...
- name: Set up Python ${{ env.PYTHON_VERSION }}
uses: actions/setup-python@v5
with:
python-version: ${{ env.PYTHON_VERSION }}
- name: Install build dependencies
run: |
python -m pip install --upgrade pip
pip install build==1.0.3 twine==4.0.2 setuptools>=65.0
- name: Build package
run: |
echo "Building package..."
python -m build --wheel --sdist
- name: Check package integrity
run: |
echo "Checking package integrity..."
if [ ! -d "dist" ] || [ -z "$(ls -A dist)" ]; then
echo "Error: No distribution files found"
exit 1
fi
echo "Distribution files found:"
ls -la dist/
if ! twine check dist/*; then
echo "Warning: twine check failed, performing basic validation..."
for file in dist/*; do
if [[ $file == *.whl ]]; then
echo "Checking wheel file: $file"
python -m zipfile -l "$file" > /dev/null || {
echo "Error: Invalid wheel file $file"
exit 1
}
elif [[ $file == *.tar.gz ]]; then
echo "Checking source distribution: $file"
tar -tzf "$file" > /dev/null || {
echo "Error: Invalid source distribution $file"
exit 1
}
fi
done
echo "Basic package validation completed"
else
echo "Package integrity check passed"
fi
- name: Test package installation
run: |
echo "Testing package installation..."
WHEEL_FILE=$(find dist -name "*.whl" | head -1)
if [ -z "$WHEEL_FILE" ]; then
echo "Error: No wheel file found for testing"
exit 1
fi
echo "Installing wheel: $WHEEL_FILE"
pip install "$WHEEL_FILE"
python -c "
import sys
print('Package installed successfully')
print('Python version:', sys.version)
try:
print('Package import test: SKIPPED (no package specified)')
except ImportError as e:
print('Package import test: FAILED -', e)
except Exception as e:
print('Package import test: ERROR -', e)
"
- name: Get package version
id: get_version
run: |
VERSION=""
if [ -f "pyproject.toml" ]; then
VERSION=$(grep -E '^version = ' pyproject.toml | sed 's/version = "\(.*\)"/\1/' | head -1)
elif [ -f "setup.py" ]; then
VERSION=$(python setup.py --version 2>/dev/null || echo "")
fi
if [ -z "$VERSION" ]; then
VERSION=$(git describe --tags --abbrev=0 2>/dev/null | sed 's/^v//' || echo "")
fi
if [ -z "$VERSION" ] && [ -f ".bumpversion.cfg" ]; then
VERSION=$(grep "current_version" .bumpversion.cfg | cut -d'=' -f2 | tr -d ' ')
fi
if [ -z "$VERSION" ]; then
VERSION="$(date +%Y.%m.%d)-$(git rev-parse --short HEAD)"
fi
echo "Detected version: $VERSION"
echo "version=$VERSION" >> $GITHUB_OUTPUT
- name: Check artifacts
id: check_artifacts
run: |
if [ -d "dist" ] && [ -n "$(ls -A dist)" ]; then
echo "has_artifacts=true" >> $GITHUB_OUTPUT
else
echo "has_artifacts=false" >> $GITHUB_OUTPUT
fi
- name: Upload build artifacts
uses: actions/upload-artifact@v4
with:
name: dist-${{ env.PYTHON_VERSION }}-${{ github.sha }}
path: dist/
retention-days: 30
- name: Build summary
if: always()
run: |
echo "Build Summary"
echo "============="
if [ -d "dist" ] && [ -n "$(ls -A dist)" ]; then
echo "Build Status: SUCCESS"
echo "Files created:"
for file in dist/*; do
if [ -f "$file" ]; then
SIZE=$(stat -c%s "$file" 2>/dev/null || stat -f%z "$file" 2>/dev/null || echo "unknown")
echo " - $(basename "$file") (${SIZE} bytes)"
fi
done
else
echo "Build Status: FAILED - No distribution files created"
fi
echo "Build completed at: $(date -u '+%Y-%m-%d %H:%M:%S UTC')"
# UPDATED: Release job that handles both PR merges and push events
release:
name: Create Release
runs-on: ubuntu-latest
needs: build
if: |
needs.build.outputs.has_artifacts == 'true' && (
(github.event_name == 'pull_request' &&
github.event.action == 'closed' &&
github.event.pull_request.merged == true &&
github.event.pull_request.base.ref == 'main') ||
(github.event_name == 'push' &&
github.ref == 'refs/heads/main' &&
needs.build.outputs.is_merge_commit == 'true')
)
permissions:
contents: write
pull-requests: read
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Download build artifacts
uses: actions/download-artifact@v4
with:
name: dist-${{ env.PYTHON_VERSION }}-${{ github.sha }}
path: dist/
# NEW: Get PR information for push events
- name: Get PR information from merge commit
id: pr_info
if: github.event_name == 'push'
uses: actions/github-script@v7
with:
script: |
const commitMessage = `${{ github.event.head_commit.message }}`;
console.log('Commit message:', commitMessage);
// Extract PR number from merge commit message
const prMatch = commitMessage.match(/Merge pull request #(\d+) from/);
if (prMatch) {
const prNumber = parseInt(prMatch[1]);
console.log('Found PR number:', prNumber);
try {
const { data: pr } = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_request_number: prNumber
});
core.setOutput('pr_number', prNumber);
core.setOutput('pr_title', pr.title);
core.setOutput('pr_body', pr.body || '');
core.setOutput('pr_author', pr.user.login);
core.setOutput('pr_merged_at', pr.merged_at);
core.setOutput('pr_additions', pr.additions);
core.setOutput('pr_deletions', pr.deletions);
core.setOutput('pr_changed_files', pr.changed_files);
return { found: true, number: prNumber };
} catch (error) {
console.log('Error fetching PR:', error);
return { found: false };
}
}
return { found: false };
- name: Generate release notes
id: release_notes
uses: actions/github-script@v7
with:
script: |
let releaseNotes = '';
if (context.eventName === 'pull_request') {
// Original PR event logic
const pr = context.payload.pull_request;
const comments = await github.paginate(github.rest.issues.listComments, {
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pr.number
});
const reviews = await github.paginate(github.rest.pulls.listReviews, {
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: pr.number
});
releaseNotes = `## ${pr.title} (#${pr.number})\n\n`;
if (pr.body) {
releaseNotes += `### Description\n${pr.body}\n\n`;
}
releaseNotes += `### Pull Request Details\n`;
releaseNotes += `- **Author**: @${pr.user.login}\n`;
releaseNotes += `- **Merged**: ${pr.merged_at}\n`;
releaseNotes += `- **Files changed**: ${pr.changed_files}\n`;
releaseNotes += `- **Additions**: +${pr.additions}\n`;
releaseNotes += `- **Deletions**: -${pr.deletions}\n\n`;
const approvedReviews = reviews.filter(review => review.state === 'APPROVED');
if (approvedReviews.length > 0) {
releaseNotes += `### Reviewers\n`;
approvedReviews.forEach(review => {
releaseNotes += `- @${review.user.login}\n`;
});
releaseNotes += `\n`;
}
if (comments.length > 0) {
releaseNotes += `### Discussion Summary\n`;
releaseNotes += `This PR had ${comments.length} comment(s) during review.\n\n`;
}
} else {
// Push event logic - use PR info from previous step
const prNumber = '${{ steps.pr_info.outputs.pr_number }}';
const prTitle = '${{ steps.pr_info.outputs.pr_title }}';
const prBody = '${{ steps.pr_info.outputs.pr_body }}';
const prAuthor = '${{ steps.pr_info.outputs.pr_author }}';
const prMergedAt = '${{ steps.pr_info.outputs.pr_merged_at }}';
const prAdditions = '${{ steps.pr_info.outputs.pr_additions }}';
const prDeletions = '${{ steps.pr_info.outputs.pr_deletions }}';
const prChangedFiles = '${{ steps.pr_info.outputs.pr_changed_files }}';
if (prNumber) {
releaseNotes = `## ${prTitle} (#${prNumber})\n\n`;
if (prBody) {
releaseNotes += `### Description\n${prBody}\n\n`;
}
releaseNotes += `### Pull Request Details\n`;
releaseNotes += `- **Author**: @${prAuthor}\n`;
releaseNotes += `- **Merged**: ${prMergedAt}\n`;
releaseNotes += `- **Files changed**: ${prChangedFiles}\n`;
releaseNotes += `- **Additions**: +${prAdditions}\n`;
releaseNotes += `- **Deletions**: -${prDeletions}\n\n`;
} else {
releaseNotes = `## Release from commit ${context.sha.substring(0, 7)}\n\n`;
releaseNotes += `### Commit Details\n`;
releaseNotes += `- **Message**: ${{ github.event.head_commit.message }}\n`;
releaseNotes += `- **Author**: @${{ github.event.head_commit.author.username }}\n`;
releaseNotes += `- **Timestamp**: ${{ github.event.head_commit.timestamp }}\n\n`;
}
}
releaseNotes += `### Build Information\n`;
releaseNotes += `- **Python Version**: ${{ env.PYTHON_VERSION }}\n`;
releaseNotes += `- **Built on**: ${new Date().toISOString()}\n`;
releaseNotes += `- **Commit**: ${context.sha}\n`;
releaseNotes += `- **Triggered by**: ${context.eventName}\n`;
core.setOutput('notes', releaseNotes);
- name: Create GitHub Release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
VERSION="${{ needs.build.outputs.version }}"
TAG_NAME="v$VERSION"
if ! git tag -l | grep -q "^$TAG_NAME$"; then
git config --local user.name "github-actions[bot]"
git config --local user.email "github-actions[bot]@users.noreply.github.com"
git tag "$TAG_NAME"
git push origin "$TAG_NAME"
fi
cat << 'EOF' > release_notes.md
${{ steps.release_notes.outputs.notes }}
EOF
gh release create "$TAG_NAME" \
--title "Release $VERSION" \
--notes-file release_notes.md \
--latest \
dist/*
- name: Release summary
run: |
echo "Release Created Successfully!"
echo "================================"
echo "Version: ${{ needs.build.outputs.version }}"
echo "Tag: v${{ needs.build.outputs.version }}"
echo "Triggered by: ${{ github.event_name }}"
if [ "${{ github.event_name }}" = "push" ]; then
echo "PR: #${{ steps.pr_info.outputs.pr_number }}"
echo "Author: @${{ steps.pr_info.outputs.pr_author }}"
else
echo "PR: #${{ github.event.pull_request.number }}"
echo "Author: @${{ github.event.pull_request.user.login }}"
fi
echo "Files included:"
ls -la dist/