name: Build and Publish Docker Image

on:
  push:
    branches:
      - main 
    paths:
      - '.github/runner/docker/**' 
  schedule:
    - cron: '0 0 * * *'  

jobs:
  build:
    if: github.repository == 'community-scripts/ProxmoxVE'
    runs-on: ubuntu-latest #To ensure it always builds we use the github runner with all the right tooling

    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Log in to GHCR
        uses: docker/login-action@v2
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Build Docker image
        run: |
          repo_name=${{ github.repository }}  # Get repository name
          repo_name_lower=$(echo $repo_name | tr '[:upper:]' '[:lower:]')  # Convert to lowercase
          docker build -t ghcr.io/$repo_name_lower/gh-runner-self:latest -f .github/runner/docker/gh-runner-self.dockerfile .
  
      - name: Push Docker image to GHCR
        run: |
          repo_name=${{ github.repository }}  # Get repository name
          repo_name_lower=$(echo $repo_name | tr '[:upper:]' '[:lower:]')  # Convert to lowercase
          docker push ghcr.io/$repo_name_lower/gh-runner-self:latest
