name: 编译caddy

on:
#每天北京时间11点 4点自动检测更新
  schedule:
   - cron: "0 3,20 * * *"
  workflow_dispatch:
    inputs:
      branch:
        description: '指定编译的版本号'
        required: false
        default: ''
      info:
        description: '发布Release的补充说明'
        required: false
        default: ''
      bj:
        description: '强制使用手动编译'
        required: true
        default: false
        type: boolean
permissions:
  contents: write
  actions: write
env:
  TZ: Asia/Shanghai
  BJ: ${{ github.event.inputs.bj }}
  BRANCH: ${{ github.event.inputs.branch }}
jobs:
 check:
    runs-on: ubuntu-latest
    outputs:
      tag: ${{ steps.getversion.outputs.tag }}
      ver: ${{ steps.getversion.outputs.ver }}
    steps:
      - name: 获取版本号
        id: getversion
        run: |
          ver=`curl https://api.github.com/repos/${GITHUB_REPOSITORY}/releases/latest | jq -r '.tag_name'`
          tag=`curl https://api.github.com/repos/caddyserver/caddy/releases/latest | jq -r '.tag_name'`
          echo -e "获取到最新的版本号：${tag}\n当前仓库最新版本号：${ver}"
          echo "tag=${tag}" >> $GITHUB_OUTPUT
          echo "ver=${ver}" >> $GITHUB_OUTPUT
          
      - name: 删除流程记录
        uses: Mattraks/delete-workflow-runs@main
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          retain_days: 0
          keep_minimum_runs: 0
      
 build: 
    needs: check
    if: ${{ github.event.inputs.bj == 'true' || (github.event.inputs.bj != 'true' && needs.check.outputs.tag != needs.check.outputs.ver) }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - TARGET: amd64
            OS: ubuntu-latest
            AARCH: amd64
            OOS: linux
            
          - TARGET: arm64
            OS: ubuntu-latest
            ARCH: arm64
            OOS: linux
            
          - TARGET: mips
            OS: ubuntu-latest
            SOFT: softfloat
            ARCH: mips
            OOS: linux
            
          - TARGET: mips64
            OS: ubuntu-latest
            SOFT: softfloat
            ARCH: mips64
            OOS: linux
            
          - TARGET: mips64le
            OS: ubuntu-latest
            SOFT: softfloat
            ARCH: mips64le
            OOS: linux
            
          - TARGET: mipsel
            OS: ubuntu-latest
            SOFT: softfloat
            ARCH: mipsle
            OOS: linux
            
          - TARGET: ppc64le
            OS: ubuntu-latest
            ARCH: ppc64le
            OOS: linux

          - TARGET: s390x
            OS: ubuntu-latest
            ARCH: s390x
            OOS: linux
            
          - TARGET: arm
            OS: ubuntu-latest
            ARM: 5
            ARCH: arm
            OOS: linux
            
          - TARGET: armv5l
            OS: ubuntu-latest
            ARM: 5
            ARCH: arm
            OOS: linux
            
          - TARGET: armv6
            OS: ubuntu-latest
            ARM: 6
            ARCH: arm
            OOS: linux

          - TARGET: armv7l
            OS: ubuntu-latest
            ARM: 7
            ARCH: arm
            OOS: linux

          - TARGET: freebsd-x86-64
            OS: ubuntu-latest
            ARCH: amd64
            OOS: freebsd
            
    runs-on: ${{ matrix.OS }}
    env:
      TARGET: ${{ matrix.TARGET }}
      OS: ${{ matrix.OS }}
      OOS: ${{ matrix.OOS }}
      SOFT: ${{ matrix.SOFT }}
      ARM: ${{ matrix.ARM }}
      ARCH: ${{ matrix.ARCH }}
    steps:
      - name: 获取最新Go
        run: |
          latest_version=$(curl -L -k -s https://golang.org/dl/?mode=json | grep -m 1 -o '"version": "[^"]*' | sed 's/"version": "//' | tr -d 'go" ')
          echo "go版本=$latest_version"
          echo "gotag=$latest_version" >> $GITHUB_ENV
      - name: 安装go
        uses: actions/setup-go@v5
        with:
          go-version: ${{ env.gotag }}
      -
        name: 检查代码仓库
        uses: actions/checkout@v4
      - name: 安装 UPX
        uses: crazy-max/ghaction-upx@v3
        with:
          version: latest
          install-only: true
      - 
        name: 编译
        run: |
          go install github.com/caddyserver/xcaddy/cmd/xcaddy@latest
          if [[ "${{ env.BJ }}" == "true" ]]; then
            echo "强制进行编译..."
            if [[ -n "${{ github.event.inputs.branch }}" ]]; then
                echo "使用手动指定的版本：${{ github.event.inputs.branch }}"
                TAG=${{ github.event.inputs.branch }}
                echo "TAG=$TAG" >> $GITHUB_ENV
            else
                echo "使用自动获取的版本：${{ needs.check.outputs.tag }}"
                TAG=${{ needs.check.outputs.tag }}
                echo "TAG=$TAG" >> $GITHUB_ENV
            fi
          else
            echo "使用自动获取的版本：${{ needs.check.outputs.tag }}"
            TAG=${{ needs.check.outputs.tag }}
            echo "TAG=$TAG" >> $GITHUB_ENV
          fi
          echo "开始拉取 ${TAG} 源码..."
          git clone -b ${TAG} https://github.com/caddyserver/caddy /opt/mycaddy
          curl -Lko /opt/browse.html https://github.com/lmq8267/caddy/raw/refs/heads/main/browse.html || wget --no-check-certificate -O /opt/browse.html https://github.com/lmq8267/caddy/raw/refs/heads/main/browse.html
          
          cp -rf /opt/browse.html /opt/mycaddy/modules/caddyhttp/fileserver/browse.html
          #sed -i 's/caddy\.Log().Warn("failed to set GOMAXPROCS", zap.Error(err))/_ = err/g' /opt/mycaddy/cmd/main.go
          sed -i 's/caddy\.Log().Warn("failed to set GOMAXPROCS", zap.Error(err))/runtime.GOMAXPROCS(4)/g' /opt/mycaddy/cmd/main.go
          sed -i 's/full += " " + module.Sum/full = module.Version/g' /opt/mycaddy/caddy.go
          sed -i 's/full += " => " + module.Replace.Path/full = module.Version/g' /opt/mycaddy/caddy.go
          sed -i 's/simple = module.Replace.Version + "_custom"/simple = module.Version/g' /opt/mycaddy/caddy.go
          sed -i 's/full += "@" + module.Replace.Version/full = module.Version/g' /opt/mycaddy/caddy.go
          sed -i 's/full += " " + module.Replace.Sum/full = module.Version/g' /opt/mycaddy/caddy.go
          
          echo "开始构建"
          mkdir -p /opt/caddy
          cd /opt/caddy
          sudo timedatectl set-timezone "Asia/Shanghai"
          GOOS=${OOS} GOARCH=${ARCH} GOMIPS=${SOFT} GOARM=${ARM} xcaddy build \
          --with github.com/caddyserver/caddy/v2=/opt/mycaddy \
          --with github.com/mholt/caddy-webdav \
          --with github.com/aksdb/caddy-cgi/v2 \
          --with github.com/abiosoft/caddy-exec
          file caddy
          du -k caddy
          mv caddy caddy-${TARGET}
          if [[ $TARGET != mips64 ]] && [[ $TARGET != mips64le ]] && [[ $TARGET != s390x ]] && [[ $TARGET != freebsd-x86-64 ]] ;then
          cp caddy-${TARGET} caddy-${TARGET}-upx
          upx --lzma --best caddy-${TARGET}-upx
          du -k caddy-${TARGET}-upx
          fi
          
          echo "build_time=$(date '+%Y-%m-%d %H:%M:%S')" >> $GITHUB_ENV
      - name: 上传
        uses: actions/upload-artifact@v4
        with:
          name: caddy-${{ github.event.inputs.branch }}-${{ matrix.TARGET }}
          path: /opt/caddy/
      - name: 发布
        uses: softprops/action-gh-release@v2
        with:
            token: ${{ github.token }}
            body: |
             > ### 编译时间 ：${{ env.build_time }}

              caddy-CPU架构-upx 为upx压缩文件
              
              添加了三个插件：
              **[mholt/caddy-webdav](https://github.com/mholt/caddy-webdav)**
              **[aksdb/caddy-cgi](https://github.com/aksdb/caddy-cgi)**
              **[abiosoft/caddy-exec](https://github.com/abiosoft/caddy-exec)**

              查看集成的插件列表命令： `caddy list-modules -s`
              
              **[官方更新说明](https://github.com/caddyserver/caddy/releases)**

              ${{ github.event.inputs.info }}
              
            tag_name: ${{ env.TAG }}
            files: /opt/caddy/*

 keepalive-workflow:
    name: 工作流保活
    runs-on: ubuntu-latest
    permissions:
      actions: write
      contents: write
    steps:
      - uses: actions/checkout@v4
      - uses: gautamkrishnar/keepalive-workflow@v2
        #with:
          #use_api: true
