从简单的python程序开始,了解一下GitHub Actions。
例子一
首先创建python文件
文件名 : test_1.py
该文件需要满足pytest基本要求,即
- 文件以
test_
开头,或者以 _test.py
结尾
- 文件内待测函数以
test_
开头
| import pytest
def test_hello():
print('Hello Actions!')
|
然后创建workflow文件
文件名:.github/workflows/py-app.yml
该文件可以通过点击Actions后选择合适的模板生成。
内容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 | name: Python application
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: "3.10"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install pytest
- name: Test with pytest
run: |
pytest
|
例子二
例子一只在Ubuntu下的Python3.10下运行。如果需要同时在Windows和Ubuntu运行,且同时在Python3.10和Python3.11下测试。
那么,workflow中需要使用 matrix。修改上面的workflow文件:
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: Python application matrix
on: [push]
jobs:
build:
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
python: ["3.10", "3.11"]
runs-on: ${{matrix.os}}
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: ${{matrix.python}}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install pytest
- name: Test with pytest
run: |
pytest
|
参考