GitHub Actions 是 GitHub 的持续集成服务。

在这个Python项目 https://github.com/xmaihh/CSVFilter 中使用Github Actions自动化构建exe并发布到Release。

pyinstaller使用问题参考**PyInstaller-Perfect-Build-Method**

在每个工作流作业开始时,GitHub 会自动创建唯一的 GITHUB_TOKEN 机密以在工作流中使用。 可以使用 GITHUB_TOKEN 在工作流作业中进行身份验证。使用标准语法引用密钥GITHUB_TOKEN:${{ secrets.GITHUB_TOKEN }},有关详细信息请参阅“关于 GITHUB_TOKEN 机密”。

使用 Github Actions 编译Python项目自动化构建 exe

在你的Python源码仓库根目录下创建一个 .github/workflows 文件夹,在该文件夹内创建一个新的 YAML 文件(例如 deploy.yml)用于定义 GitHub Actions 工作流。

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# This workflow will upload a Python Package using PyInstaller when a release is published
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python

name: Publish to Release

on:
push:
branches:
- main
workflow_dispatch:

jobs:
build:
runs-on: windows-latest
env:
OUTPUT_FILE_PREFIX: CSVFilter.Setup

steps:
- name: Checkout repository
uses: actions/checkout@v3
with:
fetch-depth: 0

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

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install pip-tools
pip-sync requirements.txt

- name: Update configuration file
run: python prebuild_scripts/version.py

- name: Read config file
id: read_config
run: |
$version = (python -c "import configparser;config = configparser.ConfigParser();config.read('config.ini');print(config.get('DEFAULT', 'version'))")
echo "version=$version" >> $env:GITHUB_OUTPUT

- name: Build executable
run: |
echo "Using version: ${{ steps.read_config.outputs.version }}"
pyinstaller --onefile --add-data "resources;resources" --add-data "config.ini;." --icon="resources/csv_filter.ico" --windowed --clean --name "${{ env.OUTPUT_FILE_PREFIX }}.${{ steps.read_config.outputs.version }}" main.py

- name: Create release
id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: v${{ steps.read_config.outputs.version }}
release_name: Release ${{ steps.read_config.outputs.version }}
draft: false
prerelease: false

- name: Delete old releases
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
$current_release = "v${{ steps.read_config.outputs.version }}"
$releases = gh release list --limit 1000 | ForEach-Object { $_.Split()[0] }
foreach ($release in $releases) {
if ($release -ne $current_release) {
echo "Deleting release: $release"
gh release delete $release --cleanup-tag -y
}
}

- name: Upload release asset
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: dist/${{ env.OUTPUT_FILE_PREFIX }}.${{ steps.read_config.outputs.version }}.exe
asset_name: ${{ env.OUTPUT_FILE_PREFIX }}.${{ steps.read_config.outputs.version }}.exe
asset_content_type: application/octet-stream

Reference

官方文档

PyInstaller-Perfect-Build-Method