GitHub Actions 实现夸克网盘定时签到

项目链接

GitHub 仓库地址:QuarkAutoSignIn - GitHub Repository

什么是 GitHub Actions

GitHub Actions 是 GitHub 提供的一种 持续集成和交付 (CI/CD) 平台,可以帮助开发者在代码库中自动化工作流程。它支持通过事件触发运行自定义脚本或操作,从而完成一系列任务,如构建、测试、部署、运行定时任务等。

为什么用 GitHub Actions

  1. 与 GitHub 无缝集成:直接在代码仓库内配置和运行,无需额外的工具。
  2. 易用性:YAML 配置简单,内置丰富的官方和社区支持的 Actions。
  3. 免费资源(最重要的一点):对于开源项目,GitHub 提供免费运行时,私有项目也有一定的免费配额。

思路

通过 Github Actions 定时运行 Python 脚本。

使用 Github Actions

完整 Github Actions 脚本:main.yml

在项目根目录下创建 .github/workflows 文件夹,新建一个工作流文件,如 main.yml

设置工作流名称

1
name: daily auto sign-in

这个名称会在 GitHub Actions 页面中显示,以帮助识别不同的工作流。

指定触发 Action 的事件

1
2
3
4
on:
  workflow_dispatch: # 允许手动触发
  schedule:
    - cron: '0 2 * * *' # 北京时间每天上午10点触发一次
  1. workflow_dispatch
    • workflow_dispatch 允许用户手动触发工作流。
    • 可以在 GitHub 仓库的 Actions 页面中点击 Run workflow 按钮来手动启动该工作流
  2. schedule
    • schedule 允许用户基于 cron 表达式 设置定时任务,自动触发工作流。

设置 Action 要执行的命令

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
27
28
29
30
31
32
33
jobs:
  sign_in:
    runs-on: ubuntu-latest

    env:
      KPS: ${{ secrets.KPS }}
      SIGN: ${{ secrets.SIGN }}
      VCODE: ${{ secrets.VCODE }}
      SENDKEY: ${{ secrets.SENDKEY }}

    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Set up Python
        uses: actions/setup-python@v2
        with:
          python-version: '3.12'

      - name: Cache Python dependencies
        uses: actions/cache@v3
        with:
          path: ~/.cache/pip
          key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
          restore-keys: |
            ${{ runner.os }}-pip-

      - name: Install dependencies if requirements.txt exists
        run: |
          if [ -f requirements.txt ]; then pip install -r requirements.txt; fi

      - name: Run main.py
        run: python main.py

jobs 下,定义了一个名为 sign_in 的任务,它将自动运行一些命令:

  1. runs-on

    • 设置工作流运行的环境,这里选择使用 ubuntu-latest,即使用最新版本的 Ubuntu 运行工作流。
  2. 环境变量 (env):

    • env 用来定义环境变量,这些环境变量可以在后续的步骤中使用。如果没有需要传递的环境变量可以省略这部分。
    • 在工作流中,我们通过 GitHub 的 Secrets 配置了一些敏感信息,如 KPS, SIGN, VCODESENDKEY
  3. 步骤 (steps):

    • Checkout code:拉取最新的代码,确保工作流可以访问代码库中的 Python 脚本。

      1
      2
      - name: Checkout code
        uses: actions/checkout@v3 # 使用官方的 checkout action 来拉取代码库
    • Set up Python:设置所需的 Python 版本,这里选择了 python-version: '3.12'

      1
      2
      3
      4
      - name: Set up Python
        uses: actions/setup-python@v2
        with:
          python-version: '3.12' # 使用官方的 setup-python action 来安装指定版本的 Python
    • Cache Python dependencies:缓存 Python 依赖项,保证每次工作流运行时只安装新依赖,而不是每次都重新安装。

      1
      2
      3
      4
      5
      6
      7
      - name: Cache Python dependencies
        uses: actions/cache@v3 # 使用官方的缓存 action 来缓存 pip 安装的依赖
        with:
          path: ~/.cache/pip
          key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
          restore-keys: |
            ${{ runner.os }}-pip-
    • Install dependencies:如果 requirements.txt 存在,安装 Python 依赖库。

      1
      2
      3
      - name: Install dependencies if requirements.txt exists
        run: |
          if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
    • Run the Python script:最后,执行 main.py 脚本。

      1
      2
      - name: Run main.py
        run: python main.py

提交工作流文件

.github/workflows/main.yml 文件提交到 GitHub 仓库后,GitHub Actions 会根据触发事件(如定时任务或手动触发)自动执行该工作流。


GitHub Actions 实现夸克网盘定时签到
http://xiaowhang.github.io/archives/4220125092/
作者
Xiaowhang
发布于
2024年12月11日
许可协议