name: Ansible CI/CD

on:
  pull_request:
    branches:
      - main
    types: [closed]


jobs:
  deploy:
    name: Deploy Ansible Playbook
    if: github.event.pull_request.merged == true
    runs-on: skylab

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Set up SSH
        run: |
          echo "${{ secrets.DEVHOST_SSH_PRIVATE_KEY }}" > private_key.pem
          chmod 600 private_key.pem
          
      - name: Install Ansible
        shell: bash
        run: |
          sudo apt update
          sudo apt install -y ansible

      - name: Run Ansible Playbook
        env:
          ANSIBLE_USER: ${{ secrets.ANSIBLE_USER }}
          ANSIBLE_HOST_KEY_CHECKING: False
        run: |
          ansible-playbook -i opdracht-2/inventory.ini opdracht-2/nginx.yml --private-key private_key.pem -u ${{ secrets.DEVHOST_ANSIBLE_USER }}