added pr-preview and release workflows #2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.12' | |
- name: Install dependencies | |
run: | | |
python -m pip install --upgrade pip | |
pip install requests | |
- name: Set preview version | |
run: | | |
BASE_VERSION=$(python -c "from socketdev import __version__; print(__version__)") | |
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}\"" > socketdev/__init__.py.tmp | |
cat socketdev/__init__.py | grep -v "__version__" >> socketdev/__init__.py.tmp | |
mv socketdev/__init__.py.tmp socketdev/__init__.py | |
# Verify the change | |
echo "Updated version in __init__.py:" | |
cat socketdev/__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/socket-sdk-python/$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}\"" > socketdev/__init__.py.tmp | |
cat socketdev/__init__.py | grep -v "__version__" >> socketdev/__init__.py.tmp | |
mv socketdev/__init__.py.tmp socketdev/__init__.py | |
- name: Publish to Test PyPI | |
if: steps.version_check.outputs.exists != 'true' | |
uses: pypa/[email protected] | |
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; | |
// Find existing bot comments | |
const comments = await github.rest.issues.listComments({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
issue_number: prNumber, | |
}); | |
const botComment = comments.data.find(comment => | |
comment.user.type === 'Bot' && | |
comment.body.includes('🚀 Preview package published!') | |
); | |
const comment = ` | |
🚀 Preview package published! | |
Install with: | |
\`\`\`bash | |
pip install --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple socket-sdk-python==${version} | |
\`\`\`; | |
if (botComment) { | |
// Update existing comment | |
await github.rest.issues.updateComment({ | |
owner: owner, | |
repo: repo, | |
comment_id: botComment.id, | |
body: comment | |
}); | |
} else { | |
// Create new comment | |
await 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: | | |
for i in {1..30}; do | |
if pip install --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple socket-sdk-python==${VERSION}; then | |
echo "Package ${VERSION} is now available and installable on Test PyPI" | |
pip uninstall -y socket-sdk-python | |
echo "success=true" >> $GITHUB_OUTPUT | |
exit 0 | |
fi | |
echo "Attempt $i: Package not yet installable, waiting 20s... (${i}/30)" | |
sleep 20 | |
done | |
echo "success=false" >> $GITHUB_OUTPUT | |
exit 1 |