name: BuildReleaser

on:
  push:
    tags:
      - 'v4*'

jobs:
  goreleaser:
    runs-on: ubuntu-latest
    if: github.repository == 'kubesphere/kubekey'
    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          fetch-depth: 0
      - name: Prepare
        id: prepare
        run: |
          LDFLAGS=$(bash hack/version.sh)
          VERSION=latest
          if [[ $GITHUB_REF == refs/tags/* ]]; then
            VERSION=${GITHUB_REF#refs/tags/}
          fi

          echo "ldflags=${LDFLAGS}" >> "$GITHUB_OUTPUT"
          echo "version=${VERSION}" >> "$GITHUB_OUTPUT"
      - name: Set up Go
        uses: actions/setup-go@v4
        with:
          go-version: 1.23.3

      - name: Run GoReleaser
        uses: goreleaser/goreleaser-action@v4
        with:
          distribution: goreleaser
          version: v2.5.1
          args: release --clean
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          LDFLAGS: ${{ steps.prepare.outputs.ldflags }}

      - name: Upload Extra File
        run: |
          TAG=${{ steps.prepare.outputs.version }}
          if [[ $tag =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
            REGISTRY=docker.io/kubesphere TAG=$tag make generate
          else
            REGISTRY=docker.io/kubespheredev TAG=$tag make generate
          fi
          gh release upload "$TAG" config/capkk/release/* --clobber
          gh release upload "$TAG" hack/downloadKubekey.sh --clobber
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Synchronize artifacts to OSS
        run: |
          # Check if the current Git reference is a valid semantic version tag (e.g., v1.2.3)
          if [[ ! ${GITHUB_REF#refs/*/} =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
              echo "Skipping artifact synchronization: not a release tag."
              return 0
          fi
          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 -c qsctl-config.yaml cp dist/kubekey-${{ steps.prepare.outputs.version }}-linux-amd64.tar.gz qs://kubekey/github.com/kubesphere/kubekey/releases/download/${{ steps.prepare.outputs.version }}/kubekey-${{ steps.prepare.outputs.version }}-linux-amd64.tar.gz
          qsctl -c qsctl-config.yaml cp dist/kubekey-${{ steps.prepare.outputs.version }}-linux-arm64.tar.gz qs://kubekey/github.com/kubesphere/kubekey/releases/download/${{ steps.prepare.outputs.version }}/kubekey-${{ steps.prepare.outputs.version }}-linux-arm64.tar.gz

          rm -rf qsctl-config.yaml
