Skip to content

fix: streamline build process by executing PyInstaller from project r… #15

fix: streamline build process by executing PyInstaller from project r…

fix: streamline build process by executing PyInstaller from project r… #15

Workflow file for this run

name: Create Tag and Release
on:
pull_request:
types: [closed, synchronize]
branches:
- main
- 1.2.0-release-2
concurrency:
group: create-tag-and-release-${{ github.ref }}
cancel-in-progress: true
jobs:
# Job 1: Validate the PR and extract version
validate_and_tag:
# if: github.event.pull_request.merged == true
runs-on: ubuntu-latest
outputs:
version: ${{ steps.validate_title.outputs.version }}
steps:
- name: Checkout code
uses: actions/checkout@v3
with:
token: ${{ secrets.PAT_TOKEN }}
- name: Validate branch name and extract version
id: validate_title
run: |
branch_name="${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}"
echo "Branch name: $branch_name"
echo '1' $GITHUB_REF
echo '2' $GITHUB_REF
echo '3' $GITHUB_REF_NAME
echo '4' $GITHUB_HEAD_REF
echo '5' $GITHUB_BASE_REF
if [[ "$branch_name" =~ ^v[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9-]+)?$ ]]; then
version=$(echo "$branch_name" | grep -oE '^v[0-9]+\.[0-9]+\.[0-9]+')
echo "version=$version" >> "$GITHUB_OUTPUT"
echo "Matched version: $version"
else
echo "Branch name does not match the required format (vX.X.X or vX.X.X-suffix)."
exit 1 # Exit with failure
fi
shell: bash
- name: Create a tag
if: ${{ steps.validate_title.outputs.version != '' }}
run: |
version=${{ steps.validate_title.outputs.version }}
# 检查本地是否存在该 Tag
tag_exists=$(git tag -l "$version")
if [ -z "$tag_exists" ]; then
# 本地不存在该 Tag,创建 Tag
git tag "$version"
# 检查远程是否存在该 Tag
remote_tag_exists=$(git ls-remote --tags origin | grep "refs/tags/$version" || true)
if [ -z "$remote_tag_exists" ]; then
# 如果远程也不存在,推送 Tag
git push origin "$version"
echo "Tag $version created and pushed successfully."
else
echo "Tag $version already exists in remote. Skipping push."
fi
else
echo "Tag $version already exists locally. Skipping tag creation and push."
fi
shell: bash
# Job 2: Compile using matrix for different platforms
compile:
needs: validate_and_tag
if: ${{ needs.validate_and_tag.outputs.version != '' }}
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
include:
- os: ubuntu-latest
python-version: "3.x"
- os: windows-latest
python-version: "3.x"
architecture: "x64"
- os: macos-latest
python-version: "3.x"
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
architecture: ${{ matrix.architecture || '' }}
- name: Install Dependencies
if: runner.os != 'Windows'
run: |
if [ -f requirements.txt ]; then
pip install -r requirements.txt
fi
shell: bash
- name: Install Dependencies (Windows)
if: runner.os == 'Windows'
run: |
if (Test-Path -Path requirements.txt) {
pip install -r requirements.txt
}
shell: pwsh
- name: Install PyInstaller
run: |
pip install pyinstaller
- name: Compile for Linux and macOS
if: runner.os != 'Windows'
run: |
# 直接在项目根目录下执行 PyInstaller
pyinstaller --onefile sub_adjust.py
pyinstaller --onefile sub_converter.py
# 找到当前工作目录
current_dir=$(pwd)
echo "Current directory: $current_dir"
# 假设工作目录为 /home/runner/work/sub-adjust/sub-adjust/
# dist 文件夹应位于 ${current_dir}/dist
# 移动编译后的文件到对应的 build 目录
if [ "${{ matrix.os }}" == "ubuntu-latest" ]; then
mkdir -p build/linux
if [ -f "${current_dir}/dist/sub_adjust" ]; then
mv "${current_dir}/dist/sub_adjust" build/linux/
else
echo "Error: sub_adjust not found in dist/ after PyInstaller run."
fi
if [ -f "${current_dir}/dist/sub_converter" ]; then
mv "${current_dir}/dist/sub_converter" build/linux/
else
echo "Error: sub_converter not found in dist/ after PyInstaller run."
fi
elif [ "${{ matrix.os }}" == "macos-latest" ]; then
mkdir -p build/macos
if [ -f "${current_dir}/dist/sub_adjust" ]; then
mv "${current_dir}/dist/sub_adjust" build/macos/
else
echo "Error: sub_adjust not found in dist/ after PyInstaller run."
fi
if [ -f "${current_dir}/dist/sub_converter" ]; then
mv "${current_dir}/dist/sub_converter" build/macos/
else
echo "Error: sub_converter not found in dist/ after PyInstaller run."
fi
fi
shell: bash
- name: Compile for Windows
if: runner.os == 'Windows'
run: |
# 获取当前工作目录
current_dir=$(pwd)
echo "Current directory: $current_dir"
# 直接在项目根目录下执行 PyInstaller
pyinstaller --onefile sub_adjust.py
pyinstaller --onefile sub_converter.py
# 确保存在 build/windows 目录并移动编译后的文件
mkdir -p build/windows
if [ -f "${current_dir}\\dist\\sub_adjust.exe" ]; then
mv "${current_dir}\\dist\\sub_adjust.exe" build/windows/
else
echo "Error: sub_adjust.exe not found in dist/ after PyInstaller run."
fi
if [ -f "${current_dir}\\dist\\sub_converter.exe" ]; then
mv "${current_dir}\\dist\\sub_converter.exe" build/windows/
else
echo "Error: sub_converter.exe not found in dist/ after PyInstaller run."
fi
shell: pwsh
# Job 3: Create release
create_release:
needs: [validate_and_tag, compile]
if: ${{ needs.validate_and_tag.outputs.version != '' }}
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
# Tar compiled Linux binaries
- name: Tar Linux binaries
run: |
tar -czvf "sub_adjust-${{ needs.validate_and_tag.outputs.version }}-linux-x64.tar.gz" -C build/linux .
# Tar compiled macOS binaries
- name: Tar macOS binaries
run: |
tar -czvf "sub_adjust-${{ needs.validate_and_tag.outputs.version }}-macos-x64.tar.gz" -C build/macos .
# Zip compiled Windows binaries
- name: Zip Windows binaries
run: |
zip -r "sub_adjust-${{ needs.validate_and_tag.outputs.version }}-win-x64.zip" build/windows
# Zip source code
- name: Zip source code
run: |
zip -r "sub_adjust-${{ needs.validate_and_tag.outputs.version }}-source.zip" .
# Create GitHub release
- name: Create GitHub release
id: create_release_step
uses: actions/create-release@v1
with:
tag_name: ${{ needs.validate_and_tag.outputs.version }}
release_name: Release ${{ needs.validate_and_tag.outputs.version }}
body: |
Automatically created release for version ${{ needs.validate_and_tag.outputs.version }}.
### PR Description:
${{ github.event.pull_request.body }}
draft: false
prerelease: false
# Upload Linux binaries tar.gz file to release
- name: Upload Linux binaries tar.gz file to release
uses: actions/upload-release-asset@v1
with:
upload_url: ${{ steps.create_release_step.outputs.upload_url }}
asset_path: ./sub_adjust-${{ needs.validate_and_tag.outputs.version }}-linux-x64.tar.gz
asset_name: sub_adjust-${{ needs.validate_and_tag.outputs.version }}-linux-x64.tar.gz
asset_content_type: application/gzip
# Upload macOS binaries tar.gz file to release
- name: Upload macOS binaries tar.gz file to release
uses: actions/upload-release-asset@v1
with:
upload_url: ${{ steps.create_release_step.outputs.upload_url }}
asset_path: ./sub_adjust-${{ needs.validate_and_tag.outputs.version }}-macos-x64.tar.gz
asset_name: sub_adjust-${{ needs.validate_and_tag.outputs.version }}-macos-x64.tar.gz
asset_content_type: application/gzip
# Upload Windows binaries zip file to release
- name: Upload Windows binaries zip file to release
uses: actions/upload-release-asset@v1
with:
upload_url: ${{ steps.create_release_step.outputs.upload_url }}
asset_path: ./sub_adjust-${{ needs.validate_and_tag.outputs.version }}-win-x64.zip
asset_name: sub_adjust-${{ needs.validate_and_tag.outputs.version }}-win-x64.zip
asset_content_type: application/zip
# Upload source zip file to release
- name: Upload source zip file to release
uses: actions/upload-release-asset@v1
with:
upload_url: ${{ steps.create_release_step.outputs.upload_url }}
asset_path: ./sub_adjust-${{ needs.validate_and_tag.outputs.version }}-source.zip
asset_name: sub_adjust-${{ needs.validate_and_tag.outputs.version }}-source.zip
asset_content_type: application/zip