cover
开发 #Electron #GitHub #Actions

GitHub Actions 自动构建Electron多平台应用

Asea
2023-03-15 19

Electron在windows平台下只能构建windows的应用,而对于mac和linux却束手无策,在寻找解决办法的时候关注到了GitHub的Action,每月提供一定量的upload空间以及使用时长,因此只需要编写好workflow就可以实现多平台自动构建并发布到release。最近几天一直才尝试,遇到各种bug,每个账号每个月的用量是有限的,因此注册了三个账号,反复验证,最后敲定了一套还算说得过去的流程,记录如下

# 添加workflow文件

  1. 在根目录下新建.github/workflows文件夹
  2. 在workflows文件夹下新建.yml文件,如main.yml
  3. 在此给出我的代码
name: create-release

on:
  push:
    tags: 
      - 'v*.*.*'
      
permissions:
  contents: write

jobs:
  test-build:

    # job's name
    name: create-release

    # the type of machine to run the job on
    runs-on: ${{ matrix.os }}

    # create a build matrix for jobs
    strategy:
      fail-fast: false
      matrix:
        os: [windows-latest, macos-latest]
        
    
    steps:
      # step1: check out repository
      - name: Check out git repository
        uses: actions/checkout@v3

      # step2: install node env
      - name: Install Node.js
        uses: actions/setup-node@v2-beta

      # step3: npm install
      - name: npm install
        run: |
          npm install

      # step4: rebuild
      - name: rebuild
        run: |
          npm run electron:rebuild
      
      # step5: npm run build
      - name: build
        run: |
          npm run build

      # step6: build app for mac/win
      - name: build windows app
        if: matrix.os == 'windows-latest'
        run: |
          npm run electron:build --win
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      
      - name: build mac app
        if: matrix.os == 'macos-latest'
        run: |
          npm run electron:build --mac
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      # step7: upload artifacts
      - name: upload artifacts
        uses: actions/upload-artifact@v3
        with:
          name: ${{ matrix.os }}
          path: |
            release/*latest*.yml
            release/*.exe
            release/*.dmg
            release/*.blockmap

      # step8: create release
      - name: release
        uses: softprops/action-gh-release@v1
        if: startsWith(github.ref, 'refs/tags/')
        with:
          files: |
            release/*latest*.yml
            release/*.exe
            release/*.dmg
            release/*.blockmap
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

# 自动构建

在本地执行

git tab v0.0.1
git push origin --tags

就会触发workflow,从而开始任务,在任务的执行过程中可能会遇到很多错误,大多数根据log都能解决,在此记录一些奇怪的问题

# 1. npm install 依赖报错

在项目里有一些依赖版本不太正确,可以使用 npm install --force 来避免,或自行更正版本

# 2. robotJs安装报错

暂时不知道问题出在哪里,因为不能稳定复现

# 3. 上传文件的路径匹配问题,最后只好选择的代码的方法

# 4. 空间用完

免费的每月貌似只有500M,所以我只好重新注册账号

# 5. release创建失败

貌似是因为我在新账号里面添加主账号为合作人员,提交的时候都是用主账号提交的,所以release创建的时候出现了权限问题,最后解决办法就是添加了permissions

# 6. release上传文件过多导致错误

做好路径匹配

CC BY-NC-SA 4.0 Licensed

COMMENTS (0)

2025 AseaBlog

豫ICP备2022022909号