name: Gitea Docker Redeploy
run-name: ${{ gitea.actor }} is deploying new version
on: [push]
jobs:
  ssh_to_self_and_redeploy:
    name: Redploy-App-on-self-via-SSH
    runs-on: ubuntu-latest
    steps:
    - name: Down app and git pull
      uses: appleboy/ssh-action@v1.0.3
      with:
        host: ${{ secrets.HOST }}
        username: ${{ secrets.USERNAME }}
        password: ${{ secrets.PASSWORD }}
        port: ${{ secrets.PORT }}
        script_stop: true
        #requires stored git password: git config credential.helper store
        script: |
            cd Ollama-Bot/deployment
            docker-compose down
            git pull
            echo "============ Git pull finished! ========="
            echo '${{ vars.ENV }}' > .env
            echo "============ Env creation finished! ========="


    - name: Build image
      uses: appleboy/ssh-action@v1.0.3
      with:
        host: ${{ secrets.HOST }}
        username: ${{ secrets.USERNAME }}
        password: ${{ secrets.PASSWORD }}
        port: ${{ secrets.PORT }}
        script_stop: true
        #requires stored git password: git config credential.helper store
        script: |
            cd Ollama-Bot/deployment
            docker-compose build
            echo "============= Image build finishes! =============="


    - name: Push image
      uses: appleboy/ssh-action@v1.0.3
      with:
        host: ${{ secrets.HOST }}
        username: ${{ secrets.USERNAME }}
        password: ${{ secrets.PASSWORD }}
        port: ${{ secrets.PORT }}
        script_stop: true
        #requires stored git password: git config credential.helper store
        script: |
            cd Ollama-Bot/deployment
            docker login --username="${{ secrets.REGUSER }}" --password="${{ secrets.REGPWD }}" ${{ vars.REGISTRY }}
            docker-compose push
            echo "============== Image push finishes! ==============="


    - name: Deploy app
      uses: appleboy/ssh-action@v1.0.3
      with:
        host: ${{ secrets.HOST }}
        username: ${{ secrets.USERNAME }}
        password: ${{ secrets.PASSWORD }}
        port: ${{ secrets.PORT }}
        #script_stop: true
        #requires stored git password: git config credential.helper store
        script: |
            cd Ollama-Bot/deployment
            docker-compose up -d
            echo "========== Docker compose deployment finished! =============="


    - name: Clean up
      uses: appleboy/ssh-action@v1.0.3
      with:
        host: ${{ secrets.HOST }}
        username: ${{ secrets.USERNAME }}
        password: ${{ secrets.PASSWORD }}
        port: ${{ secrets.PORT }}
        #script_stop: true
        #requires stored git password: git config credential.helper store
        script: |
            docker rmi $(docker images -f "dangling=true" -q)
            echo "========== Docker dangling images cleanup finished! =============="









