I think car-sensor-secrets is being tracked by git? Trying to remove. #20
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: CI — Build & Push Docker Image | |
| on: | |
| push: | |
| branches: ["main", "actions-test", "ST/flask-react-cicd"] | |
| pull_request: | |
| branches: ["main"] | |
| env: | |
| IMAGE_NAME: ${{ vars.DOCKERHUB_USERNAME }}/car-sensor-api | |
| jobs: | |
| # ───────────────────────────────────────── | |
| # 1. Lint & test the Flask app | |
| # ───────────────────────────────────────── | |
| test: | |
| name: Test Flask API | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Set up Python 3.12 | |
| uses: actions/setup-python@v5 | |
| with: | |
| python-version: "3.12" | |
| cache: "pip" | |
| cache-dependency-path: api/requirements.txt | |
| - name: Install dependencies | |
| run: | | |
| python -m pip install --upgrade pip | |
| pip install -r api/requirements.txt | |
| - name: Syntax check (compile all Python files) | |
| run: | | |
| find api/ -name "*.py" -exec python -m py_compile {} \; | |
| echo "✅ All Python files compiled successfully" | |
| # ───────────────────────────────────────── | |
| # 2. Build Docker image & push to DockerHub | |
| # Tagged with the full git SHA (unique per commit) | |
| # ───────────────────────────────────────── | |
| build-and-push: | |
| name: Build & Push to DockerHub | |
| runs-on: ubuntu-latest | |
| needs: test | |
| # Only push on pushes to main/actions-test (not on PRs) | |
| if: github.event_name == 'push' | |
| outputs: | |
| image_tag: ${{ steps.meta.outputs.sha_tag }} | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| # Enable Docker layer caching (speeds up rebuilds significantly) | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| # Log in to Ayman's DockerHub account | |
| - name: Log in to DockerHub | |
| uses: docker/login-action@v3 | |
| with: | |
| username: ${{ vars.DOCKERHUB_USERNAME }} | |
| password: ${{ secrets.DOCKERHUB_TOKEN }} | |
| # Generate image tags: | |
| # - aymanhaq/car-sensor-api:abc1234 (short SHA — unique per commit, used by ArgoCD) | |
| # - aymanhaq/car-sensor-api:latest (always points to most recent push on main) | |
| - name: Generate image metadata & tags | |
| id: meta | |
| run: | | |
| SHORT_SHA="${GITHUB_SHA::7}" | |
| echo "sha_tag=${SHORT_SHA}" >> $GITHUB_OUTPUT | |
| echo "IMAGE_TAG=${SHORT_SHA}" >> $GITHUB_ENV | |
| echo "📦 Image will be tagged: ${{ env.IMAGE_NAME }}:${SHORT_SHA}" | |
| - name: Build and push Docker image | |
| uses: docker/build-push-action@v5 | |
| with: | |
| context: . | |
| file: ./Dockerfile | |
| push: true | |
| # Two tags per push: the immutable SHA tag + rolling 'latest' | |
| tags: | | |
| ${{ env.IMAGE_NAME }}:${{ env.IMAGE_TAG }} | |
| ${{ env.IMAGE_NAME }}:latest | |
| # Pass the SHA into the image as a label for traceability | |
| labels: | | |
| org.opencontainers.image.revision=${{ github.sha }} | |
| org.opencontainers.image.source=${{ github.repositoryUrl }} | |
| # Use GitHub Actions cache to speed up layer pulls | |
| cache-from: type=gha | |
| cache-to: type=gha,mode=max | |
| - name: Print pushed image info | |
| run: | | |
| echo "✅ Pushed: ${{ env.IMAGE_NAME }}:${{ env.IMAGE_TAG }}" | |
| echo "✅ Pushed: ${{ env.IMAGE_NAME }}:latest" |