name: Publish Collab Server Image

on:
  push:
    tags:
      - collab-v*

env:
  DOCKER_BUILDKIT: 1
  DIGITALOCEAN_ACCESS_TOKEN: ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }}

jobs:
  publish:
    name: Publish collab server image 
    runs-on:
      - self-hosted
      - deploy
    steps:
      - name: Add Rust to the PATH
        run: echo "$HOME/.cargo/bin" >> $GITHUB_PATH

      - name: Sign into DigitalOcean docker registry
        run: doctl registry login

      - name: Checkout repo
        uses: actions/checkout@v3
        with:
          clean: false
          submodules: 'recursive'

      - name: Determine version
        run: |
          set -eu
          version=$(script/get-crate-version collab)
          if [[ $GITHUB_REF_NAME != "collab-v${version}" ]]; then
            echo "release tag ${GITHUB_REF_NAME} does not match version ${version}"
            exit 1
          fi
          echo "Publishing collab version: ${version}"
          echo "COLLAB_VERSION=${version}" >> $GITHUB_ENV

      - name: Build docker image
        run: docker build . --tag registry.digitalocean.com/zed/collab:v${COLLAB_VERSION}
    
      - name: Publish docker image
        run: docker push registry.digitalocean.com/zed/collab:v${COLLAB_VERSION}
