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
