-
Notifications
You must be signed in to change notification settings - Fork 7
87 lines (78 loc) · 2.98 KB
/
adrules_update.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
name: AdRules_update
on:
push:
branches:
- main
paths:
- '*.txt'
workflow_dispatch:
jobs:
update:
runs-on: ubuntu-latest
steps:
- name: Set up time zone
run: |
sudo timedatectl set-timezone Asia/Shanghai
- name: Checkout
uses: actions/checkout@main
- name: Get last commit SHA
id: last_commit
run: |
echo "sha=$(git rev-parse HEAD)" >> $GITHUB_ENV
- name: Get changed files
id: file_list
run: |
COMMIT=${{ env.sha }}
URL="https://api.github.com/repos/${{ github.repository }}/compare/${COMMIT}^...${COMMIT}"
FILES=$(curl -s -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" $URL | jq -r '.files[] | select(.filename | test("^.*\\.txt$")) | .filename')
if [ -z "$FILES" ]; then
FILES=$(find . -maxdepth 1 -name '*.txt')
fi
echo FILES=$FILES >> $GITHUB_OUTPUT
- name: Update files
run: |
echo "Updating files..."
FILES="${{ steps.file_list.outputs.FILES }}"
for file in $FILES; do
sed -i -e 's/>/>/g' -e 's/!/!/g' $file
sed -i -E '/^\s*$/! { /^[^!#]/ { /^.*\^$/! s/$/\^/ } }' $file
current_date=$(TZ=Asia/Shanghai date '+%Y%m%d%H%M')
count=$(grep -v -e '^$' -e '^!' -e '^#' $file | wc -l)
count_with_unit="${count}"
sed -i "s/^! Version: .*/! Version: $current_date/" $file
sed -i "s/^! Total lines: .*/! Total lines: $count_with_unit/" $file
temp_file=$(mktemp)
domain_list=""
while IFS= read -r line; do
if [[ ! $line =~ ^[!#[:space:]]*$ && $line =~ ^([a-zA-Z0-9.-]+|\|\||@@) ]]; then
domain_list="$domain_list"$'\n'"$line"
else
if [[ $domain_list ]]; then
echo "$domain_list" | sort | sed '/^$/d' >> "$temp_file"
domain_list=""
fi
echo "$line" >> "$temp_file"
fi
done < "$file"
if [[ $domain_list ]]; then
echo "$domain_list" | sort | sed '/^$/d' >> "$temp_file"
fi
mv "$temp_file" "$file"
done
- name: 更新 README
run: |
echo "Updating README.md..."
new_date=$(TZ='Asia/Shanghai' date +'%Y年%m月%d日%H:%M:%S')
count_1=$(grep -v -e '^$' -e '^!' -e '^#' *.txt | wc -l)
sed -i "s/最近更新: .*/最近更新: $new_date/" readme.md
sed -i "s/补充数量: .*/补充数量: $count_1条/" readme.md
- name: Commit And Push
run: |
git config --global user.email "[email protected]" && git config --global user.name "Bot"
git add . && git commit -m "日常更新$(date +'%Y-%m-%d %H:%M')"
git push
- name: Cleanup Workflow
uses: Mattraks/delete-workflow-runs@main
with:
retain_days: 1
keep_minimum_runs: 2