---
# SPDX-License-Identifier: GPL-2.0-or-later
#
# This file is part of nvme.
# Copyright (c) 2026 SUSE LLC
#
# Authors: Daniel Wagner <dwagner@suse.de>

name: Upload

on:
  push:
    branches: [master]
    tags:
      - 'v*'
  release:
    types: [published]

jobs:
  build-static:
    name: build static binary
    runs-on: ubuntu-latest
    container:
      image: ghcr.io/linux-nvme/debian:latest
    outputs:
      VERSION: ${{ steps.build.outputs.VERSION }}
    steps:
      - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
        with:
          fetch-depth: 0

      - name: Mark repo as safe for git
        run: git config --global --add safe.directory "$GITHUB_WORKSPACE"

      - name: build
        id: build
        run: |
          git config --global --add safe.directory "$GITHUB_WORKSPACE"
          VERSION="$(git describe --always --abbrev=12 --dirty)"
          echo "VERSION=${VERSION}" >> $GITHUB_OUTPUT
          echo $VERSION

          scripts/build.sh static
          mkdir upload
          cp .build-ci/nvme upload/nvme-cli-latest-x86_64
          case "${GITHUB_REF}" in
            refs/tags/v*)
              cp .build-ci/nvme "upload/nvme-cli-${GITHUB_REF_NAME}-x86_64"
              ;;
          esac

      - uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f # v7.0.0
        name: upload artifacts to github
        with:
          name: nvme-cli
          path: upload/*

  upload-test-binary:
    name: upload test binary
    runs-on: ubuntu-latest
    needs: build-static
    if: ${{ (github.event_name == 'push' || github.event_name == 'release') && github.repository == 'linux-nvme/nvme-cli' }}
    steps:
      - uses: actions/download-artifact@70fc10c6e5e1ce46ad2ea6f2b72d43f7d47b13c3 # v8.0.0
        with:
          name: nvme-cli
          path: upload

      - name: upload to SFTP server
        env:
          SFTP_USERNAME: ${{ secrets.SFTP_USERNAME }}
          SFTP_SERVER: ${{ secrets.SFTP_SERVER }}
          SFTP_HOST_KEY: ${{ secrets.SFTP_HOST_KEY }}
          SFTP_PRIVATE_KEY: ${{ secrets.SFTP_PRIVATE_KEY }}
        run: |
          mkdir -p ~/.ssh
          chmod 700 ~/.ssh
          echo "${SFTP_HOST_KEY}" > ~/.ssh/known_hosts
          echo "${SFTP_PRIVATE_KEY}" > ~/.ssh/id_ed25519
          chmod 600 ~/.ssh/id_ed25519
          SFTP_BATCH=$(mktemp)
          trap 'rm -f "$SFTP_BATCH"' EXIT
          echo "cd /upload" > "$SFTP_BATCH"
          for f in upload/nvme-cli-*; do
            [ -f "$f" ] || continue
            echo "put $f $(basename "$f")" >> "$SFTP_BATCH"
          done
          sftp -b "$SFTP_BATCH" "${SFTP_USERNAME}@${SFTP_SERVER}"

  upload-release-assets:
    name: upload GitHub release assets
    runs-on: ubuntu-latest
    needs: build-static
    env:
      VERSION: ${{ needs.build-static.outputs.VERSION }}
    if: ${{ github.event_name == 'release' && github.repository == 'linux-nvme/nvme-cli' }}
    steps:
      - uses: actions/download-artifact@70fc10c6e5e1ce46ad2ea6f2b72d43f7d47b13c3 # v8.0.0
        with:
          name: nvme-cli
          path: upload
      - name: upload versioned binary
        uses: actions/upload-release-asset@e8f9f06c4b078e705bd2ea027f0926603fc9b4d5 # v1.0.2
        with:
          upload_url: ${{ github.event.release.upload_url }}
          asset_path: upload/nvme-cli-${{ env.VERSION }}-x86_64
          asset_name: nvme-cli-${{ env.VERSION }}-x86_64
          asset_content_type: application/octet-stream
