Github_workflow

starlitxiling Lv3

记录一个Github仓库发布的workflow模板:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
name: Release

on:
release:
types: [published]

jobs:
build-and-release:
runs-on: ubuntu-latest
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
arch: [x64, arm64]

steps:
# 检出代码
- name: Checkout Code
uses: actions/checkout@v3

# 设置 Node.js(如果需要)
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '16'

# 设置环境(例如安装依赖)
- name: Install Dependencies
run: |
npm install

# 构建步骤
- name: Build for ${{ matrix.os }} on ${{ matrix.arch }}
run: |
npm run build -- --arch=${{ matrix.arch }}

# 将构建结果打包
- name: Create Archive
run: |
mkdir -p release
tar -czvf release/myapp-${{ matrix.os }}-${{ matrix.arch }}.tar.gz dist/

# 上传构建文件以便发布
- name: Upload Artifact
uses: actions/upload-artifact@v3
with:
name: myapp-${{ matrix.os }}-${{ matrix.arch }}
path: release/myapp-${{ matrix.os }}-${{ matrix.arch }}.tar.gz

create-release:
needs: build-and-release
runs-on: ubuntu-latest
steps:
# 检出代码
- name: Checkout Code
uses: actions/checkout@v3

# 确保 GITHUB_TOKEN 环境变量存在以便上传工件
- name: Upload Release Assets
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ github.event.release.upload_url }}
asset_path: release/myapp-*.tar.gz
asset_name: myapp-${{ matrix.os }}-${{ matrix.arch }}.tar.gz
asset_content_type: application/gzip

这里要注意的是第63行的upload_url: ${{ github.event.release.upload_url }},它表示只有当你在仓库中按照以下图片步骤进行,才会触发,如果直接在ci中强制运行这个文件,这里会报错:You have to provide a GITHUB_TOKEN or GH_PAT 等。

第一步
第二步

  • Title: Github_workflow
  • Author: starlitxiling
  • Created at : 2024-10-16 19:17:10
  • Updated at : 2024-11-12 21:07:35
  • Link: http://starlitxiling.github.io/2024/10/16/Github-workflow/
  • License: This work is licensed under CC BY-NC-SA 4.0.
Comments
On this page
Github_workflow