name: 📦🚀 Build Installer - Linux DEB AMD64

on:
  workflow_dispatch:
  push:
    paths-ignore:
    - '**.md'
    branches:
      - 'long_lived/**'
      - main
      - 'release/**'
  release:
    types: [published]
  pull_request:
    paths-ignore:
    - '**.md'
    branches:
      - '**'

concurrency:
  # SHA is added to the end if on `main` to let all main workflows run
  group: ${{ github.ref }}-${{ github.workflow }}-${{ github.event_name }}-${{ (github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/heads/release/') || startsWith(github.ref, 'refs/heads/long_lived/')) && github.sha || '' }}
  cancel-in-progress: true

jobs:
  build:
    name: Linux amd64 DEB Installer
    runs-on: ubuntu-latest
    container: chianetwork/ubuntu-18.04-builder:latest
    timeout-minutes: 120
    strategy:
      fail-fast: false
      max-parallel: 4
      matrix:
        python-version: [3.8]

    steps:
    - uses: Chia-Network/actions/clean-workspace@main

    - name: Add safe git directory
      uses: Chia-Network/actions/git-mark-workspace-safe@main

    - name: Checkout Code
      uses: actions/checkout@v3
      with:
        fetch-depth: 0
        submodules: recursive

    - uses: Chia-Network/actions/git-ssh-to-https@main

    - name: Cleanup any leftovers that exist from previous runs
      run: bash build_scripts/clean-runner.sh || true

    - name: Set Env
      uses: Chia-Network/actions/setjobenv@main
      env:
        GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

    # Create our own venv outside of the git directory JUST for getting the ACTUAL version so that install can't break it
    - name: Get version number
      id: version_number
      run: |
        python3 -m venv ../venv
        . ../venv/bin/activate
        pip3 install setuptools_scm
        echo "CHIA_INSTALLER_VERSION=$(python3 ./build_scripts/installer-version.py)" >>$GITHUB_OUTPUT
        deactivate

    - name: Test for secrets access
      id: check_secrets
      shell: bash
      run: |
        unset HAS_AWS_SECRET
        unset HAS_GLUE_SECRET

        if [ -n "$AWS_SECRET" ]; then HAS_AWS_SECRET='true' ; fi
        echo HAS_AWS_SECRET=${HAS_AWS_SECRET} >>$GITHUB_OUTPUT

        if [ -n "$GLUE_ACCESS_TOKEN" ]; then HAS_GLUE_SECRET='true' ; fi
        echo HAS_GLUE_SECRET=${HAS_GLUE_SECRET} >>$GITHUB_OUTPUT
      env:
        AWS_SECRET: "${{ secrets.INSTALLER_UPLOAD_KEY }}"
        GLUE_ACCESS_TOKEN: "${{ secrets.GLUE_ACCESS_TOKEN }}"

    - name: Get latest madmax plotter
      env:
        GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      run: |
        LATEST_MADMAX=$(gh api repos/Chia-Network/chia-plotter-madmax/releases/latest --jq 'select(.prerelease == false) | .tag_name')
        mkdir "$GITHUB_WORKSPACE/madmax"
        gh release download -R Chia-Network/chia-plotter-madmax $LATEST_MADMAX -p 'chia_plot-*-x86-64' -O $GITHUB_WORKSPACE/madmax/chia_plot
        gh release download -R Chia-Network/chia-plotter-madmax $LATEST_MADMAX -p 'chia_plot_k34-*-x86-64' -O $GITHUB_WORKSPACE/madmax/chia_plot_k34
        chmod +x "$GITHUB_WORKSPACE/madmax/chia_plot"
        chmod +x "$GITHUB_WORKSPACE/madmax/chia_plot_k34"

    - name: Get latest bladebit plotter
      if: '!github.event.release.prerelease'
      env:
        GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      run: |
        LATEST_BLADEBIT=$(gh api repos/Chia-Network/bladebit/releases/latest --jq 'select(.prerelease == false) | .tag_name')
        mkdir "$GITHUB_WORKSPACE/bladebit"
        gh release download -R Chia-Network/bladebit $LATEST_BLADEBIT -p '*-ubuntu-x86-64.tar.gz' -O - | tar -xz -C $GITHUB_WORKSPACE/bladebit
        chmod +x "$GITHUB_WORKSPACE/bladebit/bladebit"

    - name: Get latest prerelease bladebit plotter
      if: env.PRE_RELEASE == 'true'
      env:
        GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      run: |
        LATEST_PRERELEASE=$(gh api repos/Chia-Network/bladebit/releases --jq 'map(select(.prerelease)) | first | .tag_name')
        mkdir "$GITHUB_WORKSPACE/bladebit"
        gh release download -R Chia-Network/bladebit $LATEST_PRERELEASE -p '*ubuntu-x86-64.tar.gz' -O - | tar -xz -C $GITHUB_WORKSPACE/bladebit
        chmod +x "$GITHUB_WORKSPACE/bladebit/bladebit"

    - uses: ./.github/actions/install
      with:
        python-version: ${{ matrix.python-version }}
        development: true

    - uses: chia-network/actions/activate-venv@main

    - name: Prepare GUI cache
      id: gui-ref
      run: |
        gui_ref=$(git submodule status chia-blockchain-gui | sed -e 's/^ //g' -e 's/ chia-blockchain-gui.*$//g')
        echo "${gui_ref}"
        echo "GUI_REF=${gui_ref}" >>$GITHUB_OUTPUT
        echo "rm -rf ./chia-blockchain-gui"
        rm -rf ./chia-blockchain-gui

    - name: Cache GUI
      uses: actions/cache@v3
      id: cache-gui
      with:
        path: ./chia-blockchain-gui
        key: ${{ runner.os }}-deb-chia-blockchain-gui-${{ steps.gui-ref.outputs.GUI_REF }}

    - if: steps.cache-gui.outputs.cache-hit != 'true'
      name: Build GUI
      continue-on-error: false
      run: |
        cd ./build_scripts
        bash build_linux_deb-1-gui.sh

    - name: Build .deb package
      env:
        CHIA_INSTALLER_VERSION: ${{ steps.version_number.outputs.CHIA_INSTALLER_VERSION }}
      run: |
        ldd --version
        cd ./build_scripts
        sh build_linux_deb-2-installer.sh amd64

    - name: Upload Linux artifacts
      uses: actions/upload-artifact@v3
      with:
        name: chia-installers-linux-deb-intel
        path: ${{ github.workspace }}/build_scripts/final_installer/

    - name: Configure AWS Credentials
      if: steps.check_secrets.outputs.HAS_AWS_SECRET
      uses: aws-actions/configure-aws-credentials@v2
      with:
        aws-access-key-id: ${{ secrets.INSTALLER_UPLOAD_KEY }}
        aws-secret-access-key: ${{ secrets.INSTALLER_UPLOAD_SECRET }}
        aws-region: us-west-2

    - name: Upload to s3
      if: steps.check_secrets.outputs.HAS_AWS_SECRET
      env:
        CHIA_INSTALLER_VERSION: ${{ steps.version_number.outputs.CHIA_INSTALLER_VERSION }}
      run: |
        GIT_SHORT_HASH=$(echo "${GITHUB_SHA}" | cut -c1-8)
        CHIA_DEV_BUILD=${CHIA_INSTALLER_VERSION}-$GIT_SHORT_HASH
        echo "CHIA_DEV_BUILD=$CHIA_DEV_BUILD" >>$GITHUB_ENV
        aws s3 cp "$GITHUB_WORKSPACE/build_scripts/final_installer/chia-blockchain_${CHIA_INSTALLER_VERSION}_amd64.deb" "s3://download.chia.net/dev/chia-blockchain_${CHIA_DEV_BUILD}_amd64.deb"
        aws s3 cp "$GITHUB_WORKSPACE/build_scripts/final_installer/chia-blockchain-cli_${CHIA_INSTALLER_VERSION}-1_amd64.deb" "s3://download.chia.net/dev/chia-blockchain-cli_${CHIA_DEV_BUILD}-1_amd64.deb"

    - name: Create Checksums
      if: env.FULL_RELEASE == 'true' || github.ref == 'refs/heads/main'
      env:
        CHIA_INSTALLER_VERSION: ${{ steps.version_number.outputs.CHIA_INSTALLER_VERSION }}
      run: |
        ls $GITHUB_WORKSPACE/build_scripts/final_installer/
        sha256sum $GITHUB_WORKSPACE/build_scripts/final_installer/chia-blockchain_${CHIA_INSTALLER_VERSION}_amd64.deb > $GITHUB_WORKSPACE/build_scripts/final_installer/chia-blockchain_${CHIA_INSTALLER_VERSION}_amd64.deb.sha256
        sha256sum $GITHUB_WORKSPACE/build_scripts/final_installer/chia-blockchain-cli_${CHIA_INSTALLER_VERSION}-1_amd64.deb > $GITHUB_WORKSPACE/build_scripts/final_installer/chia-blockchain-cli_${CHIA_INSTALLER_VERSION}-1_amd64.deb.sha256
        ls $GITHUB_WORKSPACE/build_scripts/final_installer/

    - name: Create .deb torrent
      if: env.FULL_RELEASE == 'true'
      env:
        CHIA_INSTALLER_VERSION: ${{ steps.version_number.outputs.CHIA_INSTALLER_VERSION }}
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      run: |
        py3createtorrent -f -t udp://tracker.opentrackr.org:1337/announce $GITHUB_WORKSPACE/build_scripts/final_installer/chia-blockchain_${CHIA_INSTALLER_VERSION}_amd64.deb -o $GITHUB_WORKSPACE/build_scripts/final_installer/chia-blockchain_${CHIA_INSTALLER_VERSION}_amd64.deb.torrent --webseed https://download.chia.net/install/chia-blockchain_${CHIA_INSTALLER_VERSION}_amd64.deb
        py3createtorrent -f -t udp://tracker.opentrackr.org:1337/announce $GITHUB_WORKSPACE/build_scripts/final_installer/chia-blockchain-cli_${CHIA_INSTALLER_VERSION}-1_amd64.deb -o $GITHUB_WORKSPACE/build_scripts/final_installer/chia-blockchain-cli_${CHIA_INSTALLER_VERSION}-1_amd64.deb.torrent --webseed https://download.chia.net/install/chia-blockchain-cli_${CHIA_INSTALLER_VERSION}-1_amd64.deb
        gh release upload $RELEASE_TAG $GITHUB_WORKSPACE/build_scripts/final_installer/chia-blockchain_${CHIA_INSTALLER_VERSION}_amd64.deb.torrent

    - name: Upload Dev Installer
      if: steps.check_secrets.outputs.HAS_AWS_SECRET && github.ref == 'refs/heads/main'
      env:
        CHIA_INSTALLER_VERSION: ${{ steps.version_number.outputs.CHIA_INSTALLER_VERSION }}
      run: |
        aws s3 cp $GITHUB_WORKSPACE/build_scripts/final_installer/chia-blockchain_${CHIA_INSTALLER_VERSION}_amd64.deb s3://download.chia.net/latest-dev/chia-blockchain_amd64_latest_dev.deb
        aws s3 cp $GITHUB_WORKSPACE/build_scripts/final_installer/chia-blockchain_${CHIA_INSTALLER_VERSION}_amd64.deb.sha256 s3://download.chia.net/latest-dev/chia-blockchain_amd64_latest_dev.deb.sha256
        aws s3 cp $GITHUB_WORKSPACE/build_scripts/final_installer/chia-blockchain-cli_${CHIA_INSTALLER_VERSION}-1_amd64.deb s3://download.chia.net/latest-dev/chia-blockchain-cli_amd64_latest_dev.deb
        aws s3 cp $GITHUB_WORKSPACE/build_scripts/final_installer/chia-blockchain-cli_${CHIA_INSTALLER_VERSION}-1_amd64.deb.sha256 s3://download.chia.net/latest-dev/chia-blockchain-cli_amd64_latest_dev.deb.sha256

    - name: Upload Release Files
      if: steps.check_secrets.outputs.HAS_AWS_SECRET && env.FULL_RELEASE == 'true'
      env:
        CHIA_INSTALLER_VERSION: ${{ steps.version_number.outputs.CHIA_INSTALLER_VERSION }}
      run: |
        aws s3 cp $GITHUB_WORKSPACE/build_scripts/final_installer/chia-blockchain_${CHIA_INSTALLER_VERSION}_amd64.deb s3://download.chia.net/install/
        aws s3 cp $GITHUB_WORKSPACE/build_scripts/final_installer/chia-blockchain_${CHIA_INSTALLER_VERSION}_amd64.deb.sha256 s3://download.chia.net/install/
        aws s3 cp $GITHUB_WORKSPACE/build_scripts/final_installer/chia-blockchain_${CHIA_INSTALLER_VERSION}_amd64.deb.torrent s3://download.chia.net/torrents/
        aws s3 cp $GITHUB_WORKSPACE/build_scripts/final_installer/chia-blockchain-cli_${CHIA_INSTALLER_VERSION}-1_amd64.deb s3://download.chia.net/install/
        aws s3 cp $GITHUB_WORKSPACE/build_scripts/final_installer/chia-blockchain-cli_${CHIA_INSTALLER_VERSION}-1_amd64.deb.sha256 s3://download.chia.net/install/
        aws s3 cp $GITHUB_WORKSPACE/build_scripts/final_installer/chia-blockchain-cli_${CHIA_INSTALLER_VERSION}-1_amd64.deb.torrent s3://download.chia.net/torrents/

    - name: Get tag name
      if: startsWith(github.ref, 'refs/tags/')
      id: tag-name
      run: |
        echo "TAG_NAME=$(echo ${{ github.ref }} | cut -d'/' -f 3)" >>$GITHUB_OUTPUT
        echo "REPO_NAME=$(echo ${{ github.repository }} | cut -d'/' -f 2)" >>$GITHUB_OUTPUT

    - name: Upload release artifacts
      if: env.RELEASE == 'true'
      env:
        GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        CHIA_INSTALLER_VERSION: ${{ steps.version_number.outputs.CHIA_INSTALLER_VERSION }}
      run: |
        gh release upload \
          $RELEASE_TAG \
          build_scripts/final_installer/chia-blockchain_${CHIA_INSTALLER_VERSION}_amd64.deb \
          build_scripts/final_installer/chia-blockchain-cli_${CHIA_INSTALLER_VERSION}-1_amd64.deb

    - name: Mark installer complete
      if: steps.check_secrets.outputs.HAS_GLUE_SECRET && env.FULL_RELEASE == 'true'
      run: |
        curl -s -XPOST -H "Authorization: Bearer ${{ secrets.GLUE_ACCESS_TOKEN }}" --data '{"chia_ref": "${{ steps.tag-name.outputs.TAG_NAME }}"}' ${{ secrets.GLUE_API_URL }}/api/v1/${{ steps.tag-name.outputs.REPO_NAME }}/${{ steps.tag-name.outputs.TAG_NAME }}/success/build-linux-deb

    - name: Remove working files to exclude from cache
      run: |
        rm -rf ./chia-blockchain-gui/packages/gui/daemon
