name: Build and Deploy on Tag
on:
  push:
    tags:
      - 'v*'

env:
  VERSION: ${{ gitea.ref_name }}
  CLWS_PACKAGE: tesses50/crosslang-withshell
  CPKG_PACKAGE: tesses50/cpkg
  GITEA_DOMAIN: git.tesses.org
  GITEA_REGISTRY_USER: tesses50

jobs:
  update-tap:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - run: |
          cd vscode-extension
          npm install
          npm install -g typescript
          npm install -g @@vscode/vsce
          vsce package
          cd ..
          wget -O /usr/local/bin/crosslang https://redirect.tesses.net/cl-slim-x86_64-linux-musl
          chmod 755 /usr/local/bin/crosslang
          ln -s /usr/local/bin/crosslang /usr/local/bin/crossc
          ln -s /usr/local/bin/crosslang /usr/local/bin/crossint
          ln -s /usr/local/bin/crosslang /usr/local/bin/crossvm
          crossint ./build.tcross pack
          mkdir -p artifacts
          cp Tesses.CrossLang.ShellPackage-*.crvm artifacts/ShellPackage.crvm
          cp Tesses.CrossLang.PackageServer/bin/Tesses.CrossLang.PackageServer-*.crvm artifacts/CPKG.crvm
      - name: "Publish artifacts"
        uses: akkuman/gitea-release-action@v1
        env:
          NODE_OPTIONS: '--experimental-fetch' # if nodejs < 18
        with:
          prerelease: true
          files: |-
            artifacts/** 
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3
      - name: Log in to registry
        uses: docker/login-action@v3
        with:
          registry: ${{ env.GITEA_DOMAIN }}
          username: ${{ env.GITEA_REGISTRY_USER }}
          password: ${{ secrets.PACKAGE_AND_BREW }}
      - name: Build and push image
        uses: docker/build-push-action@v6
        with:
          context: .
          file: ./Dockerfile
          push: true
          tags:  ${{ env.GITEA_DOMAIN }}/${{ env.CLWS_PACKAGE }}:latest
      - name: Build and push image
        uses: docker/build-push-action@v6
        with:
          context: .
          file: ./Dockerfile.cpkg
          push: true
          tags:  ${{ env.GITEA_DOMAIN }}/${{ env.CPKG_PACKAGE }}:latest