name: build-image
on:
  push:
    branches: [main]

jobs:
  build-image:
    runs-on: [ubuntu-latest]
    env:
      DOTNET_INSTALL_DIR: "/home/runner"
    permissions:
      contents: write
      packages: write
    steps:
      - name: 'Cleanup build folder'
        run: |
          rm ./* -r || true
          rm ./.??* -r  || true
        
      - uses: actions/setup-dotnet@v3
        with:
          dotnet-version: 8.0.x

      - name: Set up GitVersion
        uses: gittools/actions/gitversion/setup@v0.9.15
        with:
          versionSpec: '5.x'
    
      - name: Checkout code
        uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - name: Execute GitVersion
        uses: gittools/actions/gitversion/execute@v0.9.15
        with:
          useConfigFile: true
          configFilePath: GitVersion.yml

      - name: login into registry
        run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login https://ghcr.io -u $ --password-stdin

      - name: Build image
        env:
          dockerfile: ./src/comissions.app.api/Dockerfile
          docker_image_name: ghcr.io/comissions-app/core-api
        run: |
          docker build --build-arg NUGET_SOURCE=https://nuget.pkg.github.com/data443/index.json --build-arg NUGET_API_KEY=${{ secrets.PACKAGE_TOKEN }} -t ${{ env.docker_image_name }}:${{ env.GitVersion_MajorMinorPatch }} -f ${{ env.dockerfile }} --force-rm .
          docker tag ${{ env.docker_image_name }}:${{ env.GitVersion_MajorMinorPatch }} ${{ env.docker_image_name }}:latest
          docker push --all-tags ${{ env.docker_image_name }}

      - name: Tag branch
        run: |
          git tag ${{ env.GitVersion_MajorMinorPatch }}
          git push origin ${{ env.GitVersion_MajorMinorPatch }}
