Skip to content

main(greasyfork).user.js and locals(greasyfork).js update #40

main(greasyfork).user.js and locals(greasyfork).js update

main(greasyfork).user.js and locals(greasyfork).js update #40

name: main(greasyfork).user.js and locals(greasyfork).js update
on:
schedule:
# 每周四的UTC时间16点(北京时间周五凌晨0点)
- cron: "0 16 * * 4"
workflow_dispatch:
jobs:
update_version:
runs-on: ubuntu-latest
environment: github-pages
steps:
- name: Generate a token
id: generate_token
uses: actions/create-github-app-token@v1
with:
app-id: ${{ vars.APP_ID }}
private-key: ${{ secrets.APP_PRIVATE_KEY }}
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Sync update locals(greasyfork).js
run: |
# 获得最近以`main.user.js Update to version`开头的提交的引用
ref=$(git log --grep="^main.user.js Update to version" -n 1 --pretty=format:"%H")
# 获取该提交中`locals.js`的文件时间, 并设置环境变量
locals_js_time=$(git log -1 --format=%at $ref -- locals.js)
echo "locals_js_time=$locals_js_time" >> $GITHUB_ENV
# 从该提交中提取文件`locals.js`并覆写到本地文件`locals(greasyfork).js`
git show $ref:locals.js > "locals(greasyfork).js"
git diff --quiet --exit-code "locals(greasyfork).js" || \
echo "LOCALS_JS_IS_CHANGED=true" >> $GITHUB_ENV
- name: Update version in main(greasyfork).user.js
if: |
${{ env.LOCALS_JS_IS_CHANGED == 'true' }}
run: |
# 使用`locals.js`的文件日期为词库版本号
locals_js_date=$(TZ='Asia/Shanghai' date -d "@${{ env.locals_js_time }}" +'%Y-%m-%d')
sed -i -E "s/(@version\s*[0-9]+\.[0-9]+\.[0-9]+)(-[0-9]{4}-[0-9]{2}-[0-9]{2})?/\
\1-$locals_js_date/" "main(greasyfork).user.js"
git diff --quiet --exit-code "main(greasyfork).user.js" || \
echo "MAIN_USER_JS_IS_CHANGED=true" >> $GITHUB_ENV
- name: Commit and push locals(greasyfork).js and main(greasyfork).user.js
if: ${{ env.LOCALS_JS_IS_CHANGED == 'true' &&
env.MAIN_USER_JS_IS_CHANGED == 'true' }}
env:
GITHUB_TOKEN: ${{ steps.generate_token.outputs.token }}
APP_SLUG: ${{ steps.generate_token.outputs.app-slug }}
run: |
bash script/ci_commit_with_signature.sh \
-R "${{ github.repository }}" \
-B "${{ github.ref_name }}" \
-P "${{ github.sha }}" \
-F "locals(greasyfork).js, main(greasyfork).user.js" \
-h "main(greasyfork).user.js Update on $(TZ='Asia/Shanghai' date +'%Y-%m-%d')"