|
| 1 | +# .github/workflows/publish.yml |
| 2 | +name: 🚀 Publish to PyPI |
| 3 | + |
| 4 | +on: |
| 5 | + release: |
| 6 | + types: [published] |
| 7 | + workflow_dispatch: |
| 8 | + inputs: |
| 9 | + publish_to_pypi: |
| 10 | + description: 'Publish to PyPI' |
| 11 | + required: true |
| 12 | + default: 'false' |
| 13 | + type: choice |
| 14 | + options: |
| 15 | + - 'true' |
| 16 | + - 'false' |
| 17 | + |
| 18 | +jobs: |
| 19 | + validation: |
| 20 | + name: 🔍 Pre-publish Validation |
| 21 | + runs-on: ubuntu-latest |
| 22 | + steps: |
| 23 | + - name: 📥 Checkout repository |
| 24 | + uses: actions/checkout@v4 |
| 25 | + with: |
| 26 | + fetch-depth: 0 |
| 27 | + |
| 28 | + - name: 🐍 Set up Python |
| 29 | + uses: actions/setup-python@v5 |
| 30 | + with: |
| 31 | + python-version: '3.11' |
| 32 | + |
| 33 | + - name: 📦 Install Poetry |
| 34 | + uses: snok/install-poetry@v1 |
| 35 | + with: |
| 36 | + version: latest |
| 37 | + virtualenvs-create: true |
| 38 | + virtualenvs-in-project: true |
| 39 | + |
| 40 | + - name: 🔄 Load cached dependencies |
| 41 | + uses: actions/cache@v4 |
| 42 | + with: |
| 43 | + path: .venv |
| 44 | + key: venv-${{ runner.os }}-${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('**/poetry.lock') }} |
| 45 | + |
| 46 | + - name: 🧰 Install dependencies |
| 47 | + run: poetry install --with dev |
| 48 | + |
| 49 | + - name: 🔍 Code quality checks |
| 50 | + run: | |
| 51 | + poetry run ruff check src/ tests/ |
| 52 | + poetry run ruff format --check src/ tests/ |
| 53 | + poetry run mypy src/ |
| 54 | +
|
| 55 | + - name: 🛡️ Security scan |
| 56 | + run: poetry run bandit -r src/ |
| 57 | + |
| 58 | + - name: 🧪 Run test suite |
| 59 | + run: poetry run pytest --cov=src/contextcraft --cov-report=xml --cov-report=term-missing |
| 60 | + |
| 61 | + - name: 📋 Validate package metadata |
| 62 | + run: | |
| 63 | + poetry build |
| 64 | + poetry run twine check dist/* |
| 65 | +
|
| 66 | + build-and-publish: |
| 67 | + name: 📦 Build & Publish Package |
| 68 | + runs-on: ubuntu-latest |
| 69 | + needs: validation |
| 70 | + if: > |
| 71 | + (github.event_name == 'release' && github.event.action == 'published') || |
| 72 | + (github.event_name == 'workflow_dispatch' && github.event.inputs.publish_to_pypi == 'true') |
| 73 | + permissions: |
| 74 | + # IMPORTANT: this permission is mandatory for trusted publishing |
| 75 | + id-token: write |
| 76 | + contents: read |
| 77 | + |
| 78 | + steps: |
| 79 | + - name: 📥 Checkout repository |
| 80 | + uses: actions/checkout@v4 |
| 81 | + with: |
| 82 | + fetch-depth: 0 |
| 83 | + |
| 84 | + - name: 🐍 Set up Python |
| 85 | + uses: actions/setup-python@v5 |
| 86 | + with: |
| 87 | + python-version: '3.11' |
| 88 | + |
| 89 | + - name: 📦 Install Poetry |
| 90 | + uses: snok/install-poetry@v1 |
| 91 | + with: |
| 92 | + version: latest |
| 93 | + virtualenvs-create: true |
| 94 | + virtualenvs-in-project: true |
| 95 | + |
| 96 | + - name: 🔄 Load cached dependencies |
| 97 | + uses: actions/cache@v4 |
| 98 | + with: |
| 99 | + path: .venv |
| 100 | + key: venv-${{ runner.os }}-${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('**/poetry.lock') }} |
| 101 | + |
| 102 | + - name: 🧰 Install dependencies |
| 103 | + run: poetry install --only main |
| 104 | + |
| 105 | + - name: 🏗️ Build package |
| 106 | + run: poetry build |
| 107 | + |
| 108 | + - name: 📊 Package info |
| 109 | + run: | |
| 110 | + echo "📦 Package files:" |
| 111 | + ls -la dist/ |
| 112 | + echo "" |
| 113 | + echo "📋 Package metadata:" |
| 114 | + poetry run twine check dist/* --strict |
| 115 | +
|
| 116 | + - name: 🚀 Publish to PyPI |
| 117 | + uses: pypa/gh-action-pypi-publish@release/v1 |
| 118 | + with: |
| 119 | + print-hash: true |
| 120 | + verbose: true |
| 121 | + |
| 122 | + - name: ✅ Publication success |
| 123 | + run: | |
| 124 | + echo "🎉 ContextCraft successfully published to PyPI!" |
| 125 | + echo "📦 Package: https://pypi.org/project/contextcraft/" |
| 126 | + echo "🔗 Installation: pip install contextcraft" |
| 127 | +
|
| 128 | + post-publish: |
| 129 | + name: 📢 Post-publish Actions |
| 130 | + runs-on: ubuntu-latest |
| 131 | + needs: build-and-publish |
| 132 | + if: success() |
| 133 | + steps: |
| 134 | + - name: 📥 Checkout repository |
| 135 | + uses: actions/checkout@v4 |
| 136 | + |
| 137 | + - name: 📧 Create release notification |
| 138 | + run: | |
| 139 | + echo "🎉 ContextCraft has been published to PyPI!" |
| 140 | + echo "Version: ${{ github.event.release.tag_name || github.sha }}" |
| 141 | + echo "PyPI: https://pypi.org/project/contextcraft/" |
| 142 | + echo "Docs: https://shorzinator.github.io/ContextCraft/" |
| 143 | +
|
| 144 | + - name: 🔄 Trigger documentation update |
| 145 | + uses: actions/github-script@v7 |
| 146 | + with: |
| 147 | + script: | |
| 148 | + github.rest.actions.createWorkflowDispatch({ |
| 149 | + owner: context.repo.owner, |
| 150 | + repo: context.repo.repo, |
| 151 | + workflow_id: 'docs.yml', |
| 152 | + ref: 'main' |
| 153 | + }); |
0 commit comments