name: Build

on:
  push:
    branches: [ 'main' ]
    paths-ignore:
      - '**.md'
      - '.gitea/workflows/release.yml'
      - 'renovate.json'
  workflow_dispatch:

env:
  PHP_VERSION: '8.3'                  # set this to the PHP version to use
  COMPOSER_ALLOW_SUPERUSER: 1

permissions:
  contents: read

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3

      - name: Setup PHP
        uses: shivammathur/setup-php@7c0b4c8c8ebed23eca9ec2802474895d105b11bc
        with:
          php-version: ${{ env.PHP_VERSION }}

      - name: Run composer install if composer.json exists
        run: | 
          composer validate --no-check-publish && composer install --prefer-dist --no-progress --ignore-platform-reqs

      - name: Set up Node.js
        uses: actions/setup-node@v3
        with:
          node-version: '16'

      - name: Install dependencies
        run: npm install

      - name: Build CSS
        run: npm run build:css

      - name: Create Artifact
        run:  tar -czf typo3.tar.gz bin public packages config vendor build composer.json composer.lock

      - uses: actions/upload-artifact@v3
        with:
          name: typo3
          path: typo3.tar.gz

  deploy-stage:
    needs: build
    runs-on: ubuntu-latest
    steps:
      - name: Setup PHP
        uses: shivammathur/setup-php@7c0b4c8c8ebed23eca9ec2802474895d105b11bc
        with:
          php-version: ${{ env.PHP_VERSION }}
      - uses: actions/download-artifact@v3
        with:
          name: typo3
      - name: Extract artifact
        run: |
          tar xf typo3.tar.gz
          rm typo3.tar.gz
      - name: Install ssh agent and rsync
        run: |
          apt update
          apt install -y openssh-client rsync
      - name: Upload release
        uses: deployphp/action@v1
        with:
          deployer-binary: "./bin/dep"
          dep: --file=./build/deploy.php release:create stage
          private-key: ${{secrets.STAGE_KEY}}
  switch-stage:
    needs: deploy-stage
    runs-on: ubuntu-latest
    steps:
      - name: Setup PHP
        uses: shivammathur/setup-php@7c0b4c8c8ebed23eca9ec2802474895d105b11bc
        with:
          php-version: ${{ env.PHP_VERSION }}
      - uses: actions/download-artifact@v3
        with:
          name: typo3
      - name: Extract artifact
        run: |
          tar xf typo3.tar.gz
          rm typo3.tar.gz
      - name: Install ssh agent and rsync
        run: |
          apt update
          apt install -y openssh-client rsync
      - name: Switch to release
        uses: deployphp/action@v1
        with:
          deployer-binary: "./bin/dep"
          dep: --file=./build/deploy.php release:switch stage
          private-key: ${{secrets.STAGE_KEY}}

  deploy-production:
    needs: build
    runs-on: ubuntu-latest
    steps:
      - name: Setup PHP
        uses: shivammathur/setup-php@7c0b4c8c8ebed23eca9ec2802474895d105b11bc
        with:
          php-version: ${{ env.PHP_VERSION }}
      - uses: actions/download-artifact@v3
        with:
          name: typo3
      - name: Extract artifact
        run: |
          tar xf typo3.tar.gz
          rm typo3.tar.gz
      - name: Install ssh agent and rsync
        run: |
          apt update
          apt install -y openssh-client rsync
      - name: Deploy
        uses: deployphp/action@v1
        with:
          deployer-binary: "./bin/dep"
          dep: --file=./build/deploy.php release:create production
          private-key: ${{secrets.PROD_KEY}}
