在GitLab.com 上使用官方的 Windows SaaS Runner
4 min readMay 18, 2025
最近在工作上,因為需要發布一套要在 WIndows 平台上使用內部工具,所以用上了 GitLab 官方的 Windows runner 跑一些 CI 工作,不然之前 CI 相關的事情都是用 docker 跑 Linux。
要換成 Windows 的 runner,其實只需要在 gitlab-ci.yml 簡單將 tag 換成 saas-windows-medium-amd64 就好,而在 script 的部分,則是要寫 PowerShell — 這個我就從來沒熟悉過,在這邊簡單紀錄一下。
這套工具是用 Python 寫的,然後透過 tkinter 做了簡單的 GUI,讓內部其他同事可以在這個 GUI 裡頭從網路上抓取一些資料,寫入本地。而編譯這個小工具,首先要在 WIndows 上設定我們在 GitLab 中設定的 SSH Key 環境變數,抓取一些相依套件。
我們可以先寫一個設定 ssh 的 GitLab CI reference:
.windows_setup_ssh:
before_script:
- |
$sshFolder = Join-Path $env:USERPROFILE '.ssh'
$keyPath = Join-Path $sshFolder 'id_ed25519'
New-Item -ItemType Directory -Path $sshFolder -Force | Out-Null
Set-Content -Path $keyPath -Value $env:SSH_PRIVATE_KEY -NoNewline
icacls $keyPath /inheritance:r /grant:r "$($env:USERNAME):(R)" | Out-Null
ssh-keyscan gitlab.com | Out-File -Encoding ascii (Join-Path $sshFolder 'known_hosts')
Try { Start-Service ssh-agent -ErrorAction Stop } Catch {
Set-Service ssh-agent -StartupType Manual; Start-Service ssh-agent
}
ssh-add $keyPath
ssh -o BatchMode=yes -T git@gitlab.com 2>$null
if ($LASTEXITCODE) { Write-Host '❌ SSH handshake failed' ; exit 1 }
然後在實際使用的 CI job 就可以這樣寫
build:iot_burner:windows:
stage: build
before_script:
- !reference [.windows_setup_ssh, before_script]
tags:
- saas-windows-medium-amd64
script:
- powershell.exe .\build_windows.ps1
在 Windows 上有許多個不同的 Python 發行方式,在 Windows 的 App Market 裡頭有,Python 官網也有 Windows 的安裝程式,用 winget 也可以裝。不過,因為我們用了 tkinter,安裝 WinPython 還是比較方便,畢竟裡頭已經直接包含了 tkinter。
$WINPYTHON_URL = "https://github.com/winpython/winpython/releases/download/15.3.20250425final/Winpython64-3.13.3.0dot.exe"
Invoke-WebRequest $WINPYTHON_URL -OutFile wp.exe
Start-Process -FilePath ".\wp.exe" -ArgumentList "-y", "-o", "." -Wait -NoNewWindow
裝完之後設定一下路徑
$PYTHON_EXE = "WPy64-31330/python/python.exe"
$PYTHON_DIR = Split-Path -Path $PYTHON_EXE
$env:Path = "$PYTHON_DIR;$env:Path"
安裝一下其他需要的套件
& $PYTHON_EXE -m pip install pyinstaller
$PYINSTALLER_EXE = "WPy64-31330/python/Scripts/pyinstaller.exe"
& $PYTHON_EXE -m pip install requests
最後就可以編譯打包了
$PYINSTALLER_EXE --name my_app --onefile my_app.py