-
Notifications
You must be signed in to change notification settings - Fork 5
133 lines (116 loc) · 4.72 KB
/
pr-preview.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
name: PR Preview
on:
pull_request:
types: [opened, synchronize]
jobs:
preview:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.x'
- name: Set preview version
run: |
BASE_VERSION=$(grep -o "__version__.*" socketsecurity/__init__.py | awk '{print $3}' | tr -d "'")
PREVIEW_VERSION="${BASE_VERSION}.dev${{ github.event.pull_request.number }}${{ github.event.pull_request.commits }}"
echo "VERSION=${PREVIEW_VERSION}" >> $GITHUB_ENV
# Update version in __init__.py
echo "__version__ = \"${PREVIEW_VERSION}\"" > socketsecurity/__init__.py.tmp
cat socketsecurity/__init__.py | grep -v "__version__" >> socketsecurity/__init__.py.tmp
mv socketsecurity/__init__.py.tmp socketsecurity/__init__.py
# Verify the change
echo "Updated version in __init__.py:"
cat socketsecurity/__init__.py | grep "__version__"
- name: Check if version exists on Test PyPI
id: version_check
env:
VERSION: ${{ env.VERSION }}
run: |
if curl -s -f https://test.pypi.org/pypi/socketsecurity/$VERSION/json > /dev/null; then
echo "Version ${VERSION} already exists on Test PyPI"
echo "exists=true" >> $GITHUB_OUTPUT
else
echo "Version ${VERSION} not found on Test PyPI"
echo "exists=false" >> $GITHUB_OUTPUT
fi
- name: Build package
if: steps.version_check.outputs.exists != 'true'
run: |
pip install build
python -m build
- name: Restore original version
if: always()
run: |
BASE_VERSION=$(echo $VERSION | cut -d'.' -f1-3)
echo "__version__ = \"${BASE_VERSION}\"" > socketsecurity/__init__.py.tmp
cat socketsecurity/__init__.py | grep -v "__version__" >> socketsecurity/__init__.py.tmp
mv socketsecurity/__init__.py.tmp socketsecurity/__init__.py
- name: Publish to Test PyPI
if: steps.version_check.outputs.exists != 'true'
uses: pypa/gh-action-pypi-publish@v1.8.11
with:
repository-url: https://test.pypi.org/legacy/
password: ${{ secrets.TEST_PYPI_TOKEN }}
verbose: true
- name: Comment on PR
if: steps.version_check.outputs.exists != 'true'
uses: actions/github-script@v7
env:
VERSION: ${{ env.VERSION }}
with:
script: |
const version = process.env.VERSION;
const prNumber = context.payload.pull_request.number;
const owner = context.repo.owner;
const repo = context.repo.repo;
// Log the values for debugging
console.log("Owner:", context.repo.owner);
console.log("Repo:", context.repo.repo);
console.log("Context Repo:", JSON.stringify(context.repo, null, 2));
const comment = `
🚀 Preview package published!
Install with:
\`\`\`bash
pip install --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple socketsecurity==${version}
\`\`\`
Docker image: \`socketdev/cli:pr-${prNumber}\`
`;
github.rest.issues.createComment({
owner: owner,
repo: repo,
issue_number: prNumber,
body: comment
});
- name: Verify package is available
if: steps.version_check.outputs.exists != 'true'
id: verify_package
env:
VERSION: ${{ env.VERSION }}
run: |
# Wait for package to be available (try up to 30 times with 20s delay - total 10 minutes)
for i in {1..30}; do
if curl -s -f https://test.pypi.org/pypi/socketsecurity/$VERSION/json > /dev/null; then
echo "Package ${VERSION} is now available on Test PyPI"
exit 0
fi
echo "Attempt $i: Package not yet available, waiting 20s... (${i}/30)"
sleep 20
done
echo "Package ${VERSION} not available after 10 minutes"
exit 1
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build & Push Docker Preview
uses: docker/build-push-action@v5
env:
VERSION: ${{ env.VERSION }}
with:
push: true
tags: socketdev/cli:pr-${{ github.event.pull_request.number }}
build-args: |
CLI_VERSION=${{ env.VERSION }}
PIP_INDEX_URL=https://test.pypi.org/simple