name: Deploy Production

on:
  workflow_dispatch:
    inputs:
      image_sha:
        description: "Getesteter Commit SHA aus Staging"
        required: true

jobs:
  deploy-production:
    runs-on: francesco

    steps:
      - name: Login to Docker Hub
        run: |
          echo "${{ secrets.DOCKER_TOKEN }}" | docker login \
            -u "${{ secrets.DOCKER_USERNAME }}" \
            --password-stdin

      - name: Pull tested image
        run: |
          docker pull francesco448/razorpagesmovie:${{ inputs.image_sha }}

      - name: Promote image to Production
        run: |
          docker tag \
            francesco448/razorpagesmovie:${{ inputs.image_sha }} \
            francesco448/razorpagesmovie:prod

          docker push francesco448/razorpagesmovie:prod

      - name: Configure SSH
        run: |
          mkdir -p ~/.ssh
          echo "${{ secrets.TRUENAS_SSH_KEY }}" > ~/.ssh/id_ed25519
          chmod 600 ~/.ssh/id_ed25519
          ssh-keyscan -H "${{ secrets.TRUENAS_HOST }}" >> ~/.ssh/known_hosts

      - name: Deploy Production
        run: |
          ssh -i ~/.ssh/id_ed25519 \
            "${{ secrets.TRUENAS_USER }}@${{ secrets.TRUENAS_HOST }}" \
            "midclt call -j app.redeploy razorpages-movie-dev"
