name: Docker Image Deploy

on:
  push:
    branches: [ "master" ]
    paths-ignore:
      - '.github/workflows/CI.yml'
      - '.github/workflows/github-pages.yaml'
      - 'doc/**'
      - docker-compose.yml
      - log-cfg.yml
      - README.md
  workflow_dispatch:

concurrency:
  group: "docker"
  cancel-in-progress: false

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout
      uses: actions/checkout@v4
      with:
        fetch-depth: 0
        submodules: true
    - name: Get cache key
      id: cache_key
      run: python3 scripts/get_cache_key.py --docker
    - name: Login to Docker Hub
      uses: docker/login-action@v3
      with:
        username: lifegpc
        password: ${{ secrets.DOCKER_PASSWORD }}
    - name: Extract metadata for Docker
      id: meta
      uses: docker/metadata-action@v5
      with:
        images: lifegpc/pixiv_downloader
    - name: Switch docker driver
      run: docker buildx create --use
    - name: Build and push Docker image
      uses: docker/build-push-action@v6
      with:
        context: .
        cache-from: type=gha,scope=docker-${{ steps.cache_key.outputs.cache_key }}
        cache-to: type=gha,mode=max,scope=docker-${{ steps.cache_key.outputs.cache_key }}
        push: true
        tags: ${{ steps.meta.outputs.tags }}
        labels: ${{ steps.meta.outputs.labels }}
