name: Deploy on: push: branches: [main, master] tags: - 'v*' workflow_dispatch: permissions: contents: read packages: write attestations: write id-token: write concurrency: group: ${{ github.workflow }}-${{ github.ref }} cancel-in-progress: true env: REGISTRY: ghcr.io IMAGE_NAME: ${{ github.repository }} jobs: deploy-check: name: Pre-deployment Checks runs-on: ubuntu-latest timeout-minutes: 10 steps: - name: Checkout repository uses: actions/checkout@v7 - name: Validate required files run: | for file in Dockerfile requirements.txt app.py; do if [ ! -f "$file" ]; then echo "❌ Required file missing: $file" exit 1 fi done echo "✅ Required files present" - name: Lint Dockerfile continue-on-error: true uses: hadolint/hadolint-action@v3.3.0 with: dockerfile: ./Dockerfile build-docker: name: Build and Push Docker Image runs-on: ubuntu-latest timeout-minutes: 30 needs: deploy-check steps: - name: Checkout repository uses: actions/checkout@v7 - name: Set up QEMU uses: docker/setup-qemu-action@v4 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v4 - name: Login to GitHub Container Registry uses: docker/login-action@v4 with: registry: ${{ env.REGISTRY }} username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - name: Extract metadata id: meta uses: docker/metadata-action@v6 with: images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} tags: | type=ref,event=branch type=ref,event=tag type=semver,pattern={{version}} type=semver,pattern={{major}}.{{minor}} type=sha,prefix={{branch}}- type=raw,value=latest,enable={{is_default_branch}} - name: Build and push Docker image id: build uses: docker/build-push-action@v7 with: context: . push: true tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} annotations: ${{ steps.meta.outputs.annotations }} platforms: linux/amd64,linux/arm64 cache-from: type=gha cache-to: type=gha,mode=max provenance: true sbom: true build-args: | VERSION=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.version'] }} - name: Smoke test pushed image timeout-minutes: 2 run: | FIRST_TAG=$(echo '${{ steps.meta.outputs.tags }}' | head -n 1) echo "Testing image: $FIRST_TAG" docker run --rm -d \ --name squidstats-test \ -p 5000:5000 \ -e DATABASE_URL=sqlite:///:memory: \ -e FLASK_DEBUG=false \ "$FIRST_TAG" # Wait up to 60 seconds for the application to respond for i in $(seq 1 60); do if curl -sf http://localhost:5000/ >/dev/null 2>&1; then echo "✅ Docker image responds on HTTP" docker stop squidstats-test || true exit 0 fi sleep 1 done echo "❌ Docker image did not respond on HTTP" docker logs squidstats-test || true docker stop squidstats-test || true exit 1