Deploy #1891
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: Deploy | |
on: | |
schedule: | |
- cron: "0 4,16 * * *" | |
workflow_dispatch: | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
strategy: | |
matrix: | |
node-version: | |
- 16.x | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
with: | |
# 令 GitHub 在 git clone 和 git checkout 后「忘记」使用的 credentials。 | |
# 如果之后需要以另外的身份(如你的 GitHub Bot)执行 git push 操作时(如部署到 GitHub Pages),必须设置为 false。 | |
persist-credentials: false | |
- name: Use Node.js ${{ matrix.node-version }} | |
uses: actions/setup-node@v4 | |
with: | |
node-version: ${{ matrix.node-version }} | |
# 缓存 node_modules,缓存机制参见 GitHub 文档:https://help.github.com/en/actions/configuring-and-managing-workflows/caching-dependencies-to-speed-up-workflows | |
- name: Cache node_modules | |
uses: actions/cache@v4 # 使用 GitHub 官方的缓存 Action。 | |
env: | |
cache-name: blogroll-node-modules | |
with: | |
path: node_modules | |
key: ${{ runner.os }}-${{ env.cache-name }}-${{ hashFiles('package-lock.json') }} # 使用 package-lock.json 的 Hash 作为缓存的 key。也可以使用 package.json 代替 | |
# Wrangler 在构建时会在 workers-site 目录下执行 npm i,因此也要缓存这里的 node_modules | |
- name: Cache workers-site/node_modules | |
uses: actions/cache@v4 | |
env: | |
cache-name: workers-site-node-modules | |
with: | |
path: workers-site/node_modules | |
key: ${{ runner.os }}-${{ env.cache-name }}-${{ hashFiles('workers-site/package-lock.json') }} | |
- run: npm i # 执行 Blogroll 的依赖安装 | |
- run: npm run update # 相当于 node update_files.js,从 Seatable 更新数据到 README.md | |
env: | |
SEATABLE_API_TOKEN: ${{ secrets.SEATABLE_API_TOKEN }} | |
- name: Commit and push if README.md changed | |
env: | |
DEPLOY_REPO: [email protected]:nju-lug/blogroll.git | |
DEPLOY_BRANCH: main | |
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }} | |
run: | | |
if [ -n "$(git status --porcelain README.md)" ]; then | |
git config --local user.email "github-actions[bot]@users.noreply.github.com" | |
git config --local user.name "github-actions[bot]" | |
git add README.md | |
git commit -m "update README.md from github actions" | |
mkdir -p ~/.ssh | |
echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa | |
chmod 600 ~/.ssh/id_rsa | |
ssh-keyscan github.com >> ~/.ssh/known_hosts | |
git push $DEPLOY_REPO HEAD:$DEPLOY_BRANCH | |
else | |
echo "No changes detected" | |
fi | |
- run: npm run gen # 相当于 node index.js,生成 opml.xml,opml.json 和 data.json | |
- run: npm run build | |
- name: Deploy to Cloudflare Workers | |
uses: cloudflare/wrangler-action@v3 | |
with: | |
apiToken: ${{ secrets.CF_WORKERS_TOKEN }} # 前一步设置的 Secrets 的名称 | |
# Wrangler Action 也支持使用传统的 Global API Token + Email 的鉴权方式,但不推荐 |