name: Release

on:
  push:
    tags:
      - '*'

jobs:
  build:
    name: create draft release
    runs-on: ubuntu-latest
    steps:
      - name: Set env
        run:  echo "RELEASE_TAG=${GITHUB_REF:10}" >> $GITHUB_ENV
      - name: checkout code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - name: Install go
        uses: actions/setup-go@v5
        with:
          go-version: '^1.19'
      - name: generate release artifacts
        run: |
          make release
      - name: Release
        uses: softprops/action-gh-release@v1
        with:
          draft: true
          files: out/*
          
      - name: Get Version
        id: get_version
        run: echo "VERSION=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT
      - name: Synchronize artifacts to OSS
        run: |
          rm -rf qsctl_v2.4.3_linux_amd64.tar.gz
          wget https://attack-on-titan.gd2.qingstor.com/qsctl/v2.4.3/qsctl_v2.4.3_linux_amd64.tar.gz
          tar -zxvf qsctl_v2.4.3_linux_amd64.tar.gz
          rm -rf qsctl_v2.4.3_linux_amd64.tar.gz
          mv qsctl_v2.4.3_linux_amd64 /usr/local/bin/qsctl
          echo "access_key_id: ${{secrets.KS_QSCTL_ACCESS_KEY_ID}}" > qsctl-config.yaml
          echo "secret_access_key: ${{ secrets.KS_QSCTL_SECRET_ACCESS_KEY }}" >> qsctl-config.yaml
          qsctl cp out/kubekey-${{ steps.get_version.outputs.VERSION }}-linux-amd64.tar.gz  qs://kubernetes/kubekey/releases/download/${{ steps.get_version.outputs.VERSION }}/kubekey-${{ steps.get_version.outputs.VERSION }}-linux-amd64.tar.gz -c qsctl-config.yaml
          qsctl cp out/kubekey-${{ steps.get_version.outputs.VERSION }}-linux-arm64.tar.gz  qs://kubernetes/kubekey/releases/download/${{ steps.get_version.outputs.VERSION }}/kubekey-${{ steps.get_version.outputs.VERSION }}-linux-arm64.tar.gz -c qsctl-config.yaml
          rm -rf qsctl-config.yaml
