利用github定时执行任务

1、前言


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

2、 github怎么实现


利用github Actions实现。

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

![image.png](https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/140229c6001c46f5afd7bfffa6235a05~tplv-k3u1fbpfcp-watermark.image?)
2、在该tab页中,向下滚动找到node相关的(我的是node的,按照自己实际代码找对应的)

![image.png](https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/8a40ca31a6e447dfa8e4c551dae2dfff~tplv-k3u1fbpfcp-watermark.image?)

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点,具体参照https://crontab.guru/examples.html 
- npm run dev 就是自己程序里配置的程序启动命令了
4、文件保存,至此就结束了,每天9点执行任务,不过他不一定准时

![image.png](https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/107c88fa5e3249f89db3e69ef98185b0~tplv-k3u1fbpfcp-watermark.image?)

2、后记


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


3、 待解决问题


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

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值