将JS写的抓取 Altmetric 热门预印本,与 bioRxiv API 结合后储存在 csv 文件中的过程,部署在 Github Action 中。官方文档写的顺序都有点混乱,网上找到的教程又经常都比较老了,因此自己记录一个。

新建Github仓库

新建一个public仓库,名叫TRxiv,并将远程仓库git clone到本地。

git clone git@github.com:Yorks0n/TRxiv.git

cd TRxiv

创建一个动作元数据文件

要让仓库里能被以Action的形式直接调用,需要在根目录中创建一个action.yml配置文件,可以在这个文件中指定Action的输入和输出,调用的参数及运行环境

# action.yml
name: 'trxiv'
description: 'Tracking popular bioRxiv and medRxiv preprints'

runs:
  using: 'node16'
  main: 'dist/index.js'

准备运行的代码

手动将写好的JS脚本拷贝进来,完整代码在此 Yorkson/TRxiv

# 在这里初始化一下npm
npm init -y

准备一个.gitignore文件,防止在推送的时候把不必要的文件放到储存库,可以用下面这个工具,或者自己写一下,比如这里就可能有node_modules

https://www.toptal.com/developers/gitignore

文件推送到远程仓库

然后push到远程仓库

git add .
git commit -m "Initialize"
git push

打包软件

因为前面把node_modules 从上传的文件列表中忽略了,但脚本index.js内有些依赖的包,所以最好把软件和依赖打包在一起,官方推荐用ncc

npm install @vercel/ncc

然后对index.js 进行打包

ncc build index.js -o dist

打包产物会存放于dist/index.js

创建工作流

在本地创建.github/workflows/main.yml,这个文件实际确定了 Github Action 的行为,此yml格式文件一定要注意正确的缩进。

配置文件内容:

# main.yml
## 动作的名称
name: 'TRxiv update'

# 触发条件
## 条件1,代码push进仓库
## 条件2,定时任务,在UTC 10和22点,即北京时间18点和6点运行
on:
  push:
  schedule:
    - cron: '0 10,22 * * *'

# jobs 定义了要执行的一系列任务 

jobs:
  ## 这个job的名称
  TRxiv_update_job:
    ## runs-on 内置运行任务的server类型
    runs-on: ubuntu-latest
    name: Update the csv file
    steps: 
      ## 必须有这一步,让运行的文件夹成为一个git repo
      - uses: actions/checkout@v3
      ## 运行脚本,指向我们的repo
      - name: 'run script'
        uses: Yorks0n/TRxiv@main
      ## 运行完后,把运行产生在本地的data.csv更新回仓库中
      - name: Commit files
        run: |
         git config --local user.email "github email adress"
         git config --local user.name "github user name"
         git pull
         git add README.md
         git add data.csv
         git commit -m "update data.csv"
        shell: bash
      ## 使用秘钥确认具有上传权限,同时以秘钥方式提交不会触发push action,避免陷入loop
      - name: Push changes
        uses:  ad-m/github-push-action@master
        with:
         github_token: ${{ secrets.MY_GIT_TOKEN }}
         branch: main

再次将文件推送到远程仓库

git add .
git commit -m "Update Workflow"
git push

后续更新

每次想更新修改前,先在目录中把最新版的环境拉下来

git pull

修改过脚本后,要重新打包用到的代码ncc build index.js --license licenses.txt

然后再提交回仓库

参考

How to Build Your First JavaScript GitHub Action

如何使用 Github Actions 自动抓取每日必应壁纸?

Permission denied to github-actions[bot]

Creating a JavaScript action - GitHub Docs