name: BuildImage

on:
  push:
    branches:
      - main
      # tags:
      #   - '*'

jobs:
  build-image:
    runs-on: ubuntu-latest
    container:
      # 使用这个镜像，不然Docker无法打包镜像
      image: catthehacker/ubuntu:act-latest

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

      - name: Gen Tags
        id: gen_tags
        uses: docker/metadata-action@v5
        with:
          images: gitee.ltd/lxh/wechat-robot
          flavor: |
            latest=true
          tags: |
            type=raw,value={{date 'YYYYMMDD' tz='Asia/Shanghai'}}

      - name: Print Tags
        run: |
          echo "${{ steps.gen_tags.outputs.tags }}"
          echo "----------------- labels -----------------"
          echo "${{ steps.meta.outputs.labels }}"

      - name: Login to Repository
        uses: docker/login-action@v2
        with:
          registry: gitee.ltd
          username: ${{ secrets.USERNAME }}
          password: ${{ secrets.PASSWORD }}

      - name: Build image
        uses: docker/build-push-action@v6
        with:
          push: true
          no-cache: true
          tags: ${{ steps.gen_tags.outputs.tags }}
          labels: ${{ steps.gen_tags.outputs.labels }}

      - name: Clear images
        run: |
          docker images | grep "gitee.ltd/lxh/wechat-robot"
          docker rmi -f $(docker images | grep "gitee.ltd/lxh/wechat-robot" | awk '{print $3}')
