name: Cloud Hypervisor Release
on: [create, merge_group]
concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}-${{ github.event_name }}
  cancel-in-progress: true
env:
  GITHUB_TOKEN: ${{ github.token }}

jobs:
  release:
    if: (github.event_name == 'create' && github.event.ref_type == 'tag') || github.event_name == 'merge_group'
    name: Release ${{ matrix.platform.target }}
    strategy:
      fail-fast: false
      matrix:
        platform:
        - target: x86_64-unknown-linux-gnu
          args: --all --release --features mshv
          name_ch: cloud-hypervisor
          name_ch_remote: ch-remote
        - target: x86_64-unknown-linux-musl
          args: --all --release --features mshv
          name_ch: cloud-hypervisor-static
          name_ch_remote: ch-remote-static
        - target: aarch64-unknown-linux-musl
          args:  --all --release
          name_ch: cloud-hypervisor-static-aarch64
          name_ch_remote: ch-remote-static-aarch64
    runs-on: ubuntu-latest
    steps:
      - name: Code checkout
        uses: actions/checkout@v7
      - name: Install musl-gcc
        if: contains(matrix.platform.target, 'musl')
        run: sudo apt install -y musl-tools
      - name: Create release directory
        if: |
          github.event_name == 'create' && github.event.ref_type == 'tag' &&
          matrix.platform.target == 'x86_64-unknown-linux-gnu'
        run: rsync -rv --exclude=.git . ../cloud-hypervisor-${{ github.event.ref }}
      - name: Build ${{ matrix.platform.target }}
        uses: houseabsolute/actions-rust-cross@v1
        with:
          command: build
          target: ${{ matrix.platform.target }}
          args: ${{ matrix.platform.args  }}
          strip: true
          toolchain: "1.89.0"
      - name: Copy Release Binaries
        if: github.event_name == 'create' && github.event.ref_type == 'tag'
        shell: bash
        run: |
          cp target/${{ matrix.platform.target }}/release/cloud-hypervisor ./${{ matrix.platform.name_ch }}
          cp target/${{ matrix.platform.target }}/release/ch-remote ./${{ matrix.platform.name_ch_remote }}
      - name: Upload Release Artifacts
        if: github.event_name == 'create' && github.event.ref_type == 'tag'
        uses: actions/upload-artifact@v7
        with:
          name: Artifacts for ${{ matrix.platform.target }}
          path: |
            ./${{ matrix.platform.name_ch }}
            ./${{ matrix.platform.name_ch_remote }}
      - name: Vendor
        if: |
          github.event_name == 'create' && github.event.ref_type == 'tag' &&
          matrix.platform.target == 'x86_64-unknown-linux-gnu'
        working-directory: ../cloud-hypervisor-${{ github.event.ref }}
        run: |
          mkdir ../vendor-cargo-home
          export CARGO_HOME=$(realpath ../vendor-cargo-home)
          mkdir .cargo
          cargo vendor > .cargo/config.toml
      - name: Create vendored source archive
        if: |
          github.event_name == 'create' && github.event.ref_type == 'tag' &&
          matrix.platform.target == 'x86_64-unknown-linux-gnu'
        run: tar cJf cloud-hypervisor-${{ github.event.ref }}.tar.xz ../cloud-hypervisor-${{ github.event.ref }}
      - name: Upload cloud-hypervisor vendored source archive
        if: |
          github.event_name == 'create' && github.event.ref_type == 'tag' &&
          matrix.platform.target == 'x86_64-unknown-linux-gnu'
        id: upload-release-cloud-hypervisor-vendored-sources
        uses: actions/upload-artifact@v7
        with:
          path: cloud-hypervisor-${{ github.event.ref }}.tar.xz
          name: cloud-hypervisor-${{ github.event.ref }}.tar.xz
      - name: Create GitHub Release
        if: github.event_name == 'create' && github.event.ref_type == 'tag'
        uses: softprops/action-gh-release@v3
        with:
          draft: true
          files: |
            ./${{ matrix.platform.name_ch }}
            ./${{ matrix.platform.name_ch_remote }}
            ./cloud-hypervisor-${{ github.event.ref }}.tar.xz
