4
0

deploy.yml 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. name: Deploy
  2. on:
  3. push:
  4. branches: [main, master]
  5. tags:
  6. - 'v*'
  7. workflow_dispatch:
  8. permissions:
  9. contents: read
  10. packages: write
  11. attestations: write
  12. id-token: write
  13. concurrency:
  14. group: ${{ github.workflow }}-${{ github.ref }}
  15. cancel-in-progress: true
  16. env:
  17. REGISTRY: ghcr.io
  18. IMAGE_NAME: ${{ github.repository }}
  19. jobs:
  20. deploy-check:
  21. name: Pre-deployment Checks
  22. runs-on: ubuntu-latest
  23. timeout-minutes: 10
  24. steps:
  25. - name: Checkout repository
  26. uses: actions/checkout@v7
  27. - name: Validate required files
  28. run: |
  29. for file in Dockerfile requirements.txt app.py; do
  30. if [ ! -f "$file" ]; then
  31. echo "❌ Required file missing: $file"
  32. exit 1
  33. fi
  34. done
  35. echo "✅ Required files present"
  36. - name: Lint Dockerfile
  37. continue-on-error: true
  38. uses: hadolint/hadolint-action@v3.3.0
  39. with:
  40. dockerfile: ./Dockerfile
  41. build-docker:
  42. name: Build and Push Docker Image
  43. runs-on: ubuntu-latest
  44. timeout-minutes: 30
  45. needs: deploy-check
  46. steps:
  47. - name: Checkout repository
  48. uses: actions/checkout@v7
  49. - name: Set up QEMU
  50. uses: docker/setup-qemu-action@v4
  51. - name: Set up Docker Buildx
  52. uses: docker/setup-buildx-action@v4
  53. - name: Login to GitHub Container Registry
  54. uses: docker/login-action@v4
  55. with:
  56. registry: ${{ env.REGISTRY }}
  57. username: ${{ github.actor }}
  58. password: ${{ secrets.GITHUB_TOKEN }}
  59. - name: Extract metadata
  60. id: meta
  61. uses: docker/metadata-action@v6
  62. with:
  63. images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
  64. tags: |
  65. type=ref,event=branch
  66. type=ref,event=tag
  67. type=semver,pattern={{version}}
  68. type=semver,pattern={{major}}.{{minor}}
  69. type=sha,prefix={{branch}}-
  70. type=raw,value=latest,enable={{is_default_branch}}
  71. - name: Build and push Docker image
  72. id: build
  73. uses: docker/build-push-action@v7
  74. with:
  75. context: .
  76. push: true
  77. tags: ${{ steps.meta.outputs.tags }}
  78. labels: ${{ steps.meta.outputs.labels }}
  79. annotations: ${{ steps.meta.outputs.annotations }}
  80. platforms: linux/amd64,linux/arm64
  81. cache-from: type=gha
  82. cache-to: type=gha,mode=max
  83. provenance: true
  84. sbom: true
  85. build-args: |
  86. VERSION=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.version'] }}
  87. - name: Smoke test pushed image
  88. timeout-minutes: 2
  89. run: |
  90. FIRST_TAG=$(echo '${{ steps.meta.outputs.tags }}' | head -n 1)
  91. echo "Testing image: $FIRST_TAG"
  92. docker run --rm -d \
  93. --name squidstats-test \
  94. -p 5000:5000 \
  95. -e DATABASE_URL=sqlite:///:memory: \
  96. -e FLASK_DEBUG=false \
  97. "$FIRST_TAG"
  98. # Wait up to 60 seconds for the application to respond
  99. for i in $(seq 1 60); do
  100. if curl -sf http://localhost:5000/ >/dev/null 2>&1; then
  101. echo "✅ Docker image responds on HTTP"
  102. docker stop squidstats-test || true
  103. exit 0
  104. fi
  105. sleep 1
  106. done
  107. echo "❌ Docker image did not respond on HTTP"
  108. docker logs squidstats-test || true
  109. docker stop squidstats-test || true
  110. exit 1