GitHub Actions教程:运行python代码并Push到远端仓库

Github Actions教程:运行python代码并Push到远端仓库
以下为[Github Actions教程:运行python代码并Push到远端仓库]这位大佬的例子稍微改动一下

我自己做了一个网站,这个网站会使用一个python脚本来生成。
具体生成的方法是python脚本会读取目录下的csv文件,将每一行数据解析成固定格式,然后生成html文件,最后需要将修改后的文件自动push到github

当然上面所有的步骤都是自动化实现的,总结如下:

我本地修改csv文件,然后push到github
我的push操作会触发实现设定好的action
action代码设置如下:

 # This is a basic workflow to help you get started with Actions

name: vpn

# Controls when the action will run. 
on:
  # Triggers the workflow on push or pull request events but only for the main branch
  push:
    branches: [ main ]
  #pull_request:
    #branches: [ main ]
  schedule:
    - cron: '40 0/2 * * *'
  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - name: Checkout
        uses: actions/checkout@v2

      # Runs a single command using the runners shell
      - name: 'Set up Python'
        uses: actions/setup-python@v1
        with:
           python-version: 3.7
      - name: 'Install requirements'
        run: pip install -r ./Spider/requirements.txt
      - name: 'Working'
        run: 
          python ./Spider/vpn.py

      - name: commit
        run: |
          git config --global user.email 3196@qq.com
          git config --global user.name ndt
          git add .
          git commit -m "update" -a
      - name: Push changes
        uses: ad-m/github-push-action@master
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}

代码解释:

第一行name: 随便可以设置,就是你的action名字
on: 触发条件,我这里设置的是push操作一旦发生就出发
jobs: Github Actions的层级关系是这样的: workflow/jobs/steps/action。 注意将action和Github Actions中的Actions区分开来,二者是不同的概念,action就表示最低level的动作,Actions就是Github给我们提供的一个功能的名字而已。
steps:和jobs类似。可以看到steps由若干个step组成,每个step都可以设置name
uses:这个表示使用别人预先设置好的Actions,比如因为我代码中要用到python,所以就用了actions/setup-python@v1来设置python环境,不用我自己设置了。
run: 表示具体运行什么命令行代码
可以看到,我首先在名字为Update paper list里运行了python脚本
之后对github文件夹做了commit(commit 一定先在仓库里创建README)
最后使用别人的actions把更新后的代码再次push到github
最后一行github_token这个设置方法是进入你在个人设置页面(即Settings,不是仓库里的Settings),选择Developer settings>Personal access tokens>Generate new token,设置名字为GITHUB_TOKEN,然后勾选repo,admin:repo_hook,workflow等选项,最后点击Generate token即可。

具体代码可参见https://github.com/marsggbo/automl_a_survey_of_state_of_the_art

参考:https://www.cnblogs.com/marsggbo/p/12090703.html

posted @ 2021-01-02 15:19  nmydt  阅读(551)  评论(0编辑  收藏  举报