Github Action(first use)
基础了解
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| name: 名字随便取
# on是触发条件 on: push: # 当push到仓库main时触发 branches: [ main ] pull_request: # 当pull仓库main时触发 branches: [ main ] schedule: # 定时触发,接受正则表达式,使用UTC时间(北京时间+8h) - cron: '0 0 1 * *' workflow_dispatch: # 可手动触发(后面介绍一个骚操作)
# 工作内容 jobs: it_is_only_a_name: # 工作内容一:build(可改任何名字) runs-on: ubuntu-latest # 创建工作环境 steps: # 步骤 # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it - name: 'Checkout codes' # 检查代码(好像是必须有的一步) uses: actions/checkout@v1 # 可运行github给出的一些命令 - name: '给你的一个步骤起个名字' run: pip install -r requirements.txt # 也可直接运行命令 - name: "运行一下python文件" run: python print.py
|
按上述代码填好后结果如下:(箭头位置可手动触发)
每一步的运行结果:
至此,Github Action 的基本使用就到此为止了,十分方便。但也因此使用人数太多导致服务器资源不够用,有时需要 排队『排‘ 排队‘ 的队伍』,导致schedule定时任务延迟。
那么对于那些需要精准控时的任务要怎么办呢?只能自己租服务器了吗?
不!我们要将白嫖精神落实到底!
基本原理
如果手动去点 Run workflow 按钮的话排队是不会有延迟的
可以直接 post 一段链接来代替点击按钮这个行为
国内服务器厂商的定时云函数几乎是没有延迟的
简单的云函数不用花钱
于是我选择了腾讯云
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| import requests import json
def run(): payload = json.dumps({"ref": "main"}) header = {'Authorization': 'token GitHubToken', "Accept": "application/vnd.github.v3+json"} response_decoded_json = requests.post( f'https://api.github.com/repos/Github账号/Github项目名/actions/workflows/GithubAction工作流名称或ID/dispatches', data=payload, headers=header)
def main_handler(event, context): return run()
|
将上述代码填好之后创建腾讯云云函数即可