name: Build train

on:
  workflow_dispatch:
  push:
    branches:
      - master

jobs:

  ##########################################################################################
  #                                                                                        #
  #                cancels previous runs associated with a workflow                        #
  #                                                                                        #
  ##########################################################################################

  Cancel:
    if: ${{ github.repository_owner == 'Armbian' }}
    runs-on: small
    steps:
      - uses: n1hility/cancel-previous-runs@v2
        with: 
          token: ${{ secrets.GITHUB_TOKEN }}

  ##########################################################################################
  #                                                                                        #
  #           Merge master into nighly image from which we build packages                  #
  #                                                                                        #
  ##########################################################################################
  
  Merge:
    name: Merging
    needs: Cancel
    if: ${{ github.repository_owner == 'Armbian' }}
    uses: armbian/scripts/.github/workflows/merge-from-branch.yml@master

    with:
      branch: 'nightly'
      runner: small

    secrets:
      GPG_KEY2: ${{ secrets.GPG_KEY2 }}
      GPG_PASSPHRASE2: ${{ secrets.GPG_PASSPHRASE2 }}

  ##########################################################################################
  #                                                                                        #
  #           Check for changes in patches, kernel config and upstream                     #
  #                                                                                        #
  ##########################################################################################

  Check:
    name: Checking
    needs: Merge
    if: ${{ success() && github.repository_owner == 'Armbian' }}
    uses: armbian/scripts/.github/workflows/check-for-changes.yml@master

  ##########################################################################################
  #                                                                                        #
  #                           Build changed kernel packages                                #
  #                                                                                        #
  ##########################################################################################

  Kernel:
    needs: Check
    if: ${{ success() && github.repository_owner == 'Armbian' }}
    uses: armbian/scripts/.github/workflows/build-kernel.yml@master

    with:
      uploading: true
      runner: fast
      reference: nightly

    secrets:
      GPG_KEY1: ${{ secrets.GPG_KEY1 }}
      GPG_PASSPHRASE1: ${{ secrets.GPG_PASSPHRASE1 }}
      GPG_KEY2: ${{ secrets.GPG_KEY2 }}
      GPG_PASSPHRASE2: ${{ secrets.GPG_PASSPHRASE2 }}
      SCRIPTS_ACCESS_TOKEN: ${{ secrets.SCRIPTS_ACCESS_TOKEN }}
      KEY_TORRENTS: ${{ secrets.KEY_TORRENTS }}
      KNOWN_HOSTS_UPLOAD: ${{ secrets.KNOWN_HOSTS_UPLOAD }}

  ##########################################################################################
  #                                                                                        #
  #                           Build changed desktop packages                               #
  #                                                                                        #
  ##########################################################################################

  Desktop:
    needs: Check
    if: ${{ success() && github.repository_owner == 'Armbian' }}
    uses: armbian/scripts/.github/workflows/build-desktop.yml@master

    with:
      uploading: true

    secrets:
      GPG_KEY1: ${{ secrets.GPG_KEY1 }}
      GPG_PASSPHRASE1: ${{ secrets.GPG_PASSPHRASE1 }}
      GPG_KEY2: ${{ secrets.GPG_KEY2 }}
      GPG_PASSPHRASE2: ${{ secrets.GPG_PASSPHRASE2 }}
      SCRIPTS_ACCESS_TOKEN: ${{ secrets.SCRIPTS_ACCESS_TOKEN }}
      KEY_TORRENTS: ${{ secrets.KEY_TORRENTS }}
      KNOWN_HOSTS_UPLOAD: ${{ secrets.KNOWN_HOSTS_UPLOAD }}

  ##########################################################################################
  #                                                                                        #
  #                Build u-boot and board support packages                                 #
  #                                                                                        #
  ##########################################################################################

  legacy:
    needs: Check
    if: ${{ success() && github.repository_owner == 'Armbian' }}
    uses: armbian/scripts/.github/workflows/build-u-boot-with-docker.yml@master

    with:

      runner: "ubuntu-latest"
      include: 'grep legacy | '
      exclude: ''
      uploading: false
      destref: 'nightly'

    secrets:
      GPG_KEY1: ${{ secrets.GPG_KEY1 }}
      GPG_PASSPHRASE1: ${{ secrets.GPG_PASSPHRASE1 }}
      GPG_KEY2: ${{ secrets.GPG_KEY2 }}
      GPG_PASSPHRASE2: ${{ secrets.GPG_PASSPHRASE2 }}
      SCRIPTS_ACCESS_TOKEN: ${{ secrets.SCRIPTS_ACCESS_TOKEN }}
      SSH_KEY_TORRENTS: ${{ secrets.KEY_TORRENTS }}
      KNOWN_HOSTS_UPLOAD: ${{ secrets.KNOWN_HOSTS_UPLOAD }}

  current:
    needs: Check
    if: ${{ success() && github.repository_owner == 'Armbian' }}
    uses: armbian/scripts/.github/workflows/build-u-boot-with-docker.yml@master

    with:

      runner: "small"
      include: 'grep current | '
      exclude: ''
      uploading: false
      destref: 'nightly'

    secrets:
      GPG_KEY1: ${{ secrets.GPG_KEY1 }}
      GPG_PASSPHRASE1: ${{ secrets.GPG_PASSPHRASE1 }}
      GPG_KEY2: ${{ secrets.GPG_KEY2 }}
      GPG_PASSPHRASE2: ${{ secrets.GPG_PASSPHRASE2 }}
      SCRIPTS_ACCESS_TOKEN: ${{ secrets.SCRIPTS_ACCESS_TOKEN }}
      SSH_KEY_TORRENTS: ${{ secrets.KEY_TORRENTS }}
      KNOWN_HOSTS_UPLOAD: ${{ secrets.KNOWN_HOSTS_UPLOAD }}


  edge:
    needs: Check
    if: ${{ success() && github.repository_owner == 'Armbian' }}
    uses: armbian/scripts/.github/workflows/build-u-boot-with-docker.yml@master

    with:

      runner: "small"
      include: 'grep edge | '
      exclude: ''
      uploading: false
      destref: 'nightly'

    secrets:
      GPG_KEY1: ${{ secrets.GPG_KEY1 }}
      GPG_PASSPHRASE1: ${{ secrets.GPG_PASSPHRASE1 }}
      GPG_KEY2: ${{ secrets.GPG_KEY2 }}
      GPG_PASSPHRASE2: ${{ secrets.GPG_PASSPHRASE2 }}
      SCRIPTS_ACCESS_TOKEN: ${{ secrets.SCRIPTS_ACCESS_TOKEN }}
      SSH_KEY_TORRENTS: ${{ secrets.KEY_TORRENTS }}
      KNOWN_HOSTS_UPLOAD: ${{ secrets.KNOWN_HOSTS_UPLOAD }}

  ##########################################################################################
  #                                                                                        #
  #                         Store build hashes for future comparission                     #
  #                                                                                        #
  ##########################################################################################

  Deploy:
    needs: [Kernel,Desktop,legacy,current,edge]
    if: ${{ success() && github.repository_owner == 'Armbian' }}
    uses: armbian/scripts/.github/workflows/deploy.yml@master

    with:

      uploading: true

    secrets:
      GPG_KEY1: ${{ secrets.GPG_KEY1 }}
      GPG_PASSPHRASE1: ${{ secrets.GPG_PASSPHRASE1 }}
      GPG_KEY2: ${{ secrets.GPG_KEY2 }}
      GPG_PASSPHRASE2: ${{ secrets.GPG_PASSPHRASE2 }}
      SCRIPTS_ACCESS_TOKEN: ${{ secrets.SCRIPTS_ACCESS_TOKEN }}
      KEY_TORRENTS: ${{ secrets.KEY_TORRENTS }}
      KNOWN_HOSTS_UPLOAD: ${{ secrets.KNOWN_HOSTS_UPLOAD }}

  apt-armbian-com:
    name: "Stable repository"
    needs: [Deploy]
    if: ${{ success() && github.repository_owner == 'Armbian' }}
    uses: armbian/scripts/.github/workflows/update-repository.yml@master

    with:
      KEY_ID: 'repository'

    secrets:  
      KEY_REPOSITORY: ${{ secrets.KEY_REPOSITORY }}
      USER_REPOSITORY: ${{ secrets.USER_REPOSITORY }}
      HOST_REPOSITORY: ${{ secrets.HOST_REPOSITORY }}
      KNOWN_HOSTS_REPOSITORY: ${{ secrets.KNOWN_HOSTS_REPOSITORY }}

  beta-armbian-com:
    name: "Nighly repository"
    needs: [Deploy]
    if: ${{ success() && github.repository_owner == 'Armbian' }}
    uses: armbian/scripts/.github/workflows/update-repository.yml@master

    with:
      KEY_ID: 'repository-beta'

    secrets:  
      KEY_REPOSITORY: ${{ secrets.KEY_REPOSITORY_BETA }}
      USER_REPOSITORY: ${{ secrets.USER_REPOSITORY }}
      HOST_REPOSITORY: ${{ secrets.HOST_REPOSITORY }}
      KNOWN_HOSTS_REPOSITORY: ${{ secrets.KNOWN_HOSTS_REPOSITORY }}

  ##########################################################################################
  #                                                                                        #
  #                                    Build x86 CLI images                                #
  #                                                                                        #
  ##########################################################################################

  x86-cli-images:
    needs: [apt-armbian-com,beta-armbian-com]
    uses: armbian/scripts/.github/workflows/build-with-docker.yml@master

    with:
 
      variant: 'cli:beta'
      runner: "ubuntu-latest"
      sourcerepo: 'nightly'
      part: 1
      of: 1
      include: 'grep uefi-x86 | '
      exclude: ''
      uploading: false

    secrets:
      GPG_KEY1: ${{ secrets.GPG_KEY1 }}
      GPG_PASSPHRASE1: ${{ secrets.GPG_PASSPHRASE1 }}
      GPG_KEY2: ${{ secrets.GPG_KEY2 }}
      GPG_PASSPHRASE2: ${{ secrets.GPG_PASSPHRASE2 }}
      SCRIPTS_ACCESS_TOKEN: ${{ secrets.SCRIPTS_ACCESS_TOKEN }}
      SSH_KEY_TORRENTS: ${{ secrets.KEY_TORRENTS }}
      KNOWN_HOSTS_UPLOAD: ${{ secrets.KNOWN_HOSTS_UPLOAD }}

  ##########################################################################################
  #                                                                                        #
  #                                    Build x86 desktop images                            #
  #                                                                                        #
  ##########################################################################################

  x86-desktop-images:
    needs: [apt-armbian-com,beta-armbian-com]
    uses: armbian/scripts/.github/workflows/build-with-docker.yml@master

    with:

      variant: 'desktop:beta'
      runner: "ubuntu-latest"
      sourcerepo: 'nightly'
      part: 1
      of: 1
      include: 'grep uefi-x86 | '
      exclude: ''
      uploading: false

    secrets:
      GPG_KEY1: ${{ secrets.GPG_KEY1 }}
      GPG_PASSPHRASE1: ${{ secrets.GPG_PASSPHRASE1 }}
      GPG_KEY2: ${{ secrets.GPG_KEY2 }}
      GPG_PASSPHRASE2: ${{ secrets.GPG_PASSPHRASE2 }}
      SCRIPTS_ACCESS_TOKEN: ${{ secrets.SCRIPTS_ACCESS_TOKEN }}
      SSH_KEY_TORRENTS: ${{ secrets.KEY_TORRENTS }}
      KNOWN_HOSTS_UPLOAD: ${{ secrets.KNOWN_HOSTS_UPLOAD }}

  ##########################################################################################
  #                                                                                        #
  #                                    Build CLI images                                    #
  #                                                                                        #
  ##########################################################################################

  cli-images:
    needs: [apt-armbian-com,beta-armbian-com]
    uses: armbian/scripts/.github/workflows/build-with-docker.yml@master

    with:
 
      variant: 'cli:beta'
      runner: "ubuntu-latest"
      sourcerepo: 'nightly'
      part: 1
      of: 1
      include: ''
      exclude: 'grep -v uefi-x86 | '
      uploading: false

    secrets:
      GPG_KEY1: ${{ secrets.GPG_KEY1 }}
      GPG_PASSPHRASE1: ${{ secrets.GPG_PASSPHRASE1 }}
      GPG_KEY2: ${{ secrets.GPG_KEY2 }}
      GPG_PASSPHRASE2: ${{ secrets.GPG_PASSPHRASE2 }}
      SCRIPTS_ACCESS_TOKEN: ${{ secrets.SCRIPTS_ACCESS_TOKEN }}
      SSH_KEY_TORRENTS: ${{ secrets.KEY_TORRENTS }}
      KNOWN_HOSTS_UPLOAD: ${{ secrets.KNOWN_HOSTS_UPLOAD }}

  ##########################################################################################
  #                                                                                        #
  #                                    Build desktop images                                #
  #                                                                                        #
  ##########################################################################################

  desktop-images:
    needs: [apt-armbian-com,beta-armbian-com]
    uses: armbian/scripts/.github/workflows/build-with-docker.yml@master

    with:
      variant: 'desktop:beta'
      runner: "big"
      sourcerepo: 'nightly'
      part: 1
      of: 1      
      include: ''
      exclude: 'grep -v uefi-x86 | '
      uploading: false

    secrets:
      GPG_KEY1: ${{ secrets.GPG_KEY1 }}
      GPG_PASSPHRASE1: ${{ secrets.GPG_PASSPHRASE1 }}
      GPG_KEY2: ${{ secrets.GPG_KEY2 }}
      GPG_PASSPHRASE2: ${{ secrets.GPG_PASSPHRASE2 }}
      SCRIPTS_ACCESS_TOKEN: ${{ secrets.SCRIPTS_ACCESS_TOKEN }}
      SSH_KEY_TORRENTS: ${{ secrets.KEY_TORRENTS }}
      KNOWN_HOSTS_UPLOAD: ${{ secrets.KNOWN_HOSTS_UPLOAD }}

  ##########################################################################################
  #                                                                                        #
  #                         Bump with version if compilation succeeded                     #
  #                                                                                        #
  ##########################################################################################

  Bump:
    needs: [x86-cli-images,x86-desktop-images,cli-images,desktop-images]
    if: ${{ success() && github.repository_owner == 'Armbian' }}
    uses: armbian/scripts/.github/workflows/update-version.yml@master

    with:

      uploading: true

    secrets:
      GPG_KEY1: ${{ secrets.GPG_KEY1 }}
      GPG_PASSPHRASE1: ${{ secrets.GPG_PASSPHRASE1 }}
      GPG_KEY2: ${{ secrets.GPG_KEY2 }}
      GPG_PASSPHRASE2: ${{ secrets.GPG_PASSPHRASE2 }}
      SCRIPTS_ACCESS_TOKEN: ${{ secrets.SCRIPTS_ACCESS_TOKEN }}
      KEY_TORRENTS: ${{ secrets.KEY_TORRENTS }}
      KNOWN_HOSTS_UPLOAD: ${{ secrets.KNOWN_HOSTS_UPLOAD }}

  ##########################################################################################
  #                                                                                        #
  #                        Update download links and create torrents                       #
  #                                                                                        #
  ##########################################################################################
  
  Torrents:
    name: Torrents
    needs: [x86-cli-images,x86-desktop-images,cli-images,desktop-images]
    if: ${{ github.repository_owner == 'Armbian' }}
    uses: armbian/scripts/.github/workflows/update-download.yml@master

    secrets:
      KEY_TORRENTS: ${{ secrets.KEY_TORRENTS }}
      USER_TORRENTS: ${{ secrets.USER_TORRENTS }}
      HOST_TORRENTS: ${{ secrets.HOST_TORRENTS }}
      KNOWN_HOSTS_TORRENTS: ${{ secrets.KNOWN_HOSTS_TORRENTS }}

  ##########################################################################################
  #                                                                                        #
  #                               Clean runners                                            #
  #                                                                                        #
  ##########################################################################################

  Maintaining:

    needs: [Bump,Torrents]
    if: ${{ success() && github.repository_owner == 'Armbian' }}
    uses: armbian/scripts/.github/workflows/maintain-runners.yml@master

    with:

      checking: true

    secrets:
      KEY_TORRENTS: ${{ secrets.KEY_TORRENTS }}
      KNOWN_HOSTS_UPLOAD: ${{ secrets.KNOWN_HOSTS_UPLOAD }}
