build-win-h5-only.yml 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. name: CI 自动构建win h5,并上传到服务器
  2. on:
  3. push:
  4. branches:
  5. - master
  6. repository_dispatch:
  7. types:
  8. - webhook_trigger
  9. # 并发控制:自动取消旧的运行
  10. concurrency:
  11. group: build-win-h5-only-${{ github.ref }}
  12. cancel-in-progress: true
  13. jobs:
  14. build-h5-production:
  15. runs-on: [win-h5-only]
  16. steps:
  17. - name: 检查 PowerShell 执行策略
  18. shell: cmd
  19. run: |
  20. powershell -Command "$policy = Get-ExecutionPolicy; Write-Host '当前有效执行策略:' $policy; if ($policy -eq 'Restricted' -or $policy -eq 'Undefined') { Write-Error '执行策略未正确配置!请在 runner 机器上执行:Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine -Force'; exit 1 } else { Write-Host '✓ 执行策略检查通过' }"
  21. - name: Test SSH key
  22. shell: powershell
  23. run: |
  24. $keyPath = Join-Path $env:TEMP "test_key"
  25. "${{ secrets.DEPLOY_KEY }}" | Out-File -FilePath $keyPath -Encoding UTF8 -NoNewline
  26. ssh -i $keyPath -o StrictHostKeyChecking=no ${{ secrets.DEPLOY_HOST }} "echo 'SSH key works!'"
  27. Remove-Item -Path $keyPath -Force -ErrorAction SilentlyContinue
  28. - name: 检出代码
  29. uses: actions/checkout@v4
  30. - name: 设置 Node.js 环境
  31. uses: actions/setup-node@v4
  32. with:
  33. node-version: '20'
  34. - name: 安装依赖
  35. run: npm install --force
  36. - name: 构建 H5 (生产环境)
  37. shell: powershell
  38. env:
  39. GH_TOKEN: ${{ secrets.GH_TOKEN }}
  40. TARO_API_URL: 'http://localhost:6001'
  41. TARO_MQTT_URL: 'ws://localhost:8083/mqtt'
  42. run: |
  43. Write-Host "当前操作系统平台: Windows"
  44. Write-Host "当前CPU架构: $env:PROCESSOR_ARCHITECTURE"
  45. # Windows 环境不需要 ARM 平台特殊处理
  46. node .build/h5_for_production.js
  47. - name: Set timestamp version
  48. shell: powershell
  49. run: |
  50. $version = Get-Date -Format "yyyyMMdd-HHmmss"
  51. "VERSION=$version" | Out-File -FilePath $env:GITHUB_ENV -Append -Encoding UTF8
  52. - name: Upload dist/h5/ to server
  53. shell: powershell
  54. run: |
  55. # 1. 将私钥写入临时文件
  56. $keyPath = Join-Path $env:TEMP "deploy_key"
  57. "${{ secrets.DEPLOY_KEY }}" | Out-File -FilePath $keyPath -Encoding UTF8 -NoNewline
  58. # 2. 创建远程目录
  59. ssh -i $keyPath -o StrictHostKeyChecking=no "${{ secrets.DEPLOY_USER }}@${{ secrets.DEPLOY_HOST }}" "mkdir -p ${{ secrets.DEPLOY_PATH }}/$env:VERSION/"
  60. # 3. 上传文件(使用 scp 替代 rsync)
  61. scp -r -i $keyPath -o StrictHostKeyChecking=no dist/h5/* "${{ secrets.DEPLOY_USER }}@${{ secrets.DEPLOY_HOST }}:${{ secrets.DEPLOY_PATH }}/$env:VERSION/"
  62. # 4. 清理密钥文件
  63. Remove-Item -Path $keyPath -Force -ErrorAction SilentlyContinue
  64. - name: Update latest symlink
  65. shell: powershell
  66. run: |
  67. $keyPath = Join-Path $env:TEMP "deploy_key"
  68. "${{ secrets.DEPLOY_KEY }}" | Out-File -FilePath $keyPath -Encoding UTF8 -NoNewline
  69. ssh -i $keyPath -o StrictHostKeyChecking=no "${{ secrets.DEPLOY_USER }}@${{ secrets.DEPLOY_HOST }}" "cd ${{ secrets.DEPLOY_PATH }} && ln -sfn $env:VERSION latest"
  70. Remove-Item -Path $keyPath -Force -ErrorAction SilentlyContinue