name: Release

on:
  push:
    tags:
      - "v*"
  workflow_dispatch: { }

env:
  GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  BUCKET: ${{ secrets.CLI_OSS_BUCKET }}
  ENDPOINT: ${{ secrets.CLI_OSS_ENDPOINT }}
  ACCESS_KEY: ${{ secrets.CLI_OSS_ACCESS_KEY }}
  ACCESS_KEY_SECRET: ${{ secrets.CLI_OSS_ACCESS_KEY_SECRET }}

jobs:
  build:
    runs-on: ubuntu-latest
    name: build
    strategy:
      matrix:
        TARGETS: [ linux/amd64, darwin/amd64, windows/amd64, linux/arm64, darwin/arm64 ]
    env:
      VELA_VERSION_KEY: github.com/oam-dev/kubevela/version.VelaVersion
      VELA_GITVERSION_KEY: github.com/oam-dev/kubevela/version.GitRevision
      GO_BUILD_ENV: GO111MODULE=on CGO_ENABLED=0
      DIST_DIRS: find * -type d -exec
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Set up Go
        uses: actions/setup-go@v2
        with:
          go-version: 1.19
      - name: Get release
        id: get_release
        uses: bruceadams/get-release@v1.2.2
      - name: Get version
        run: echo "VELA_VERSION=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV
      - name: Get matrix
        id: get_matrix
        run: |
          TARGETS=${{matrix.TARGETS}}
          echo ::set-output name=OS::${TARGETS%/*}
          echo ::set-output name=ARCH::${TARGETS#*/}
      - name: Get ldflags
        id: get_ldflags
        run: |
          LDFLAGS="-s -w -X ${{ env.VELA_VERSION_KEY }}=${{ env.VELA_VERSION }} -X ${{ env.VELA_GITVERSION_KEY }}=git-$(git rev-parse --short HEAD)"
          echo "LDFLAGS=${LDFLAGS}" >> $GITHUB_ENV
      - name: Build
        run: |
          ${{ env.GO_BUILD_ENV }} GOOS=${{ steps.get_matrix.outputs.OS }} GOARCH=${{ steps.get_matrix.outputs.ARCH }} \
            go build -ldflags "${{ env.LDFLAGS }}" \
            -o _bin/vela/${{ steps.get_matrix.outputs.OS }}-${{ steps.get_matrix.outputs.ARCH }}/vela -v \
            ./references/cmd/cli/main.go
          ${{ env.GO_BUILD_ENV }} GOOS=${{ steps.get_matrix.outputs.OS }} GOARCH=${{ steps.get_matrix.outputs.ARCH }} \
            go build -ldflags "${{ env.LDFLAGS }}" \
            -o _bin/kubectl-vela/${{ steps.get_matrix.outputs.OS }}-${{ steps.get_matrix.outputs.ARCH }}/kubectl-vela -v \
            ./cmd/plugin/main.go
      - name: Compress
        run: |
          echo "\n## Release Info\nVERSION: ${{ env.VELA_VERSION }}" >> README.md && \
          echo "GIT_COMMIT: ${GITHUB_SHA}\n" >> README.md && \
          cd _bin/vela && \
          ${{ env.DIST_DIRS }} cp ../../LICENSE {} \; && \
          ${{ env.DIST_DIRS }} cp ../../README.md {} \; && \
          ${{ env.DIST_DIRS }} tar -zcf vela-{}.tar.gz {} \; && \
          ${{ env.DIST_DIRS }} zip -r vela-{}.zip {} \; && \
          cd ../kubectl-vela && \
          ${{ env.DIST_DIRS }} cp ../../LICENSE {} \; && \
          ${{ env.DIST_DIRS }} cp ../../README.md {} \; && \
          ${{ env.DIST_DIRS }} tar -zcf kubectl-vela-{}.tar.gz {} \; && \
          ${{ env.DIST_DIRS }} zip -r kubectl-vela-{}.zip {} \; && \
          cd .. && \
          sha256sum vela/vela-* kubectl-vela/kubectl-vela-* >> sha256-${{ steps.get_matrix.outputs.OS }}-${{ steps.get_matrix.outputs.ARCH }}.txt \
      - name: Upload Vela tar.gz
        uses: actions/upload-release-asset@v1.0.2
        with:
          upload_url: ${{ steps.get_release.outputs.upload_url }}
          asset_path: ./_bin/vela/vela-${{ steps.get_matrix.outputs.OS }}-${{ steps.get_matrix.outputs.ARCH }}.tar.gz
          asset_name: vela-${{ env.VELA_VERSION }}-${{ steps.get_matrix.outputs.OS }}-${{ steps.get_matrix.outputs.ARCH }}.tar.gz
          asset_content_type: binary/octet-stream
      - name: Upload Vela zip
        uses: actions/upload-release-asset@v1.0.2
        with:
          upload_url: ${{ steps.get_release.outputs.upload_url }}
          asset_path: ./_bin/vela/vela-${{ steps.get_matrix.outputs.OS }}-${{ steps.get_matrix.outputs.ARCH }}.zip
          asset_name: vela-${{ env.VELA_VERSION }}-${{ steps.get_matrix.outputs.OS }}-${{ steps.get_matrix.outputs.ARCH }}.zip
          asset_content_type: binary/octet-stream
      - name: Upload Kubectl-Vela tar.gz
        uses: actions/upload-release-asset@v1.0.2
        with:
          upload_url: ${{ steps.get_release.outputs.upload_url }}
          asset_path: ./_bin/kubectl-vela/kubectl-vela-${{ steps.get_matrix.outputs.OS }}-${{ steps.get_matrix.outputs.ARCH }}.tar.gz
          asset_name: kubectl-vela-${{ env.VELA_VERSION }}-${{ steps.get_matrix.outputs.OS }}-${{ steps.get_matrix.outputs.ARCH }}.tar.gz
          asset_content_type: binary/octet-stream
      - name: Upload Kubectl-Vela zip
        uses: actions/upload-release-asset@v1.0.2
        with:
          upload_url: ${{ steps.get_release.outputs.upload_url }}
          asset_path: ./_bin/kubectl-vela/kubectl-vela-${{ steps.get_matrix.outputs.OS }}-${{ steps.get_matrix.outputs.ARCH }}.zip
          asset_name: kubectl-vela-${{ env.VELA_VERSION }}-${{ steps.get_matrix.outputs.OS }}-${{ steps.get_matrix.outputs.ARCH }}.zip
          asset_content_type: binary/octet-stream
      - name: Post sha256
        uses: actions/upload-artifact@v2
        with:
          name: sha256sums
          path: ./_bin/sha256-${{ steps.get_matrix.outputs.OS }}-${{ steps.get_matrix.outputs.ARCH }}.txt
          retention-days: 1
      - name: clear the asset
        run: |
          rm -rf ./_bin/vela/${{ steps.get_matrix.outputs.OS }}-${{ steps.get_matrix.outputs.ARCH }}
          mv ./_bin/vela/vela-${{ steps.get_matrix.outputs.OS }}-${{ steps.get_matrix.outputs.ARCH }}.tar.gz ./_bin/vela/vela-${{ env.VELA_VERSION }}-${{ steps.get_matrix.outputs.OS }}-${{ steps.get_matrix.outputs.ARCH }}.tar.gz
          mv ./_bin/vela/vela-${{ steps.get_matrix.outputs.OS }}-${{ steps.get_matrix.outputs.ARCH }}.zip ./_bin/vela/vela-${{ env.VELA_VERSION }}-${{ steps.get_matrix.outputs.OS }}-${{ steps.get_matrix.outputs.ARCH }}.zip
      - name: Install ossutil
        run: wget http://gosspublic.alicdn.com/ossutil/1.7.0/ossutil64 && chmod +x ossutil64 && mv ossutil64 ossutil
      - name: Configure Alibaba Cloud OSSUTIL
        run: ./ossutil --config-file .ossutilconfig config -i ${ACCESS_KEY} -k ${ACCESS_KEY_SECRET} -e ${ENDPOINT} -c .ossutilconfig
      - name: sync local to cloud
        run: ./ossutil --config-file .ossutilconfig sync ./_bin/vela oss://$BUCKET/binary/vela/${{ env.VELA_VERSION }}
      
      - name: sync the latest version file
        if: ${{ !contains(env.VELA_VERSION,'alpha') && !contains(env.VELA_VERSION,'beta') }}
        run: |
          LATEST_VERSION=$(curl -fsSl https://static.kubevela.net/binary/vela/latest_version)
          verlte() {
            [  "$1" = "`echo -e "$1\n$2" | sort -V | head -n1`" ]
          }
          verlte ${{ env.VELA_VERSION }} $LATEST_VERSION && echo "${{ env.VELA_VERSION }} <= $LATEST_VERSION, skip update" && exit 0
          echo ${{ env.VELA_VERSION }} > ./latest_version
          ./ossutil --config-file .ossutilconfig cp -u ./latest_version oss://$BUCKET/binary/vela/latest_version


  upload-plugin-homebrew:
    needs: build
    runs-on: ubuntu-latest
    name: upload-sha256sums
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Get release
        id: get_release
        uses: bruceadams/get-release@v1.2.2
      - name: Download sha256sums
        uses: actions/download-artifact@v2
        with:
          name: sha256sums
          path: cli-artifacts
      - name: Display structure of downloaded files
        run: ls -R
        working-directory: cli-artifacts
      - name: Get version
        run: echo "VELA_VERSION=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV
      - shell: bash
        working-directory: cli-artifacts
        run: |
          for file in *
          do
            sed -i "s/\/vela/-${{ env.VELA_VERSION }}/g" ${file}
            sed -i "s/\/kubectl-vela/-${{ env.VELA_VERSION }}/g" ${file}
            cat ${file} >> sha256sums.txt
          done
      - name: Upload Checksums
        uses: actions/upload-release-asset@v1.0.2
        with:
          upload_url: ${{ steps.get_release.outputs.upload_url }}
          asset_path: cli-artifacts/sha256sums.txt
          asset_name: sha256sums.txt
          asset_content_type: text/plain
      - name: Update kubectl plugin version in krew-index
        uses: rajatjindal/krew-release-bot@v0.0.38
      - name: Update Homebrew formula
        uses: dawidd6/action-homebrew-bump-formula@v3
        with:
          token: ${{ secrets.HOMEBREW_TOKEN }}
          formula: kubevela
          tag: ${{ github.ref }}
          revision: ${{ github.sha }}
          force: false
