name: Docker Image CI
on:
  push:
    branches: 
      - dev

jobs:
  build:
    runs-on: gitea-runner-group-candlelight
    container: 
      image: ${{ vars.HARBOR_URL }}/candlelight/action_builder:v0.0.1
    steps:
    - name: Checkout code
      uses: actions/checkout@v4
    - name: docker login
      env: 
        HARBOR_USERNAME: ${{ secrets.HARBOR_ROBOT }}
        HARBOR_PASSWORD: ${{ secrets.HARBOR_ROBOT_SECRET }}
        HARBOR_URL: ${{ vars.HARBOR_URL }}
      run: docker login ${HARBOR_URL} -u ${HARBOR_USERNAME} -p ${HARBOR_PASSWORD}
    - name: Build and push Docker images
      env:
        HARBOR_URL: ${{ vars.HARBOR_URL }}
        TAG: ${{ github.sha }}
        REPOSITORY: ${{ github.repository }}
      run: |
        ROOT_DIR=$(pwd)
        IMAGE_NAME="${HARBOR_URL}/testing/$REPOSITORY:${TAG}"
        echo "Building image: ${IMAGE_NAME}" 
        docker build -t ${IMAGE_NAME} .
        echo "Pushing image: ${IMAGE_NAME}" 
        docker push ${IMAGE_NAME}
        echo "Successfully pushed: ${IMAGE_NAME}"
        docker rmi ${IMAGE_NAME}
        echo "cleaned up local image"

