利用github 实现定时执行某任务

1,728 阅读1分钟

前言

之前用node写了个程序,想要他每天定时执行,但是不想买云服务器也不想自己电脑当服务器天天开着机,无意中发现github可以实现该需求。

github怎么实现

利用github Actions实现。

步骤

1、程序上传到github,点击【Actions】

image.png 2、在该tab页中,向下滚动找到node相关的(我的是node的,按照自己实际代码找对应的)

image.png

3、点击 【Set up this workflow】按钮建立yml文件


# This workflow will do a clean install of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: Node.js CI

on:
  schedule:
    - cron:  '0 1 * * *'

jobs:
  build:

    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [14.x]
        # See supported Node.js release schedule at https://nodejs.org/en/about/releases/

    steps:
    - uses: actions/checkout@v2
    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v2
      with:
        node-version: ${{ matrix.node-version }}
        cache: 'npm'
    - run: npm install
    - run: npm run dev

简要解释:

  • schedule: 设置定时任务
  • cron:设置任务定时执行的时间,他和传统意义上的cron表达式不太一样,他没有秒的概念,此外此处设置的时间是UTC,不是北京时间,我这里设置的是北京时间9点,具体参照crontab.guru/examples.ht…
  • npm run dev 就是自己程序里配置的程序启动命令了 4、文件保存,至此就结束了,每天9点执行任务,不过他不一定准时

image.png

后记

简要配置,没有深入研究,如果感兴趣,请参照官方文档 docs.github.com/cn/actions

待解决问题

gitee是否有此方案,github有时候实在太慢