build-win-h5-only.yml 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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: powershell
  19. run: Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process -Force
  20. - name: Test SSH key
  21. shell: powershell
  22. run: |
  23. $keyPath = Join-Path $env:TEMP "test_key"
  24. "${{ secrets.DEPLOY_KEY }}" | Out-File -FilePath $keyPath -Encoding UTF8 -NoNewline
  25. ssh -i $keyPath -o StrictHostKeyChecking=no ${{ secrets.DEPLOY_HOST }} "echo 'SSH key works!'"
  26. Remove-Item -Path $keyPath -Force -ErrorAction SilentlyContinue
  27. - name: 检出代码
  28. uses: actions/checkout@v4
  29. - name: 设置 Node.js 环境
  30. uses: actions/setup-node@v4
  31. with:
  32. node-version: '20'
  33. - name: 安装依赖
  34. run: npm install --force
  35. - name: 构建 H5 (生产环境)
  36. shell: powershell
  37. env:
  38. GH_TOKEN: ${{ secrets.GH_TOKEN }}
  39. TARO_API_URL: 'http://localhost:6001'
  40. TARO_MQTT_URL: 'ws://localhost:8083/mqtt'
  41. run: |
  42. Write-Host "当前操作系统平台: Windows"
  43. Write-Host "当前CPU架构: $env:PROCESSOR_ARCHITECTURE"
  44. # Windows 环境不需要 ARM 平台特殊处理
  45. node .build/h5_for_production.js
  46. - name: Set timestamp version
  47. shell: powershell
  48. run: |
  49. $version = Get-Date -Format "yyyyMMdd-HHmmss"
  50. "VERSION=$version" | Out-File -FilePath $env:GITHUB_ENV -Append -Encoding UTF8
  51. - name: Upload dist/h5/ to server
  52. shell: powershell
  53. run: |
  54. # 1. 将私钥写入临时文件
  55. $keyPath = Join-Path $env:TEMP "deploy_key"
  56. "${{ secrets.DEPLOY_KEY }}" | Out-File -FilePath $keyPath -Encoding UTF8 -NoNewline
  57. # 2. 创建远程目录
  58. ssh -i $keyPath -o StrictHostKeyChecking=no "${{ secrets.DEPLOY_USER }}@${{ secrets.DEPLOY_HOST }}" "mkdir -p ${{ secrets.DEPLOY_PATH }}/$env:VERSION/"
  59. # 3. 上传文件(使用 scp 替代 rsync)
  60. scp -r -i $keyPath -o StrictHostKeyChecking=no dist/h5/* "${{ secrets.DEPLOY_USER }}@${{ secrets.DEPLOY_HOST }}:${{ secrets.DEPLOY_PATH }}/$env:VERSION/"
  61. # 4. 清理密钥文件
  62. Remove-Item -Path $keyPath -Force -ErrorAction SilentlyContinue
  63. - name: Update latest symlink
  64. shell: powershell
  65. run: |
  66. $keyPath = Join-Path $env:TEMP "deploy_key"
  67. "${{ secrets.DEPLOY_KEY }}" | Out-File -FilePath $keyPath -Encoding UTF8 -NoNewline
  68. ssh -i $keyPath -o StrictHostKeyChecking=no "${{ secrets.DEPLOY_USER }}@${{ secrets.DEPLOY_HOST }}" "cd ${{ secrets.DEPLOY_PATH }} && ln -sfn $env:VERSION latest"
  69. Remove-Item -Path $keyPath -Force -ErrorAction SilentlyContinue