|
| 1 | +# .github/workflows/docs-deploy.yml |
| 2 | +# GitHub Actions workflow to build and deploy the MkDocs documentation |
| 3 | +# site to GitHub Pages with enhanced CI integration and error handling. |
| 4 | + |
| 5 | +name: 📚 Deploy Documentation |
| 6 | + |
| 7 | +# Controls when the workflow will run |
| 8 | +on: |
| 9 | + push: |
| 10 | + branches: |
| 11 | + - main # Deploy when changes are pushed to the main branch |
| 12 | + paths: # Only run if documentation-related files change |
| 13 | + - "docs/**" |
| 14 | + - "mkdocs.yml" |
| 15 | + - "src/**" # Re-deploy if source code (for mkdocstrings) changes |
| 16 | + - ".github/workflows/docs-deploy.yml" # Re-deploy if this workflow changes |
| 17 | + - "pyproject.toml" # Re-deploy if dependencies change |
| 18 | + - "poetry.lock" # Re-deploy if lock file changes |
| 19 | + workflow_dispatch: # Allows manual triggering |
| 20 | + inputs: |
| 21 | + environment: |
| 22 | + description: 'Deployment environment' |
| 23 | + required: false |
| 24 | + default: 'production' |
| 25 | + type: choice |
| 26 | + options: |
| 27 | + - production |
| 28 | + - staging |
| 29 | + |
| 30 | +# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages |
| 31 | +permissions: |
| 32 | + contents: read |
| 33 | + pages: write |
| 34 | + id-token: write |
| 35 | + |
| 36 | +# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued. |
| 37 | +# However, do NOT cancel in-progress runs as we want to allow these production deployments to complete. |
| 38 | +concurrency: |
| 39 | + group: "pages-${{ github.ref }}" |
| 40 | + cancel-in-progress: false |
| 41 | + |
| 42 | +env: |
| 43 | + PYTHON_VERSION: "3.11" |
| 44 | + POETRY_VERSION: "1.7.1" |
| 45 | + |
| 46 | +jobs: |
| 47 | + # Pre-deployment validation job |
| 48 | + validate: |
| 49 | + name: 🔍 Pre-deployment Validation |
| 50 | + runs-on: ubuntu-latest |
| 51 | + outputs: |
| 52 | + should-deploy: ${{ steps.validation.outputs.should-deploy }} |
| 53 | + |
| 54 | + steps: |
| 55 | + - name: Checkout repository |
| 56 | + uses: actions/checkout@v4 |
| 57 | + with: |
| 58 | + fetch-depth: 0 |
| 59 | + |
| 60 | + - name: Set up Python |
| 61 | + uses: actions/setup-python@v5 |
| 62 | + with: |
| 63 | + python-version: ${{ env.PYTHON_VERSION }} |
| 64 | + |
| 65 | + - name: Install Poetry |
| 66 | + uses: snok/install-poetry@v1 |
| 67 | + with: |
| 68 | + version: ${{ env.POETRY_VERSION }} |
| 69 | + virtualenvs-create: true |
| 70 | + virtualenvs-in-project: true |
| 71 | + installer-parallel: true |
| 72 | + |
| 73 | + - name: Load cached Poetry virtual environment |
| 74 | + id: cached-poetry-dependencies |
| 75 | + uses: actions/cache@v4 |
| 76 | + with: |
| 77 | + path: .venv |
| 78 | + key: venv-docs-${{ runner.os }}-${{ env.PYTHON_VERSION }}-${{ hashFiles('**/poetry.lock') }} |
| 79 | + restore-keys: | |
| 80 | + venv-docs-${{ runner.os }}-${{ env.PYTHON_VERSION }}- |
| 81 | +
|
| 82 | + - name: Install dependencies |
| 83 | + if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true' |
| 84 | + run: poetry install --no-interaction --with docs |
| 85 | + |
| 86 | + - name: Ensure dependencies are installed |
| 87 | + run: poetry install --no-interaction --with docs |
| 88 | + |
| 89 | + - name: Validate MkDocs configuration |
| 90 | + id: validation |
| 91 | + run: | |
| 92 | + # Test build to validate configuration |
| 93 | + poetry run mkdocs build --clean --strict --verbose |
| 94 | +
|
| 95 | + # Validate all internal links |
| 96 | + echo "✅ MkDocs configuration is valid" |
| 97 | + echo "should-deploy=true" >> $GITHUB_OUTPUT |
| 98 | +
|
| 99 | + - name: Run documentation linting |
| 100 | + run: | |
| 101 | + # Check for broken internal links in markdown files |
| 102 | + echo "🔍 Checking documentation quality..." |
| 103 | +
|
| 104 | + # Basic markdown validation could go here |
| 105 | + # You could add markdownlint or other tools |
| 106 | +
|
| 107 | + echo "✅ Documentation validation complete" |
| 108 | +
|
| 109 | + # Main deployment job |
| 110 | + deploy-docs: |
| 111 | + name: 🚀 Build and Deploy |
| 112 | + needs: validate |
| 113 | + if: needs.validate.outputs.should-deploy == 'true' |
| 114 | + environment: |
| 115 | + name: github-pages |
| 116 | + url: ${{ steps.deployment.outputs.page_url }} |
| 117 | + runs-on: ubuntu-latest |
| 118 | + |
| 119 | + steps: |
| 120 | + - name: Checkout repository |
| 121 | + uses: actions/checkout@v4 |
| 122 | + with: |
| 123 | + fetch-depth: 0 # Full history for git info in docs |
| 124 | + |
| 125 | + - name: Set up Python |
| 126 | + uses: actions/setup-python@v5 |
| 127 | + with: |
| 128 | + python-version: ${{ env.PYTHON_VERSION }} |
| 129 | + |
| 130 | + - name: Install Poetry |
| 131 | + uses: snok/install-poetry@v1 |
| 132 | + with: |
| 133 | + version: ${{ env.POETRY_VERSION }} |
| 134 | + virtualenvs-create: true |
| 135 | + virtualenvs-in-project: true |
| 136 | + installer-parallel: true |
| 137 | + |
| 138 | + - name: Load cached Poetry virtual environment |
| 139 | + id: cached-poetry-dependencies-deploy |
| 140 | + uses: actions/cache@v4 |
| 141 | + with: |
| 142 | + path: .venv |
| 143 | + key: venv-docs-${{ runner.os }}-${{ env.PYTHON_VERSION }}-${{ hashFiles('**/poetry.lock') }} |
| 144 | + restore-keys: | |
| 145 | + venv-docs-${{ runner.os }}-${{ env.PYTHON_VERSION }}- |
| 146 | +
|
| 147 | + - name: Install dependencies |
| 148 | + if: steps.cached-poetry-dependencies-deploy.outputs.cache-hit != 'true' |
| 149 | + run: poetry install --no-interaction --with docs |
| 150 | + |
| 151 | + - name: Ensure dependencies are installed |
| 152 | + run: poetry install --no-interaction --with docs |
| 153 | + |
| 154 | + - name: Setup Node.js (for potential JS dependencies) |
| 155 | + uses: actions/setup-node@v4 |
| 156 | + with: |
| 157 | + node-version: '20' |
| 158 | + cache: 'npm' |
| 159 | + cache-dependency-path: | |
| 160 | + docs/package-lock.json |
| 161 | +
|
| 162 | + - name: Build MkDocs site |
| 163 | + run: | |
| 164 | + echo "🏗️ Building MkDocs site..." |
| 165 | + poetry run mkdocs build --clean --strict --verbose |
| 166 | +
|
| 167 | + # Verify build output |
| 168 | + if [ ! -d "site" ]; then |
| 169 | + echo "❌ Build failed: site directory not found" |
| 170 | + exit 1 |
| 171 | + fi |
| 172 | +
|
| 173 | + if [ ! -f "site/index.html" ]; then |
| 174 | + echo "❌ Build failed: index.html not found" |
| 175 | + exit 1 |
| 176 | + fi |
| 177 | +
|
| 178 | + echo "✅ Build completed successfully" |
| 179 | +
|
| 180 | + # Add CNAME file for custom domain AFTER build |
| 181 | + # echo "docs.contextcraft.com" > site/CNAME # Replace with YOUR desired custom domain |
| 182 | +
|
| 183 | + - name: Optimize build output |
| 184 | + run: | |
| 185 | + echo "🎯 Optimizing build output..." |
| 186 | +
|
| 187 | + # Remove unnecessary files |
| 188 | + find site -name "*.map" -delete |
| 189 | + find site -name ".DS_Store" -delete |
| 190 | +
|
| 191 | + echo "✅ Optimization complete" |
| 192 | +
|
| 193 | + - name: Setup Pages |
| 194 | + uses: actions/configure-pages@v4 |
| 195 | + |
| 196 | + - name: Upload GitHub Pages artifact |
| 197 | + uses: actions/upload-pages-artifact@v3 |
| 198 | + with: |
| 199 | + path: ./site |
| 200 | + |
| 201 | + - name: Deploy to GitHub Pages |
| 202 | + id: deployment |
| 203 | + uses: actions/deploy-pages@v4 |
| 204 | + |
| 205 | + - name: Report deployment success |
| 206 | + if: success() |
| 207 | + run: | |
| 208 | + echo "🎉 Documentation deployed successfully!" |
| 209 | + echo "📖 Site URL: ${{ steps.deployment.outputs.page_url }}" |
| 210 | +
|
| 211 | + # Post-deployment validation |
| 212 | + post-deploy-check: |
| 213 | + name: 🧪 Post-deployment Check |
| 214 | + needs: deploy-docs |
| 215 | + runs-on: ubuntu-latest |
| 216 | + if: always() && needs.deploy-docs.result == 'success' |
| 217 | + |
| 218 | + steps: |
| 219 | + - name: Wait for deployment |
| 220 | + run: sleep 30 |
| 221 | + |
| 222 | + - name: Check site accessibility |
| 223 | + run: | |
| 224 | + SITE_URL="${{ needs.deploy-docs.outputs.page_url || 'https://shorzinator.github.io/ContextCraft' }}" |
| 225 | + echo "🔍 Checking site accessibility at: $SITE_URL" |
| 226 | +
|
| 227 | + # Basic HTTP check |
| 228 | + if curl -f -s -I "$SITE_URL" > /dev/null; then |
| 229 | + echo "✅ Site is accessible" |
| 230 | + else |
| 231 | + echo "⚠️ Site may not be immediately accessible (this is normal for first deployments)" |
| 232 | + fi |
| 233 | +
|
| 234 | + - name: Notify on failure |
| 235 | + if: failure() |
| 236 | + run: | |
| 237 | + echo "❌ Post-deployment check failed" |
| 238 | + echo "This might indicate an issue with the deployment" |
0 commit comments