1
+ name : PR Preview
2
+ on :
3
+ pull_request :
4
+ types : [opened, synchronize]
5
+
6
+ jobs :
7
+ preview :
8
+ runs-on : ubuntu-latest
9
+ steps :
10
+ - uses : actions/checkout@v4
11
+ - uses : actions/setup-python@v5
12
+ with :
13
+ python-version : ' 3.12'
14
+
15
+ - name : Install dependencies
16
+ run : |
17
+ python -m pip install --upgrade pip
18
+ pip install requests
19
+
20
+ - name : Set preview version
21
+ run : |
22
+ BASE_VERSION=$(python -c "from socketdev import __version__; print(__version__)")
23
+ PREVIEW_VERSION="${BASE_VERSION}.dev${{ github.event.pull_request.number }}${{ github.event.pull_request.commits }}"
24
+ echo "VERSION=${PREVIEW_VERSION}" >> $GITHUB_ENV
25
+
26
+ # Update version in __init__.py
27
+ echo "__version__ = \"${PREVIEW_VERSION}\"" > socketdev/__init__.py.tmp
28
+ cat socketdev/__init__.py | grep -v "__version__" >> socketdev/__init__.py.tmp
29
+ mv socketdev/__init__.py.tmp socketdev/__init__.py
30
+
31
+ # Verify the change
32
+ echo "Updated version in __init__.py:"
33
+ cat socketdev/__init__.py | grep "__version__"
34
+
35
+ - name : Check if version exists on Test PyPI
36
+ id : version_check
37
+ env :
38
+ VERSION : ${{ env.VERSION }}
39
+ run : |
40
+ if curl -s -f https://test.pypi.org/pypi/socket-sdk-python/$VERSION/json > /dev/null; then
41
+ echo "Version ${VERSION} already exists on Test PyPI"
42
+ echo "exists=true" >> $GITHUB_OUTPUT
43
+ else
44
+ echo "Version ${VERSION} not found on Test PyPI"
45
+ echo "exists=false" >> $GITHUB_OUTPUT
46
+ fi
47
+
48
+ - name : Build package
49
+ if : steps.version_check.outputs.exists != 'true'
50
+ run : |
51
+ pip install build
52
+ python -m build
53
+
54
+ - name : Restore original version
55
+ if : always()
56
+ run : |
57
+ BASE_VERSION=$(echo $VERSION | cut -d'.' -f1-3)
58
+ echo "__version__ = \"${BASE_VERSION}\"" > socketdev/__init__.py.tmp
59
+ cat socketdev/__init__.py | grep -v "__version__" >> socketdev/__init__.py.tmp
60
+ mv socketdev/__init__.py.tmp socketdev/__init__.py
61
+
62
+ - name : Publish to Test PyPI
63
+ if : steps.version_check.outputs.exists != 'true'
64
+
65
+ with :
66
+ repository-url : https://test.pypi.org/legacy/
67
+ password : ${{ secrets.TEST_PYPI_TOKEN }}
68
+ verbose : true
69
+
70
+ - name : Comment on PR
71
+ if : steps.version_check.outputs.exists != 'true'
72
+ uses : actions/github-script@v7
73
+ env :
74
+ VERSION : ${{ env.VERSION }}
75
+ with :
76
+ script : |
77
+ const version = process.env.VERSION;
78
+ const prNumber = context.payload.pull_request.number;
79
+ const owner = context.repo.owner;
80
+ const repo = context.repo.repo;
81
+ // Find existing bot comments
82
+ const comments = await github.rest.issues.listComments({
83
+ owner: context.repo.owner,
84
+ repo: context.repo.repo,
85
+ issue_number: prNumber,
86
+ });
87
+
88
+ const botComment = comments.data.find(comment =>
89
+ comment.user.type === 'Bot' &&
90
+ comment.body.includes('🚀 Preview package published!')
91
+ );
92
+
93
+ const comment = `
94
+ 🚀 Preview package published!
95
+
96
+ Install with:
97
+ \`\`\`bash
98
+ pip install --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple socket-sdk-python==${version}
99
+ \`\`\``;
100
+
101
+ if (botComment) {
102
+ // Update existing comment
103
+ await github.rest.issues.updateComment({
104
+ owner: owner,
105
+ repo: repo,
106
+ comment_id: botComment.id,
107
+ body: comment
108
+ });
109
+ } else {
110
+ // Create new comment
111
+ await github.rest.issues.createComment({
112
+ owner: owner,
113
+ repo: repo,
114
+ issue_number: prNumber,
115
+ body: comment
116
+ });
117
+ }
118
+
119
+ - name : Verify package is available
120
+ if : steps.version_check.outputs.exists != 'true'
121
+ id : verify_package
122
+ env :
123
+ VERSION : ${{ env.VERSION }}
124
+ run : |
125
+ for i in {1..30}; do
126
+ if pip install --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple socket-sdk-python==${VERSION}; then
127
+ echo "Package ${VERSION} is now available and installable on Test PyPI"
128
+ pip uninstall -y socket-sdk-python
129
+ echo "success=true" >> $GITHUB_OUTPUT
130
+ exit 0
131
+ fi
132
+ echo "Attempt $i: Package not yet installable, waiting 20s... (${i}/30)"
133
+ sleep 20
134
+ done
135
+ echo "success=false" >> $GITHUB_OUTPUT
136
+ exit 1
0 commit comments