Eric/cus 10 add automation for deploying to test pypi and prod #4
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.x' | |
- name: Set preview version | |
run: | | |
BASE_VERSION=$(grep -o "__version__.*" socketsecurity/__init__.py | awk '{print $3}' | tr -d "'") | |
SHORT_SHA=$(git rev-parse --short HEAD) | |
PREVIEW_VERSION="${BASE_VERSION}.dev${GITHUB_PR_NUMBER}+${SHORT_SHA}" | |
echo "VERSION=${PREVIEW_VERSION}" >> $GITHUB_ENV | |
# Temporarily update the version in __init__.py | |
sed -i "s/__version__ = \"${BASE_VERSION}\"/__version__ = \"${PREVIEW_VERSION}\"/" socketsecurity/__init__.py | |
- name: Check if version exists on Test PyPI | |
id: version_check | |
run: | | |
if pip index versions --index-url https://test.pypi.org/simple/ socketsecurity | grep -q "${VERSION}"; 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 even if previous steps fail | |
run: | | |
BASE_VERSION=$(echo $VERSION | cut -d'.' -f1-3) | |
sed -i "s/__version__ = \"${VERSION}\"/__version__ = \"${BASE_VERSION}\"/" 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 }} | |
- name: Comment on PR | |
uses: actions/github-script@v7 | |
with: | |
script: | | |
const version = process.env.VERSION; | |
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-${context.issue.number}\` | |
`; | |
github.rest.issues.createComment({ | |
issue_number: context.issue.number, | |
owner: context.repo.owner, | |
repo: context.repo.name, | |
body: comment | |
}) | |
- 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 | |
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 |