deploy.yml 3.9 KB

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