name: Main
on:
  push:
    branches:
      - main
  pull_request:
    types: [opened, synchronize, reopened]

permissions: read-all

jobs:
  Build:
    strategy:
      matrix:
        app_name: [AudioTest, BibleVerse, BookPlayer, Brainfuck, Breakout, Calculator, Diceware, EpubReader, EspNowBridge, GPIO, GameBoy, GraphicsDemo, HelloWorld, M5UnitTest, Magic8Ball, McpScreen, MediaKeys, Mp3Player, MystifyDemo, PocketDungeon, ReynaBot, RobotArm, SerialConsole, Snake, TamaTac, TodoList, TwoEleven, VoiceRecorder]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          persist-credentials: false
      - name: "Build"
        uses: ./.github/actions/build-app
        with:
          app_name: ${{ matrix.app_name }}
  Bundle:
    runs-on: ubuntu-latest
    needs: [Build]
    outputs:
      sdk_version: ${{ steps.release.outputs.sdk_version }}
    steps:
      - uses: actions/checkout@v4
      - name: "Build"
        id: release
        uses: ./.github/actions/release-apps
  PublishApps:
    runs-on: ubuntu-latest
    needs: [Bundle]
    if: (github.event_name == 'push' && github.ref == 'refs/heads/main')
    steps:
      - uses: actions/checkout@v4
        with:
          persist-credentials: false
      - name: "Publish Apps"
        env:
          CDN_ID: ${{ secrets.CDN_ID }}
          CDN_TOKEN_NAME: ${{ secrets.CDN_TOKEN_NAME }}
          CDN_TOKEN_VALUE: ${{ secrets.CDN_TOKEN_VALUE }}
        uses: ./.github/actions/publish-apps
        with:
          sdk_version: ${{ needs.Bundle.outputs.sdk_version }}
