name: Build Docker
on:
  push:
    branches:
      - main

jobs:
  deploy:
    runs-on: node-ansible
    steps:

      - name: Setup SSH key
        run: |
          echo "${{ secrets.SSH_PRIVATE_KEY_RU }}" > /tmp/id_ru 
          chmod 600 /tmp/id_ru
#ключ машины
      - name: Create .env file
        run: echo "${{ secrets.RUNNER_ENV }}" > .env
#env для runners
      - name: Checkout only deploy.yml
        uses: actions/checkout@v4
        with:
          repository: MH.Dmitrii/wedding-site
          ref: main                                
          path: tmp-repo
          sparse-checkout: |
            ansible/
      - name: Run Ansible playbook
        run: ansible-playbook -i tmp-repo/ansible/inventory.ini tmp-repo/ansible/deploy.yml -e @tmp-repo/ansible/secrets.yml -e env_file="$(pwd)/.env"
        env:
          ANSIBLE_PRIVATE_KEY_FILE: /tmp/id_ru
          ANSIBLE_HOST_KEY_CHECKING: "False"

  build:
    runs-on: node-docker
    needs: deploy
    steps:
      - uses: actions/checkout@v4
      
      - name: Login to Docker Hub
        run: echo "${{ secrets.DOCKER_PASSWORD }}" | docker login -u mh.dmitrii@homyk.space --password-stdin

      - name: Create .env file
        run: echo "${{ secrets.WEDDING_SITE_ENV  }}" > .env

      - name: Check env
        run: cat .env
#env для проекта
      - name: Build image
        run: docker build -t back:latest -f docker/dockerfile.project .

      - name: Start docker-compose of project
        run: docker compose --env-file .env -f docker/docker-compose.yaml up -d

      - name: Build image caddy
        run: docker build -t caddy:wedding -f docker/caddy/dockerfile.caddy .

      - name: Start docker-compose caddy
        run: docker compose --env-file .env -f docker/caddy/caddy.yaml up -d
  