name: Build and Deploy
on:
    # Nightly build
    schedule:
        - cron: "0 9 * * *"
    # Manual nightly & release
    workflow_dispatch:
        inputs:
            mode:
                description: What type of build to trigger. Release builds MUST be ran from the `master` branch.
                required: true
                default: nightly
                type: choice
                options:
                    - nightly
                    - release
            macos:
                description: Build macOS
                required: true
                type: boolean
                default: true
            windows_32bit:
                description: Build Windows 32-bit
                required: true
                type: boolean
                default: true
            windows_64bit:
                description: Build Windows 64-bit
                required: true
                type: boolean
                default: true
            linux:
                description: Build Linux
                required: true
                type: boolean
                default: true
            deploy:
                description: Deploy artifacts
                required: true
                type: boolean
                default: true
concurrency: ${{ github.workflow }}
env:
    R2_BUCKET: ${{ vars.R2_BUCKET }}
jobs:
    prepare:
        uses: ./.github/workflows/build_prepare.yaml
        with:
            config: element.io/${{ inputs.mode || 'nightly' }}
            version: ${{ inputs.mode == 'release' && '' || 'develop' }}
            nightly: ${{ inputs.mode != 'release' }}
        secrets:
            CF_R2_ACCESS_KEY_ID: ${{ secrets.CF_R2_ACCESS_KEY_ID }}
            CF_R2_TOKEN: ${{ secrets.CF_R2_TOKEN }}

    windows_32bit:
        if: github.event_name != 'workflow_dispatch' || inputs.windows_32bit
        needs: prepare
        name: Windows 32-bit
        uses: ./.github/workflows/build_windows.yaml
        secrets: inherit
        with:
            sign: true
            deploy-mode: true
            arch: x86
            version: ${{ needs.prepare.outputs.win32-x86-version }}

    windows_64bit:
        if: github.event_name != 'workflow_dispatch' || inputs.windows_64bit
        needs: prepare
        name: Windows 64-bit
        uses: ./.github/workflows/build_windows.yaml
        secrets: inherit
        with:
            sign: true
            deploy-mode: true
            arch: x64
            version: ${{ needs.prepare.outputs.win32-x64-version }}

    macos:
        if: github.event_name != 'workflow_dispatch' || inputs.macos
        needs: prepare
        name: macOS
        uses: ./.github/workflows/build_macos.yaml
        secrets: inherit
        with:
            sign: true
            deploy-mode: true
            base-url: https://packages.element.io/${{ inputs.mode == 'release' && 'desktop' || 'nightly' }}
            version: ${{ needs.prepare.outputs.macos-version }}

    # We do not put these calls into deploy-mode as we do not want it to add to the packages.element.io artifact
    # We ship this build via reprepro only
    linux:
        if: github.event_name != 'workflow_dispatch' || inputs.linux
        needs: prepare
        name: Linux ${{ matrix.arch }} (sqlcipher system)
        strategy:
            matrix:
                arch: [amd64, arm64]
        uses: ./.github/workflows/build_linux.yaml
        with:
            arch: ${{ matrix.arch }}
            config: element.io/${{ inputs.mode || 'nightly' }}
            sqlcipher: system
            version: ${{ needs.prepare.outputs.linux-version }}

    # We ship the static build via static tarball only
    linux_static:
        if: github.event_name != 'workflow_dispatch' || inputs.linux
        needs: prepare
        name: Linux (sqlcipher static)
        uses: ./.github/workflows/build_linux.yaml
        with:
            arch: amd64
            deploy-mode: true
            config: element.io/${{ inputs.mode || 'nightly' }}
            sqlcipher: static
            version: ${{ needs.prepare.outputs.linux-version }}

    # This deploy job only handles Windows, macOS & linux_static as those are stateless and static.
    # Linux will be deployed via reprepro after it, but we list it as a dependency to abort if it fails.
    deploy:
        needs:
            - macos
            - linux
            - linux_static
            - windows_32bit
            - windows_64bit
        runs-on: ubuntu-latest
        name: Deploy
        if: github.event_name != 'workflow_dispatch' || (inputs.deploy && (inputs.macos || inputs.windows_32bit || inputs.windows_64bit))
        environment: packages.element.io
        steps:
            - name: Download artifacts
              uses: actions/download-artifact@v3
              with:
                  name: packages.element.io
                  path: packages.element.io

            - name: Deploy artifacts
              run: |
                  aws s3 cp --recursive packages.element.io/ s3://$R2_BUCKET/$DEPLOYMENT_DIR --endpoint-url $R2_URL --region auto
              env:
                  AWS_ACCESS_KEY_ID: ${{ secrets.CF_R2_ACCESS_KEY_ID }}
                  AWS_SECRET_ACCESS_KEY: ${{ secrets.CF_R2_TOKEN }}
                  R2_URL: ${{ vars.CF_R2_S3_API }}
                  DEPLOYMENT_DIR: ${{ inputs.mode == 'release' && 'desktop' || 'nightly' }}

            - name: Notify packages.element.io of new files
              uses: peter-evans/repository-dispatch@26b39ed245ab8f31526069329e112ab2fb224588 # v2
              with:
                  token: ${{ secrets.ELEMENT_BOT_TOKEN }}
                  repository: vector-im/packages.element.io
                  event-type: packages-index

    reprepro:
        needs:
            - linux
            # We queue this after the other deploy stage as we want to abort if that fails
            - deploy
        name: Run reprepro ${{ matrix.arch }}
        strategy:
            matrix:
                arch: [amd64, arm64]
        if: github.event_name != 'workflow_dispatch' || (inputs.deploy && inputs.linux)
        uses: ./.github/workflows/reprepro.yaml
        secrets: inherit
        with:
            artifact-name: linux-${{ matrix.arch }}-sqlcipher-system
