name: Auto Update oxidns Version on: schedule: # 每天 UTC 02:00 检查(北京时间 10:00) - cron: '0 2 * * *' workflow_dispatch: # 允许手动触发 inputs: refresh_rules: description: '即使版本没变也重新下载首启规则文件(files/seed)' type: boolean default: false permissions: contents: write jobs: update: runs-on: ubuntu-latest steps: - name: Checkout repository uses: actions/checkout@v4 with: # 使用 GITHUB_TOKEN 推送;如果分支有保护规则需换成 PAT token: ${{ secrets.GITHUB_TOKEN }} fetch-depth: 0 - name: Get latest release tag from upstream id: upstream run: | LATEST_TAG=$(curl -sL \ -H "Accept: application/vnd.github+json" \ https://api.github.com/repos/svenshi/oxidns/releases/latest \ | jq -r '.tag_name') if [[ -z "$LATEST_TAG" || "$LATEST_TAG" == "null" ]]; then echo "::error::Failed to fetch latest release tag" exit 1 fi NEW_VERSION="${LATEST_TAG#v}" echo "latest_tag=${LATEST_TAG}" >> "$GITHUB_OUTPUT" echo "new_version=${NEW_VERSION}" >> "$GITHUB_OUTPUT" echo "Latest tag: ${LATEST_TAG} (version: ${NEW_VERSION})" - name: Get current version from Makefile id: current run: | CURRENT_VERSION=$(grep -oP '^PKG_VERSION:=\K.*' Makefile) echo "current_version=${CURRENT_VERSION}" >> "$GITHUB_OUTPUT" echo "Current version: ${CURRENT_VERSION}" - name: Compare versions id: compare run: | if [ "${{ steps.current.outputs.current_version }}" = "${{ steps.upstream.outputs.new_version }}" ]; then echo "update_needed=false" >> "$GITHUB_OUTPUT" echo "Already up to date." else echo "update_needed=true" >> "$GITHUB_OUTPUT" echo "Update needed: ${{ steps.current.outputs.current_version }} -> ${{ steps.upstream.outputs.new_version }}" fi - name: Get commit hash for the release tag if: steps.compare.outputs.update_needed == 'true' id: tag_commit run: | TAG="${{ steps.upstream.outputs.latest_tag }}" REF_JSON=$(curl -sL \ -H "Accept: application/vnd.github+json" \ "https://api.github.com/repos/svenshi/oxidns/git/ref/tags/${TAG}") OBJECT_TYPE=$(echo "$REF_JSON" | jq -r '.object.type') COMMIT_SHA=$(echo "$REF_JSON" | jq -r '.object.sha') # annotated tag 需要再解析一层 if [[ "$OBJECT_TYPE" == "tag" ]]; then COMMIT_SHA=$(curl -sL \ -H "Accept: application/vnd.github+json" \ "https://api.github.com/repos/svenshi/oxidns/git/tags/${COMMIT_SHA}" \ | jq -r '.object.sha') fi if [[ -z "$COMMIT_SHA" || "$COMMIT_SHA" == "null" ]]; then echo "::error::Failed to fetch commit hash for tag ${TAG}" exit 1 fi echo "commit_hash=${COMMIT_SHA}" >> "$GITHUB_OUTPUT" echo "Commit hash: ${COMMIT_SHA}" - name: Run update script if: steps.compare.outputs.update_needed == 'true' run: | chmod +x update.sh ./update.sh \ "${{ steps.upstream.outputs.new_version }}" \ "${{ steps.tag_commit.outputs.commit_hash }}" # ---------------------------------------------------------------------- # 解决首次启动时缺少 antiad.txt / geosite.dat / geoip.dat 等规则文件的问题: # 读取 files/config.yaml 中 download 插件的 downloads 列表(url / dir / filename), # 把这些文件预先下载到 files/seed//,随包一起安装, # 这样首次启动时规则文件已经存在,不依赖 oxidns 自己联网下载。 # 仅在版本更新或手动勾选 refresh_rules 时执行,避免每天提交大文件。 # ---------------------------------------------------------------------- - name: Seed rule files from files/config.yaml if: steps.compare.outputs.update_needed == 'true' || github.event.inputs.refresh_rules == 'true' run: | set -euo pipefail CONF=files/config.yaml if [ ! -f "$CONF" ]; then echo "::error::$CONF not found" exit 1 fi mapfile -t ITEMS < <(yq -o=json '.' "$CONF" | jq -r ' .plugins[]? | select(.type == "download") | .args.downloads[]? | [ .url, (.dir // "/etc/oxidns/rules"), (.filename // (.url | split("/") | last)) ] | @tsv') if [ "${#ITEMS[@]}" -eq 0 ]; then echo "::error::No download entries found in $CONF" exit 1 fi rm -rf files/seed for ITEM in "${ITEMS[@]}"; do IFS=$'\t' read -r URL DIR NAME <<< "$ITEM" DEST="files/seed/${DIR#/}" mkdir -p "$DEST" echo "Downloading ${URL} -> ${DEST}/${NAME}" curl -fL --retry 3 --retry-delay 5 \ --connect-timeout 20 --max-time 300 \ -o "${DEST}/${NAME}" "${URL}" if [ ! -s "${DEST}/${NAME}" ]; then echo "::error::Downloaded file is empty: ${NAME}" exit 1 fi done echo "Seed files:" find files/seed -type f -exec ls -lh {} \; - name: Commit and push to current branch if: steps.compare.outputs.update_needed == 'true' || github.event.inputs.refresh_rules == 'true' run: | git config user.name "github-actions[bot]" git config user.email "41898282+github-actions[bot]@users.noreply.github.com" git add Makefile if [ -d files/seed ]; then git add -A files/seed fi if git diff --cached --quiet; then echo "No changes to commit." exit 0 fi if [ "${{ steps.compare.outputs.update_needed }}" = "true" ]; then MSG="oxidns: update to ${{ steps.upstream.outputs.new_version }} [skip ci]" else MSG="oxidns: refresh seed rule files [skip ci]" fi git commit -m "$MSG" git push origin "HEAD:${{ github.ref_name }}"