on:
  push:
    branches:
      - testing

jobs:
  test:
    runs-on: docker
    container:
      image: ghcr.io/catthehacker/ubuntu:act-22.04
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: '3.11'
          cache-dependency-path: '**/requirements.dev.txt'
      - name: Install dependencies
        working-directory: core
        run: pip3 install -r requirements.dev.txt
      - name: Run django tests
        working-directory: core
        run: python3 manage.py test

  deploy:
    needs: [test]
    runs-on: docker
    steps:
      - uses: actions/checkout@v4
      - name: Install ansible
        run: |
          apt update -y
          apt install python3-pip -y
          python3 -m pip install ansible
          python3 -m pip install ansible-lint

      - name: Populate relevant files
        run: |
          mkdir ~/.ssh
          echo "${{ secrets.C3LF_SSH_TESTING }}" > ~/.ssh/id_ed25519
          chmod 0600 ~/.ssh/id_ed25519
          ls -lah ~/.ssh
          command -v ssh-agent >/dev/null || ( apt-get update -y && apt-get install openssh-client -y )
          eval $(ssh-agent -s)
          ssh-add ~/.ssh/id_ed25519
          echo "andromeda.lab.or.it ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIDXPoO0PE+B9PYwbGaLo98zhbmjAkp6eBtVeZe43v/+T" >> ~/.ssh/known_hosts
          mkdir /etc/ansible
          echo "${{ secrets.C3LF_INVENTORY_TESTING }}" > /etc/ansible/hosts

      - name: Check ansible version
        run: |
          ansible --version

      - name: List ansible hosts
        run: |
          ansible -m ping Andromeda

      - name: Deploy testing
        run: |
          cd deploy/ansible
          ansible-playbook playbooks/deploy-c3lf-sys3.yml