name: release

run-name: ${{ gitea.actor }} release MouseSoft Draw.IO Export

on:
  push:
    tags:
      - "v*"

jobs:
  release:
    runs-on: ubuntu-latest
    # container:
    #   image: git.mousesoft.ru/ms/gitea-runner-images:python-ubuntu-latest
    steps:
      - name: check-out repository code
        uses: https://gitea.com/actions/checkout@v4
        with:
          fetch-depth: 0
          fetch-tags: true

      - name: set-up go
        uses: https://gitea.com/actions/setup-go@v3
        with:
          go-version: ">=1.22"

      - name: test
        run: make clean vendor lint test

      - name: cache
        uses: https://gitea.com/actions/cache@v4
        with:
          path: |
            out/*.deb
            out/*.changes
          key: ms_drawio-export_pkg_${{ github.sha }}

      - name: build amd64 package
        if: steps.cache.outputs.cache-hit != 'true'
        run: GOARCH=amd64 make package

      - name: build arm64 package
        if: steps.cache.outputs.cache-hit != 'true'
        run: GOARCH=arm64 make package

      - name: deploy packages
        env:
          SSH_HOST: ${{ secrets.DEB_SSH_HOST }}
          SSH_PORT: ${{ secrets.DEB_SSH_PORT }}
          SSH_USER: ${{ secrets.DEB_SSH_USER }}
          SSH_KEY: ${{ secrets.DEB_SSH_KEY }}
        run: |
          sed -e "s/DEB_SSH_HOST/$SSH_HOST/g" deploy/dput/mousesoft.json.tpl > /etc/dput.d/profiles/mousesoft.json
          mkdir -p ~/.ssh/
          echo "$SSH_KEY" | tr -d '\r' > ~/.ssh/key
          chmod 0600 ~/.ssh/key
          cat >>~/.ssh/config <<END
          Host $SSH_HOST
            IdentitiesOnly yes
            User $SSH_USER
            IdentityFile ~/.ssh/key
            Port $SSH_PORT
            StrictHostKeyChecking no
          END
          cat ~/.ssh/config
          make deploy

      - name: get meta-data
        id: meta
        run: |
          echo "VERSION=$(make version)" >> $GITHUB_OUTPUT
          echo 'CHANGES<<EOF' >> $GITHUB_OUTPUT
          gawk -f scripts/changes.awk -v version=$(make version-number) CHANGELOG.md >> $GITHUB_OUTPUT
          echo EOF >> $GITHUB_OUTPUT

      - name: release
        uses: https://gitea.com/actions/gitea-release-action@v1
        with:
          title: "MouseSoft DrawIO-Export ${{ steps.meta.outputs.VERSION }}"
          body: "${{ steps.meta.outputs.CHANGES }}"
          files: |
            out/*.deb
          sha256sum: true
          prerelease: true
          draft: true
