name: Docker Build on ZeroTier

on:
  push:
    branches:
      - master  # 当 master 分支有推送时触发
  schedule:
    - cron: '0 */6 * * *'  # 每6小时检查一次
  workflow_dispatch:  # 允许手动触发

jobs:
  check-and-build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Get latest Tailscale tag
        id: get_tag
        run: |
          LATEST_TAG=$(curl -s https://api.github.com/repos/zerotier/ZeroTierOne/tags | jq -r '.[0].name')
          echo "LATEST_TAG=${LATEST_TAG}" >> $GITHUB_ENV
          echo "latest_tag=${LATEST_TAG}" >> $GITHUB_OUTPUT

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3

      - name: Login to Docker Hub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_PASSWORD }}

      - name: Build and push tagged version
        uses: docker/build-push-action@v5
        with:
          context: .
          push: true
          tags: |
            ${{ secrets.DOCKERHUB_USERNAME }}/zerotier-planet-test:latest
            ${{ secrets.DOCKERHUB_USERNAME }}/zerotier-planet-test:${{ steps.get_tag.outputs.latest_tag }}
          build-args: |
            DERP_VERSION=${{ steps.get_tag.outputs.latest_tag }} 