name: Package

permissions:
  # for actions/create-release, actions/upload-release-asset
  contents: write

concurrency:
  group: package-${{ github.ref }}
  cancel-in-progress: true

on:
  push:
    branches:
      - "pkg/*"

env:
  CARGO_TERM_COLOR: always
  RUST_BACKTRACE: full

jobs:
  get-ckb-cli-version:
    name: Get CKB CLI Version
    runs-on: ubuntu-22.04
    outputs:
      version: ${{ steps.get-version.outputs.version }}
    steps:
      - name: Get latest ckb-cli version
        id: get-version
        run: |
          CKB_CLI_VERSION=$(curl -s https://api.github.com/repos/nervosnetwork/ckb-cli/releases/latest | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
          echo "version=$CKB_CLI_VERSION" >> $GITHUB_OUTPUT

  create-release:
    name: Create release
    runs-on: ubuntu-22.04
    outputs:
      upload_url: ${{ steps.create-release.outputs.upload_url }}
    steps:
      - uses: actions/checkout@v4
      - name: Set tag
        run: |
          export GIT_TAG_NAME=` echo ${{ github.ref }} | awk -F '/' '{print $4}' `
          echo "GIT_TAG_NAME=$GIT_TAG_NAME" >> $GITHUB_ENV
      - name: Create release
        id: create-release
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ env.GIT_TAG_NAME}}
          release_name: ${{ env.GIT_TAG_NAME}}
          draft: true
          prerelease: true

  package-for-linux:
    name: package-for-linux
    runs-on: ubuntu-22.04
    needs:
      - get-ckb-cli-version
    strategy:
      matrix:
        include:
          - rel_pkg: "x86_64-unknown-linux-gnu.tar.gz"
            build_target: "prod"
          - rel_pkg: "x86_64-unknown-linux-gnu-portable.tar.gz"
            build_target: "prod_portable"
    steps:
      - uses: actions/checkout@v4
      - name: Set Env
        run: |
          export GIT_TAG_NAME=` echo ${{ github.ref }} | awk -F '/' '{print $4}' `
          echo "GIT_TAG_NAME=$GIT_TAG_NAME" >> $GITHUB_ENV
      - name: Build CKB and Package CKB
        env:
          LARGE_SECRET_PASSPHRASE: ${{ secrets.LARGE_SECRET_PASSPHRASE }}
          QINIU_ACCESS_KEY: ${{ secrets.QINIU_ACCESS_KEY }}
          QINIU_SECRET_KEY: ${{ secrets.QINIU_SECRET_KEY }}
          GPG_SIGNER: ${{ secrets.GPG_SIGNER }}
        run: |
          export GIT_TAG_NAME=` echo ${{ github.ref }} | awk -F '/' '{print $4}' `
          docker run --rm -i -w /ckb -v $(pwd):/ckb $BUILDER_IMAGE make ${{ matrix.build_target }}
          gpg --quiet --batch --yes --decrypt --passphrase="$LARGE_SECRET_PASSPHRASE" --output devtools/ci/signer.asc devtools/ci/signer.asc.gpg
          gpg --import devtools/ci/signer.asc
          devtools/ci/package.sh target/prod/ckb
          mv ${{ github.workspace }}/releases/ckb_${{env.GIT_TAG_NAME }}_${{env.REL_PKG }} ${{ github.workspace }}
          mv ${{ github.workspace }}/releases/ckb_${{env.GIT_TAG_NAME }}_${{env.REL_PKG }}.asc ${{ github.workspace }}
      - name: upload-zip-file
        uses: actions/upload-artifact@v4
        with:
          name: ckb_${{env.GIT_TAG_NAME }}_${{env.REL_PKG }}
          path: ckb_${{env.GIT_TAG_NAME }}_${{env.REL_PKG }}
      - name: upload-asc-file
        uses: actions/upload-artifact@v4
        with:
          name: ckb_${{env.GIT_TAG_NAME }}_${{env.REL_PKG }}.asc
          path: ckb_${{env.GIT_TAG_NAME }}_${{env.REL_PKG }}.asc
    env:
      BUILDER_IMAGE: nervos/ckb-docker-builder:bionic-rust-1.85.0
      REL_PKG: ${{ matrix.rel_pkg }}
      CKB_CLI_VERSION: ${{ needs.get-ckb-cli-version.outputs.version }}

  package-for-linux-aarch64:
    name: package-for-linux-aarch64
    runs-on: ubuntu-22.04
    needs:
      - get-ckb-cli-version
    steps:
      - uses: actions/checkout@v4
      - name: Set Env
        run: |
          export GIT_TAG_NAME=` echo ${{ github.ref }} | awk -F '/' '{print $4}' `
          echo "GIT_TAG_NAME=$GIT_TAG_NAME" >> $GITHUB_ENV
      - name: Add rust target
        run: rustup target add aarch64-unknown-linux-gnu
      - name: Install dependencies
        run: sudo apt-get update && sudo apt-get install -y gcc-multilib && sudo apt-get install -y build-essential clang gcc-aarch64-linux-gnu g++-aarch64-linux-gnu build-essential
      - name: Build CKB and Package CKB
        env:
          LARGE_SECRET_PASSPHRASE: ${{ secrets.LARGE_SECRET_PASSPHRASE }}
          QINIU_ACCESS_KEY: ${{ secrets.QINIU_ACCESS_KEY }}
          QINIU_SECRET_KEY: ${{ secrets.QINIU_SECRET_KEY }}
          GPG_SIGNER: ${{ secrets.GPG_SIGNER }}
        run: |
          export GIT_TAG_NAME=` echo ${{ github.ref }} | awk -F '/' '{print $4}' `

          PKG_CONFIG_ALLOW_CROSS=1 CC=gcc CC_aarch64_unknown_linux_gnu=aarch64-linux-gnu-gcc CKB_BUILD_TARGET="--target=aarch64-unknown-linux-gnu" make prod_portable

          gpg --quiet --batch --yes --decrypt --passphrase="$LARGE_SECRET_PASSPHRASE" --output devtools/ci/signer.asc devtools/ci/signer.asc.gpg
          gpg --import devtools/ci/signer.asc
          devtools/ci/package.sh target/aarch64-unknown-linux-gnu/prod/ckb
          mv ${{ github.workspace }}/releases/ckb_${{env.GIT_TAG_NAME }}_${{env.REL_PKG }} ${{ github.workspace }}
          mv ${{ github.workspace }}/releases/ckb_${{env.GIT_TAG_NAME }}_${{env.REL_PKG }}.asc ${{ github.workspace }}
      - name: upload-zip-file
        uses: actions/upload-artifact@v4
        with:
          name: ckb_${{env.GIT_TAG_NAME }}_${{env.REL_PKG }}
          path: ckb_${{env.GIT_TAG_NAME }}_${{env.REL_PKG }}
      - name: upload-asc-file
        uses: actions/upload-artifact@v4
        with:
          name: ckb_${{env.GIT_TAG_NAME }}_${{env.REL_PKG }}.asc
          path: ckb_${{env.GIT_TAG_NAME }}_${{env.REL_PKG }}.asc
    env:
      REL_PKG: aarch64-unknown-linux-gnu.tar.gz
      CKB_CLI_VERSION: ${{ needs.get-ckb-cli-version.outputs.version }}

  package-for-centos:
    name: package-for-centos
    runs-on: ubuntu-22.04
    needs:
      - get-ckb-cli-version
    strategy:
      matrix:
        include:
          - rel_pkg: "x86_64-unknown-centos-gnu.tar.gz"
            build_target: "prod"
          - rel_pkg: "x86_64-unknown-centos-gnu-portable.tar.gz"
            build_target: "prod_portable"
    steps:
      - uses: actions/checkout@v4
      - name: Set Env
        run: |
          export GIT_TAG_NAME=` echo ${{ github.ref }} | awk -F '/' '{print $4}' `
          echo "GIT_TAG_NAME=$GIT_TAG_NAME" >> $GITHUB_ENV
      - name: Build CKB and Package CKB
        env:
          LARGE_SECRET_PASSPHRASE: ${{ secrets.LARGE_SECRET_PASSPHRASE }}
          GPG_SIGNER: ${{ secrets.GPG_SIGNER }}
        run: |
          export GIT_TAG_NAME=` echo ${{ github.ref }} | awk -F '/' '{print $4}' `
          docker run --rm -i -w /ckb -v $(pwd):/ckb $BUILDER_IMAGE make ${{ matrix.build_target }}
          gpg --quiet --batch --yes --decrypt --passphrase="$LARGE_SECRET_PASSPHRASE" --output devtools/ci/signer.asc devtools/ci/signer.asc.gpg
          gpg --import devtools/ci/signer.asc
          devtools/ci/package.sh target/prod/ckb
          mv ${{ github.workspace }}/releases/ckb_${{env.GIT_TAG_NAME }}_${{env.REL_PKG }} ${{ github.workspace }}
          mv ${{ github.workspace }}/releases/ckb_${{env.GIT_TAG_NAME }}_${{env.REL_PKG }}.asc ${{ github.workspace }}
      - name: upload-zip-file
        uses: actions/upload-artifact@v4
        with:
          name: ckb_${{env.GIT_TAG_NAME }}_${{env.REL_PKG }}
          path: ckb_${{env.GIT_TAG_NAME }}_${{env.REL_PKG }}
      - name: upload-asc-file
        uses: actions/upload-artifact@v4
        with:
          name: ckb_${{env.GIT_TAG_NAME }}_${{env.REL_PKG }}.asc
          path: ckb_${{env.GIT_TAG_NAME }}_${{env.REL_PKG }}.asc
    env:
      BUILDER_IMAGE: nervos/ckb-docker-builder:centos-7-rust-1.85.0
      REL_PKG: ${{ matrix.rel_pkg }}
      CKB_CLI_VERSION: ${{ needs.get-ckb-cli-version.outputs.version }}

  package-for-mac:
    name: package-for-mac
    runs-on: macos-15
    needs:
      - get-ckb-cli-version
    strategy:
      matrix:
        include:
          - rel_pkg: "x86_64-apple-darwin.zip"
            build_target: "prod"
          - rel_pkg: "x86_64-apple-darwin-portable.zip"
            build_target: "prod_portable"
    steps:
      - uses: actions/checkout@v4
      - name: Set Env
        run: |
          export GIT_TAG_NAME=` echo ${{ github.ref }} | awk -F '/' '{print $4}' `
          echo "GIT_TAG_NAME=$GIT_TAG_NAME" >> $GITHUB_ENV
      - name: Build CKB and Package CKB
        env:
          LARGE_SECRET_PASSPHRASE: ${{ secrets.LARGE_SECRET_PASSPHRASE }}
          GPG_SIGNER: ${{ secrets.GPG_SIGNER }}
        run: |
          export GIT_TAG_NAME=` echo ${{ github.ref }} | awk -F '/' '{print $4}' `

          make ${{ matrix.build_target }}

          gpg --quiet --batch --yes --decrypt --passphrase="$LARGE_SECRET_PASSPHRASE" --output devtools/ci/signer.asc devtools/ci/signer.asc.gpg
          gpg --import devtools/ci/signer.asc
          devtools/ci/package.sh target/prod/ckb
          mv ${{ github.workspace }}/releases/ckb_${{env.GIT_TAG_NAME }}_${{env.REL_PKG }} ${{ github.workspace }}
          mv ${{ github.workspace }}/releases/ckb_${{env.GIT_TAG_NAME }}_${{env.REL_PKG }}.asc ${{ github.workspace }}
      - name: upload-zip-file
        uses: actions/upload-artifact@v4
        with:
          name: ckb_${{env.GIT_TAG_NAME }}_${{env.REL_PKG }}
          path: ckb_${{env.GIT_TAG_NAME }}_${{env.REL_PKG }}
      - name: upload-asc-file
        uses: actions/upload-artifact@v4
        with:
          name: ckb_${{env.GIT_TAG_NAME }}_${{env.REL_PKG }}.asc
          path: ckb_${{env.GIT_TAG_NAME }}_${{env.REL_PKG }}.asc
    env:
      REL_PKG: ${{ matrix.rel_pkg }}
      CKB_CLI_VERSION: ${{ needs.get-ckb-cli-version.outputs.version }}

  package-for-mac-aarch64:
    name: package-for-mac-aarch64
    runs-on: [self-hosted, macOS, ARM64]
    needs:
      - get-ckb-cli-version
    strategy:
      matrix:
        include:
          - rel_pkg: "aarch64-apple-darwin.zip"
            build_target: "prod"
          - rel_pkg: "aarch64-apple-darwin-portable.zip"
            build_target: "prod_portable"
    steps:
      - name: Setup PATH
        run: |
          echo /opt/homebrew/bin >> $GITHUB_PATH
          echo /opt/homebrew/sbin >> $GITHUB_PATH
          echo "$HOME/.cargo/bin" >> $GITHUB_PATH
      - uses: actions/checkout@v4
      - name: Set Env
        run: |
          export GIT_TAG_NAME=` echo ${{ github.ref }} | awk -F '/' '{print $4}' `
          echo "GIT_TAG_NAME=$GIT_TAG_NAME" >> $GITHUB_ENV
      - name: Install Depedencies
        run: |
          if ! type -f gpg &> /dev/null; then
            brew install gnupg
          fi
          if ! [ -f "$HOME/.cargo/bin/rustup" ]; then
            curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
          fi
      - name: Build CKB and Package CKB
        env:
          LARGE_SECRET_PASSPHRASE: ${{ secrets.LARGE_SECRET_PASSPHRASE }}
          GPG_SIGNER: ${{ secrets.GPG_SIGNER }}
        run: |
          export GIT_TAG_NAME=` echo ${{ github.ref }} | awk -F '/' '{print $4}' `

          make ${{ matrix.build_target }}

          gpg --quiet --batch --yes --decrypt --passphrase="$LARGE_SECRET_PASSPHRASE" --output devtools/ci/signer.asc devtools/ci/signer.asc.gpg
          gpg --import devtools/ci/signer.asc
          devtools/ci/package.sh target/prod/ckb
          mv ${{ github.workspace }}/releases/ckb_${{env.GIT_TAG_NAME }}_${{env.REL_PKG }} ${{ github.workspace }}
          mv ${{ github.workspace }}/releases/ckb_${{env.GIT_TAG_NAME }}_${{env.REL_PKG }}.asc ${{ github.workspace }}
      - name: upload-zip-file
        uses: actions/upload-artifact@v4
        with:
          name: ckb_${{env.GIT_TAG_NAME }}_${{env.REL_PKG }}
          path: ckb_${{env.GIT_TAG_NAME }}_${{env.REL_PKG }}
      - name: upload-asc-file
        uses: actions/upload-artifact@v4
        with:
          name: ckb_${{env.GIT_TAG_NAME }}_${{env.REL_PKG }}.asc
          path: ckb_${{env.GIT_TAG_NAME }}_${{env.REL_PKG }}.asc
    env:
      REL_PKG: ${{ matrix.rel_pkg }}
      CKB_CLI_VERSION: ${{ needs.get-ckb-cli-version.outputs.version }}

  package-for-windows:
    name: package-for-windows
    runs-on: windows-2022
    needs:
      - get-ckb-cli-version
    steps:
      - name: Install Dependencies
        run: |
          Set-ExecutionPolicy RemoteSigned -scope CurrentUser
          iwr -useb get.scoop.sh -outfile 'install-scoop.ps1'
          .\install-scoop.ps1 -RunAsAdmin
          scoop install llvm
          echo ("GIT_TAG_NAME=" + $env:GITHUB_REF.replace('refs/heads/pkg/', '')) >> $env:GITHUB_ENV
          echo "$env:USERPROFILE\scoop\shims" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append
          echo "C:\msys64\mingw64\bin" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append
          echo "LIBCLANG_PATH=$env:USERPROFILE\scoop\apps\llvm\current\bin" >> $env:GITHUB_ENV
      - uses: actions/checkout@v4
      - name: Build
        run: |
          devtools/windows/make prod
      - name: Download ckb-cli
        run: |
          iwr -useb "https://github.com/nervosnetwork/ckb-cli/releases/download/$($env:CKB_CLI_VERSION)/ckb-cli_$($env:CKB_CLI_VERSION)_x86_64-pc-windows-msvc.zip" -outfile "ckb-cli_$($env:CKB_CLI_VERSION)_x86_64-pc-windows-msvc.zip"
      - name: Prepare archive
        run: |
          $env:GIT_TAG_NAME=($env:GITHUB_REF -split '/')[3]
          mkdir releases
          mkdir releases/ckb_$($env:GIT_TAG_NAME)_x86_64-pc-windows-msvc
          cp -r target/release/ckb.exe,README.md,CHANGELOG.md,COPYING,docs releases/ckb_$($env:GIT_TAG_NAME)_x86_64-pc-windows-msvc
          cp devtools/windows/ckb-init-mainnet.bat,devtools/windows/ckb-reinit-mainnet.bat,devtools/windows/ckb-run.bat releases/ckb_${$env:GIT_TAG_NAME}_x86_64-pc-windows-msvc
          cp rpc/README.md releases/ckb_$($env:GIT_TAG_NAME)_x86_64-pc-windows-msvc/docs/rpc.md
          expand-archive -path ckb-cli_$($env:CKB_CLI_VERSION)_x86_64-pc-windows-msvc.zip -DestinationPath ${{ github.workspace }}
          mv ckb-cli_$($env:CKB_CLI_VERSION)_x86_64-pc-windows-msvc/ckb-cli.exe releases/ckb_$( $env:GIT_TAG_NAME)_x86_64-pc-windows-msvc/
      - name: Archive Files
        run: |
          $env:GIT_TAG_NAME=($env:GITHUB_REF -split '/')[3]
          Compress-Archive -Path releases/ckb_$( $env:GIT_TAG_NAME)_x86_64-pc-windows-msvc -DestinationPath releases/ckb_$($env:GIT_TAG_NAME)_$($env:REL_PKG)
      - name: Sign Archive
        env:
          LARGE_SECRET_PASSPHRASE: ${{ secrets.LARGE_SECRET_PASSPHRASE }}
          GPG_SIGNER: ${{ secrets.GPG_SIGNER }}
        run: |
          $CYGPWD = cygpath -u (Get-Location)
          gpg --quiet --batch --yes --decrypt --passphrase="$env:LARGE_SECRET_PASSPHRASE" --output "$CYGPWD/devtools/ci/signer.asc" "$CYGPWD/devtools/ci/signer.asc.gpg"
          gpg --import "$CYGPWD/devtools/ci/signer.asc"
          $env:GIT_TAG_NAME=($env:GITHUB_REF -split '/')[3]
          gpg -u "$env:GPG_SIGNER" -ab "$CYGPWD/releases/ckb_$($env:GIT_TAG_NAME)_$($env:REL_PKG)"
          mv ${{ github.workspace }}/releases/ckb_$($env:GIT_TAG_NAME)_$($env:REL_PKG) ${{ github.workspace }}
          mv ${{ github.workspace }}/releases/ckb_$($env:GIT_TAG_NAME)_$($env:REL_PKG).asc ${{ github.workspace }}
      - name: upload-artifact
        uses: actions/upload-artifact@v4
        with:
          name: ckb_${{env.GIT_TAG_NAME }}_${{env.REL_PKG }}
          path: ckb_${{env.GIT_TAG_NAME }}_${{env.REL_PKG }}
      - name: upload-artifact
        uses: actions/upload-artifact@v4
        with:
          name: ckb_${{env.GIT_TAG_NAME }}_${{env.REL_PKG }}.asc
          path: ckb_${{env.GIT_TAG_NAME }}_${{env.REL_PKG }}.asc
    env:
      REL_PKG: x86_64-pc-windows-msvc.zip
      CKB_CLI_VERSION: ${{ needs.get-ckb-cli-version.outputs.version }}

  Upload_File:
    name: Upload_Zip_File
    runs-on: ubuntu-22.04
    strategy:
      matrix:
        include:
          - REL_PKG: x86_64-unknown-linux-gnu.tar.gz
          - REL_PKG: x86_64-unknown-linux-gnu-portable.tar.gz
          - REL_PKG: aarch64-unknown-linux-gnu.tar.gz
          - REL_PKG: x86_64-unknown-centos-gnu.tar.gz
          - REL_PKG: x86_64-unknown-centos-gnu-portable.tar.gz
          - REL_PKG: x86_64-apple-darwin.zip
          - REL_PKG: x86_64-apple-darwin-portable.zip
          - REL_PKG: aarch64-apple-darwin.zip
          - REL_PKG: aarch64-apple-darwin-portable.zip
          - REL_PKG: x86_64-pc-windows-msvc.zip
    needs:
      - create-release
      - package-for-linux
      - package-for-linux-aarch64
      - package-for-mac
      - package-for-mac-aarch64
      - package-for-windows
      - package-for-centos
    steps:
      - uses: actions/checkout@v4
      - name: Set tag
        run: |
          export GIT_TAG_NAME=` echo ${{ github.ref }} | awk -F '/' '{print $4}' `
          echo "GIT_TAG_NAME=$GIT_TAG_NAME" >> $GITHUB_ENV
      - name: Prepare - Download tar
        uses: actions/download-artifact@v4
        with:
          name: ckb_${{env.GIT_TAG_NAME}}_${{ matrix.REL_PKG }}
      - name: Prepare - Download asc
        uses: actions/download-artifact@v4
        with:
          name: ckb_${{env.GIT_TAG_NAME}}_${{ matrix.REL_PKG }}.asc
      - name: Upload tar assets
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ needs.create-release.outputs.upload_url }}
          asset_name: ckb_${{env.GIT_TAG_NAME}}_${{ matrix.REL_PKG }}
          asset_path: ${{ github.workspace }}/ckb_${{env.GIT_TAG_NAME }}_${{ matrix.REL_PKG }}
          asset_content_type: application/octet-stream
      - name: Upload asc assets
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ needs.create-release.outputs.upload_url }}
          asset_name: ckb_${{env.GIT_TAG_NAME}}_${{ matrix.REL_PKG }}.asc
          asset_path: ${{ github.workspace }}/ckb_${{env.GIT_TAG_NAME }}_${{ matrix.REL_PKG }}.asc
          asset_content_type: application/octet-stream

  Trigger_smoking_test:
    name: Trigger_smoking_test
    runs-on: ubuntu-22.04
    needs:
      - Upload_File
    steps:
      - uses: actions/checkout@v4
      - name: Set tag
        run: |
          export GIT_TAG_NAME=` echo ${{ github.ref }} | awk -F '/' '{print $4}' `
          echo "GIT_TAG_NAME=$GIT_TAG_NAME" >> $GITHUB_ENV
      - uses: peter-evans/repository-dispatch@v1
        with:
          token: ${{ secrets.REPO_ACCESS_TOKEN }}
          event-type: smoking-test
          client-payload: '{"CKB_linux_release_package": "${{secrets.QINIU_CDN_SITE}}/ckb_${{env.GIT_TAG_NAME}}_x86_64-unknown-linux-gnu.7z"}'
