name: Build Docker Image
run-name: Build Docker Image 
on: [push]

jobs:
  Build Docker Image:
    runs-on: ubuntu-latest
    steps:
      - name: Check out repository code
        uses: actions/checkout@v4
      - name: Install system dependencies
        run: |
          # change mirror to kku.ac.th for faster package downloading
          sudo sed -i 's@http://archive.ubuntu.com@http://mirror.kku.ac.th@g' /etc/apt/sources.list.d/ubuntu.sources
          sudo apt update && sudo apt install nodejs npm curl -y
      - name: Build docker image
        run: |
          mv Dockerfile ../
          mv entrypoint ../
          cd ../
          set +e
          docker rm $(docker stop $(docker ps -a -q --filter ancestor=accounting-api:latest --format="{{.ID}}"))
          set -e
          docker image rm -f accounting-api:latest
          docker build . -t accounting-api:latest
      - name: Restart compose
        run: |
          curl -X POST http://10.9.0.0:3000/run \
            -H "Content-Type: application/json" \
            -H "X-SECRET: ${{ secrets.EXPRESS_SECRET }}" \
            -d '{"command": "cd $HOME/backend-development-kickstarter && ddd && ddud"}'