name: Deploy
run-name: Release Docker Image
on:
  push:
    tags:
      - 'v*'

jobs:
  Release-Docker-Image:
    runs-on: ubuntu-latest
    environment: release
    timeout-minutes: 3
    steps:
      - name: Checkout Repository
        uses: actions/checkout@v4

      - name: Set up Node Environment lts/hydrogen
        uses: actions/setup-node@v4
        with:
          node-version: lts/hydrogen
          cache: "npm"

      - name: Create Environment Variables
        run: |
          touch .env
          echo CLIENT_TOKEN = NOT_REAL_TOKEN >> .env
          echo MODEL = ${{ secrets.MODEL }} >> .env
          echo CLIENT_UID = ${{ secrets.CLIENT_UID }} >> .env
          echo OLLAMA_IP = ${{ secrets.OLLAMA_IP }} >> .env
          echo OLLAMA_PORT = ${{ secrets.OLLAMA_PORT }} >> .env

      - name: Get Version from package.json
        run: echo "VERSION=$(jq -r '.version' package.json)" >> $GITHUB_ENV
      
      - name: Build Image
        run: |
          npm run docker:build

      - name: Build Image as Latest
        run: |
          npm run docker:build-latest
      
      - name: Log into Docker
        run: |
          docker login --username ${{ vars.DOCKER_USER }} --password ${{ secrets.DOCKER_PASS }}

      - name: Release Docker Image
        run: |
          docker push ${{ vars.DOCKER_USER }}/discord-ollama:${{ env.VERSION }}
          docker push ${{ vars.DOCKER_USER }}/discord-ollama:latest