deploy.yml 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. name: Deploy
  2. on:
  3. push:
  4. branches: [main, master]
  5. workflow_dispatch:
  6. permissions:
  7. contents: read
  8. packages: write
  9. concurrency:
  10. group: ${{ github.workflow }}-${{ github.ref }}
  11. cancel-in-progress: true
  12. jobs:
  13. deploy-check:
  14. name: Pre-deployment Checks
  15. runs-on: ubuntu-latest
  16. timeout-minutes: 10
  17. steps:
  18. - name: Checkout repository
  19. uses: actions/checkout@v4
  20. - name: Set up Python
  21. uses: actions/setup-python@v5
  22. with:
  23. python-version: "3.11"
  24. cache: 'pip'
  25. - name: Install dependencies
  26. run: |
  27. python -m pip install --upgrade pip
  28. if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
  29. - name: Run deployment checks
  30. run: |
  31. # Check if all required environment variables are documented
  32. echo "Checking environment variables..."
  33. python -c "
  34. import os
  35. required_vars = ['FLASK_ENV', 'DATABASE_URL']
  36. missing_vars = []
  37. for var in required_vars:
  38. if not os.getenv(var):
  39. missing_vars.append(var)
  40. if missing_vars:
  41. print(f'⚠️ Missing env vars (should be set in production): {missing_vars}')
  42. else:
  43. print('✅ All required environment variables are documented')
  44. "
  45. - name: Test database connection
  46. run: |
  47. python -c "
  48. import sys
  49. import os
  50. sys.path.insert(0, os.getcwd())
  51. try:
  52. from database.database import get_engine, create_dynamic_tables
  53. engine = get_engine()
  54. create_dynamic_tables(engine)
  55. print('✅ Database connection test passed')
  56. except Exception as e:
  57. print(f'❌ Database connection test failed: {e}')
  58. sys.exit(1)
  59. "
  60. - name: Test application startup
  61. run: |
  62. timeout 10s python app.py &
  63. sleep 5
  64. if pgrep -f "python app.py" > /dev/null; then
  65. echo "✅ Application starts successfully"
  66. pkill -f "python app.py"
  67. else
  68. echo "❌ Application failed to start"
  69. exit 1
  70. fi
  71. build-docker:
  72. name: Build Docker Image
  73. runs-on: ubuntu-latest
  74. timeout-minutes: 20
  75. needs: deploy-check
  76. steps:
  77. - name: Checkout repository
  78. uses: actions/checkout@v4
  79. - name: Set up Docker Buildx
  80. uses: docker/setup-buildx-action@v3
  81. - name: Login to GitHub Container Registry
  82. uses: docker/login-action@v3
  83. with:
  84. registry: ghcr.io
  85. username: ${{ github.actor }}
  86. password: ${{ secrets.GITHUB_TOKEN }}
  87. - name: Extract metadata
  88. id: meta
  89. uses: docker/metadata-action@v5
  90. with:
  91. images: ghcr.io/${{ github.repository_owner }}/squidstats
  92. tags: |
  93. type=ref,event=branch
  94. type=sha,prefix={{branch}}-
  95. type=raw,value=latest,enable={{is_default_branch}}
  96. - name: Build and push Docker image
  97. uses: docker/build-push-action@v5
  98. with:
  99. context: .
  100. push: true
  101. tags: ${{ steps.meta.outputs.tags }}
  102. labels: ${{ steps.meta.outputs.labels }}
  103. cache-from: type=gha
  104. cache-to: type=gha,mode=max
  105. - name: Test Docker image
  106. run: |
  107. docker run --rm -d --name squidstats-test -p 5000:5000 ghcr.io/${{ github.repository_owner }}/squidstats:latest
  108. sleep 10
  109. if docker ps | grep -q squidstats-test; then
  110. echo "✅ Docker image runs successfully"
  111. docker stop squidstats-test
  112. else
  113. echo "❌ Docker image failed to run"
  114. docker logs squidstats-test || true
  115. exit 1
  116. fi