Fetch #359
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Fetch | |
on: | |
schedule: | |
- cron: '11 11 * * *' | |
workflow_dispatch: | |
inputs: | |
overwrite: | |
description: 'Overwrite the old data' | |
type: boolean | |
default: false | |
required: true | |
jobs: | |
prepare: | |
name: Prepare branch | |
runs-on: ubuntu-latest | |
outputs: | |
branch: ${{ steps.branch.outputs.branch }} | |
overwrite: ${{ inputs.overwrite || 'false' }} | |
permissions: | |
contents: write | |
steps: | |
- name: Set Timezone | |
run: sudo timedatectl set-timezone "Asia/Shanghai" | |
- uses: actions/checkout@v4 | |
- name: Prepare branch | |
id: branch | |
run: | | |
BRANCH=fetch-data/$(date +"%Y-%m-%d") | |
git checkout -B $BRANCH | |
git push -u -f origin $BRANCH | |
echo "branch=$BRANCH" >> $GITHUB_OUTPUT | |
bangumi: | |
name: Fetch Bangumi | |
runs-on: ubuntu-latest | |
permissions: | |
contents: write | |
needs: | |
- prepare | |
steps: | |
- name: Set Timezone | |
run: sudo timedatectl set-timezone "Asia/Shanghai" | |
- uses: actions/checkout@v4 | |
with: | |
ref: ${{ needs.prepare.outputs.branch }} | |
- name: Setup pnpm | |
uses: pnpm/[email protected] | |
- name: Setup node | |
uses: actions/setup-node@v4 | |
with: | |
node-version: 20.x | |
cache: pnpm | |
- name: Install and build | |
run: | | |
pnpm install | |
pnpm build | |
- name: Fetch Bangumi | |
run: | | |
npx tsx packages/bgmd/scripts/bangumi.ts --overwrite=${{ needs.prepare.outputs.overwrite }} | tee run.log | |
grep "^Error:" run.log > data/bangumi-error.log || true | |
# - name: Upload artifact | |
# uses: actions/upload-artifact@v3 | |
# with: | |
# name: bangumi-data | |
# path: | | |
# data/bangumi/ | |
# data/bangumi-*.log | |
- name: Push changes | |
run: | | |
TIMESTAMP=$(date +"%Y-%m-%d %T") | |
BRANCH=${{ needs.prepare.outputs.branch }} | |
if [[ -n $(git status -s) ]]; then | |
git config user.email "[email protected]" | |
git config user.name "GitHub Actions" | |
git add data | |
git commit -m "feat(bgmd): update bangumi data at $TIMESTAMP" | |
git push origin $BRANCH | |
fi | |
tmdb: | |
name: Fetch TMDB | |
runs-on: ubuntu-latest | |
permissions: | |
contents: write | |
needs: | |
- prepare | |
- bangumi | |
steps: | |
- name: Set Timezone | |
run: sudo timedatectl set-timezone "Asia/Shanghai" | |
- uses: actions/checkout@v4 | |
with: | |
ref: ${{ needs.prepare.outputs.branch }} | |
- name: Setup pnpm | |
uses: pnpm/[email protected] | |
- name: Setup node | |
uses: actions/setup-node@v4 | |
with: | |
node-version: 20.x | |
cache: pnpm | |
- name: Install and build | |
run: | | |
pnpm install | |
pnpm build | |
- name: Fetch TMDB | |
run: | | |
npx tsx packages/bgmd/scripts/tmdb.ts --overwrite=${{ needs.prepare.outputs.overwrite }} | tee run.log | |
if [ "${{ needs.prepare.outputs.overwrite }}" = true ] ; then | |
grep "^Info:" run.log > data/tmdb-info.log || true | |
grep "^Error:" run.log > data/tmdb-error.log || true | |
else | |
grep "^Info:" run.log >> data/tmdb-info.log || true | |
grep "^Error:" run.log > data/tmdb-error.log || true | |
fi | |
# - name: Upload artifact | |
# uses: actions/upload-artifact@v3 | |
# with: | |
# name: tmdb-data | |
# path: | | |
# data/tmdb/ | |
# data/tmdb-*.log | |
- name: Push changes | |
env: | |
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
run: | | |
TIMESTAMP=$(date +"%Y-%m-%d %T") | |
BRANCH=${{ needs.prepare.outputs.branch }} | |
if [[ -n $(git status -s) ]]; then | |
git config user.email "[email protected]" | |
git config user.name "GitHub Actions" | |
git add data | |
git commit -m "feat(bgmd): update TMDB data at $TIMESTAMP" | |
git push origin $BRANCH | |
fi | |
pr: | |
name: Create Pull Request | |
runs-on: ubuntu-latest | |
needs: | |
- prepare | |
- bangumi | |
- tmdb | |
permissions: | |
contents: write | |
pull-requests: write | |
steps: | |
- name: Set Timezone | |
run: sudo timedatectl set-timezone "Asia/Shanghai" | |
- uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
- name: Create Pull Request | |
env: | |
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
run: | | |
DATE=$(date +"%Y-%m-%d") | |
pr_list=$(gh pr list --label=data) | |
if [[ -n "$pr_list" ]]; then | |
echo "$pr_list" | awk '{print $1}' | xargs gh pr close -d | |
fi | |
if [[ $(git rev-list --count origin/main..origin/${{ needs.prepare.outputs.branch }}) -gt 0 ]]; then | |
gh pr create \ | |
--title "feat(bgmd): update data at $DATE" \ | |
--body "Update data" \ | |
--head "${{ needs.prepare.outputs.branch }}" \ | |
--label "data" | |
fi |