動かざることバグの如し

近づきたいよ 君の理想に

NuitkaでPythonをexe化するCIを書いた

環境

やりたいこと

Pythonコードをバイナリ化してPythonがないWindows環境でも実行できるようにしたい

調べるとPyinstallerが有名だが最近はNuitkaがトレンドらしい。

nuitka.net

毎回手動でビルドしてたらやってられないのでCIでPythonをexe化したい。

コード

以下がCIのコード

name: Build Windows EXE (Nuitka)

on:
  push:
    branches: [main]
  workflow_dispatch:

jobs:
  build-windows:
    runs-on: windows-latest
    timeout-minutes: 60
    env:
      PYTHONIOENCODING: "UTF-8" # UnicodeEncodeError: 'charmap' codec can't encode characters対策

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup Python 3.12.8
        uses: actions/setup-python@v5
        with:
          python-version: "3.12.8"

      - name: Setup uv
        uses: astral-sh/setup-uv@v5
        with:
          enable-cache: true

      - name: Sync dependencies (uv)
        run: uv sync --frozen

      - name: Build exe with Nuitka
        run: >-
          uv run python -m nuitka
          --standalone --onefile
          --assume-yes-for-downloads
          --msvc=latest
          --output-dir=dist
          --output-filename=windows-click.exe
          src/main.py

      - name: Verify built exe shows help
        run: .\dist\windows-click.exe --help

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: windows-click-x64
          path: dist/windows-click.exe
          if-no-files-found: error
          retention-days: 7

参考リンク