name: CI-Docker

on:
  push:
    branches:
      - master
  repository_dispatch:
    types: [run_build]


jobs:
  build:
    runs-on: ubuntu-latest
  
    steps:
    - uses: actions/checkout@v4

    - name: Login to Github registry
      uses: docker/login-action@v3
      with:
        username: ${{ github.actor }}
        password:  ${{ secrets.GITHUB_TOKEN }}
        registry: ghcr.io
    
    - uses: docker/build-push-action@v5
      with:
        context: .
        push: true
        tags: ghcr.io/${{ github.repository }}:latest
        build-args: |
          BASE_DOCKER_IMAGE=ghcr.io/${{ github.repository_owner }}/psptoolchain:latest
    
    - name: Send Compile action
      run: |
        export DISPATCH_ACTION="$(echo run_build)"
        echo "NEW_DISPATCH_ACTION=$DISPATCH_ACTION" >> $GITHUB_ENV

    - name: Repository Dispatch to psp-packages
      uses: peter-evans/repository-dispatch@v2
      with:
        repository: ${{ github.repository_owner }}/psp-packages
        token: ${{ secrets.DISPATCH_TOKEN }}
        event-type: ${{ env.NEW_DISPATCH_ACTION }}
        client-payload: '{"ref": "refs/heads/master"}'
    
    - name: Repository Dispatch to psplinkusb
      uses: peter-evans/repository-dispatch@v2
      with:
        repository: ${{ github.repository_owner }}/psplinkusb
        token: ${{ secrets.DISPATCH_TOKEN }}
        event-type: ${{ env.NEW_DISPATCH_ACTION }}
        client-payload: '{"ref": "refs/heads/master"}'
