name: Build and Push Docker Images on: push: branches: [main] paths: - 'backend/**' - 'frontend/**' - '.github/workflows/docker-build.yml' pull_request: branches: [main] paths: - 'backend/**' - 'frontend/**' workflow_dispatch: env: REGISTRY: ghcr.io BACKEND_IMAGE: ghcr.io/${{ github.repository_owner }}/priceghost-backend FRONTEND_IMAGE: ghcr.io/${{ github.repository_owner }}/priceghost-frontend jobs: # Detect which paths changed changes: runs-on: ubuntu-latest outputs: backend: ${{ steps.filter.outputs.backend }} frontend: ${{ steps.filter.outputs.frontend }} steps: - name: Checkout repository uses: actions/checkout@v4 - name: Check for changes uses: dorny/paths-filter@v3 id: filter with: filters: | backend: - 'backend/**' frontend: - 'frontend/**' build-backend: needs: changes if: needs.changes.outputs.backend == 'true' || github.event_name == 'workflow_dispatch' runs-on: ubuntu-latest permissions: contents: read packages: write steps: - name: Checkout repository uses: actions/checkout@v4 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 - name: Log in to GitHub Container Registry if: github.event_name != 'pull_request' uses: docker/login-action@v3 with: registry: ${{ env.REGISTRY }} username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - name: Extract metadata for Backend id: meta uses: docker/metadata-action@v5 with: images: ${{ env.BACKEND_IMAGE }} tags: | type=ref,event=branch type=sha,prefix= type=raw,value=latest,enable={{is_default_branch}} - name: Build and push Backend image uses: docker/build-push-action@v5 with: context: ./backend push: ${{ github.event_name != 'pull_request' }} tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} cache-from: type=gha cache-to: type=gha,mode=max build-frontend: needs: changes if: needs.changes.outputs.frontend == 'true' || github.event_name == 'workflow_dispatch' runs-on: ubuntu-latest permissions: contents: read packages: write steps: - name: Checkout repository uses: actions/checkout@v4 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 - name: Log in to GitHub Container Registry if: github.event_name != 'pull_request' uses: docker/login-action@v3 with: registry: ${{ env.REGISTRY }} username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - name: Extract metadata for Frontend id: meta uses: docker/metadata-action@v5 with: images: ${{ env.FRONTEND_IMAGE }} tags: | type=ref,event=branch type=sha,prefix= type=raw,value=latest,enable={{is_default_branch}} - name: Build and push Frontend image uses: docker/build-push-action@v5 with: context: ./frontend push: ${{ github.event_name != 'pull_request' }} tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} cache-from: type=gha cache-to: type=gha,mode=max