name: .NET

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

jobs:
  BuildDebugAndTest:
    runs-on: ubuntu-latest
    name: "Run Tests"
    steps:
    - uses: actions/checkout@v4
    - name: update dotnet core sdk
      uses: actions/setup-dotnet@v4
      with:
        dotnet-version: 10.0.x
    - name: dotnet restore (first try)
      run: dotnet restore --ignore-failed-sources
    - name: dotnet restore (second try)
      run: dotnet restore --ignore-failed-sources
    - name: dotnet restore (last try)
      run: dotnet restore
    - name: dotnet build Debug
      run: dotnet build --no-restore --configuration Debug
    - name: dotnet test
      run: dotnet test --no-build --verbosity normal Tests/Tests.csproj

  BuildRelease:
    runs-on: ubuntu-latest
    name: "Build Release"
    steps:
    - uses: actions/checkout@v4
    - name: update dotnet core sdk
      uses: actions/setup-dotnet@v4
      with:
        dotnet-version: 10.0.x
    - name: dotnet restore (first try)
      run: dotnet restore --ignore-failed-sources
    - name: dotnet restore (second try)
      run: dotnet restore --ignore-failed-sources
    - name: dotnet restore (last try)
      run: dotnet restore
    - name: dotnet build Release
      run: dotnet build --no-restore --configuration Release

  ConfigTest:
    runs-on: ubuntu-latest
    outputs: 
      docker-username: ${{ steps.docker-username.outputs.defined }}
    steps:
      - id: docker-username
        env:
          DOCKER_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
        if: "${{ env.DOCKER_USERNAME }}"
        run: echo "::set-output  name=defined::true"

  DockerImage:
    needs: [BuildDebugAndTest, BuildRelease, ConfigTest]
    runs-on: ubuntu-latest
    name: "Build Docker image"
    if: (github.ref == 'refs/heads/master') && (needs.ConfigTest.outputs.docker-username == 'true')
    steps:
    - uses: actions/checkout@v4
      with:
        fetch-depth: 0
    - name: "clean build artifacts"
      run: git clean -dfx
    - name: update dotnet core sdk
      uses: actions/setup-dotnet@v4
      with:
        dotnet-version: 10.0.x
    - name: dotnet restore (first try)
      run: dotnet restore --ignore-failed-sources
    - name: dotnet restore (second try)
      run: dotnet restore --ignore-failed-sources
    - name: dotnet restore (last try)
      run: dotnet restore
    - name: dotnet build Release
      run: dotnet build --no-restore --configuration Release
    - name: Set up Docker Buildx
      uses: docker/setup-buildx-action@v3
    - name: Login to DockerHub
      uses: docker/login-action@v3
      with:
        username: ${{ secrets.DOCKERHUB_USERNAME }}
        password: ${{ secrets.DOCKERHUB_TOKEN }}
    - name: Generating build ID with offset for old builds
      env:
        NUM: ${{ github.run_number }}
      run: echo "GITHUB_RUN_NUMBER_WITH_OFFSET=$(($NUM+1050))" >> $GITHUB_ENV
    - name: Build and push
      uses: docker/build-push-action@v6
      with:
        context: .
        push: 'true'
        tags: rpcs3/discord-bot:${{ env.GITHUB_RUN_NUMBER_WITH_OFFSET }},rpcs3/discord-bot:release-latest,rpcs3/discord-bot:latest
