|
| 1 | +# This workflow will install Python dependencies, run tests and lint with a single version of Python |
| 2 | +# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python |
| 3 | + |
| 4 | +name: Python application |
| 5 | + |
| 6 | +on: |
| 7 | + push: |
| 8 | + branches: [ "main" ] |
| 9 | + pull_request: |
| 10 | + branches: [ "main" ] |
| 11 | + |
| 12 | +permissions: |
| 13 | + contents: write |
| 14 | + |
| 15 | +jobs: |
| 16 | + build: |
| 17 | + |
| 18 | + runs-on: windows-latest |
| 19 | + |
| 20 | + steps: |
| 21 | + - uses: actions/checkout@v4 |
| 22 | + with: |
| 23 | + fetch-depth: 0 |
| 24 | + |
| 25 | + - name: Set up Python 3.11 |
| 26 | + uses: actions/setup-python@v3 |
| 27 | + with: |
| 28 | + python-version: "3.11" |
| 29 | + |
| 30 | + - name: Extract latest version from commits |
| 31 | + id: extract-version |
| 32 | + run: | |
| 33 | + $version = git log --pretty=format:%s | ForEach-Object { |
| 34 | + if ($_ -match '^v\d+(\.\d+)+') { |
| 35 | + $matches[0] |
| 36 | + } |
| 37 | + } | Select-Object -First 1 |
| 38 | + if (-not $version) { |
| 39 | + throw "No version tag found in git log." |
| 40 | + } |
| 41 | + echo "Latest version: $version" |
| 42 | + echo "VERSION_TAG=$version" >> $env:GITHUB_ENV |
| 43 | + shell: pwsh |
| 44 | + |
| 45 | + - name: Get latest UPX version |
| 46 | + id: get-upx |
| 47 | + run: | |
| 48 | + $response = Invoke-RestMethod -Uri "https://api.github.com/repos/upx/upx/releases/latest" |
| 49 | + $version = $response.tag_name -replace 'v','' |
| 50 | + $downloadUrl = $response.assets | Where-Object { $_.name -match 'win64\.zip$' } | Select-Object -ExpandProperty browser_download_url |
| 51 | + echo "UPX_VERSION=$version" >> $env:GITHUB_ENV |
| 52 | + echo "UPX_URL=$downloadUrl" >> $env:GITHUB_ENV |
| 53 | + echo "UPX_DIR=upx-$version-win64" >> $env:GITHUB_ENV |
| 54 | + shell: pwsh |
| 55 | + |
| 56 | + - name: Install UPX |
| 57 | + run: | |
| 58 | + # Clean up any previous failed attempts |
| 59 | + Remove-Item -Path upx -Recurse -Force -ErrorAction SilentlyContinue |
| 60 | + Remove-Item -Path upx.zip -Force -ErrorAction SilentlyContinue |
| 61 | + # Download and extract |
| 62 | + Invoke-WebRequest -Uri $env:UPX_URL -OutFile upx.zip |
| 63 | + Expand-Archive -Path upx.zip -DestinationPath upx -Force |
| 64 | + # Verify UPX works |
| 65 | + & "$env:GITHUB_WORKSPACE\upx\$env:UPX_DIR\upx.exe" --version |
| 66 | + shell: pwsh |
| 67 | + |
| 68 | + - name: Install dependencies |
| 69 | + run: | |
| 70 | + python -m pip install --upgrade pip |
| 71 | + pip install -r requirements.txt |
| 72 | + shell: cmd |
| 73 | + |
| 74 | + - name: Build the application |
| 75 | + run: | |
| 76 | + pyinstaller app.spec --upx-dir="$env:GITHUB_WORKSPACE\upx\$env:UPX_DIR" |
| 77 | + shell: pwsh |
| 78 | + |
| 79 | + - name: Get application name |
| 80 | + id: app-name |
| 81 | + run: | |
| 82 | + $appName = Get-ChildItem -Path dist | Where-Object { $_.PSIsContainer } | Select-Object -ExpandProperty Name |
| 83 | + echo "APP_NAME=$appName" >> $env:GITHUB_ENV |
| 84 | + shell: pwsh |
| 85 | + |
| 86 | + - name: Archive the built program |
| 87 | + run: | |
| 88 | + mkdir release |
| 89 | + Compress-Archive -Path "dist\$env:APP_NAME\*" -DestinationPath "release\$env:APP_NAME.zip" |
| 90 | + shell: pwsh |
| 91 | + |
| 92 | + - name: Delete existed tag |
| 93 | + env: |
| 94 | + GITHUB_TOKEN: ${{ secrets.BUILDER_PAT }} |
| 95 | + run: | |
| 96 | + try { |
| 97 | + gh release delete $env:VERSION_TAG --yes |
| 98 | + } catch { |
| 99 | + Write-Host "No existing release found, skipped deleting." |
| 100 | + } |
| 101 | + try { |
| 102 | + git push origin ":refs/tags/$env:VERSION_TAG" |
| 103 | + } catch { |
| 104 | + Write-Host "No existing tag found, skipped deleting." |
| 105 | + } |
| 106 | + shell: pwsh |
| 107 | + |
| 108 | + - name: Create GitHub Release |
| 109 | + uses: softprops/action-gh-release@v2 |
| 110 | + env: |
| 111 | + GITHUB_TOKEN: ${{ secrets.BUILDER_PAT }} |
| 112 | + with: |
| 113 | + tag_name: ${{ env.VERSION_TAG }} |
| 114 | + name: ${{ env.APP_NAME }} |
| 115 | + files: release/${{ env.APP_NAME }}.zip |
| 116 | + body: Automatically built and released by GitHub Actions. |
0 commit comments