Skip to content

Commit eafb3e4

Browse files
authored
Update build-release.yml
1 parent bbce341 commit eafb3e4

File tree

1 file changed

+47
-16
lines changed

1 file changed

+47
-16
lines changed

.github/workflows/build-release.yml

+47-16
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@ name: Build and Release
22

33
on:
44
push:
5-
tags:
6-
- 'v*'
5+
branches: [ "main" ]
6+
workflow_dispatch:
7+
inputs:
8+
tag:
9+
description: 'Tag name'
10+
required: false
11+
default: 'latest'
712

813
jobs:
914
build:
@@ -21,7 +26,7 @@ jobs:
2126
- name: Build and package for Windows x86_64
2227
run: |
2328
GOOS=windows GOARCH=amd64 go build -o thingspanel-backend-community-windows-x86_64.exe
24-
zip thingspanel-backend-community-windows-x86_64.zip thingspanel-backend-community-windows-x86.exe configs sql files
29+
zip thingspanel-backend-community-windows-x86_64.zip thingspanel-backend-community-windows-x86_64.exe configs sql files
2530
2631
- name: Build and package for Linux x86_64
2732
run: |
@@ -38,27 +43,53 @@ jobs:
3843
GOOS=darwin GOARCH=amd64 go build -o thingspanel-backend-community-darwin-x64
3944
zip thingspanel-backend-community-darwin-x64.zip thingspanel-backend-community-darwin-x64 configs sql files
4045
41-
- name: Create Release
42-
id: create_release
43-
uses: actions/create-release@v1
44-
env:
45-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
46+
- name: Determine Release Tag
47+
id: determine-tag
48+
run: echo "RELEASE_TAG=$(if [ '${{ github.event_name }}' == 'push' ]; then echo 'latest'; else echo '${{ github.event.inputs.tag }}'; fi)" >> $GITHUB_ENV
4649

50+
- name: Create or Update Release
51+
id: create_or_update_release
52+
uses: actions/github-script@v6
4753
with:
48-
tag_name: ${{ github.ref }}
49-
release_name: Release ${{ github.ref }}
50-
body: Release notes for ${{ github.ref }}
51-
draft: false
52-
prerelease: false
54+
script: |
55+
const tagName = process.env.RELEASE_TAG;
56+
const { data: releases } = await github.rest.repos.listReleases({
57+
owner: context.repo.owner,
58+
repo: context.repo.repo,
59+
});
60+
let release = releases.find(r => r.tag_name === tagName);
61+
62+
if (release) {
63+
// Update release
64+
await github.rest.repos.updateRelease({
65+
owner: context.repo.owner,
66+
repo: context.repo.repo,
67+
release_id: release.id,
68+
body: `Release updated by GitHub Actions. Tag ${tagName}`,
69+
});
70+
return { upload_url: release.upload_url.replace(/\/[^\/]+$/, '') };
71+
} else {
72+
// Create new release
73+
const { data: newRelease } = await github.rest.repos.createRelease({
74+
owner: context.repo.owner,
75+
repo: context.repo.repo,
76+
tag_name: tagName,
77+
name: `Release ${tagName}`,
78+
body: `Release created by GitHub Actions. Tag ${tagName}`,
79+
draft: false,
80+
prerelease: false,
81+
});
82+
return { upload_url: newRelease.upload_url.replace(/\/[^\/]+$/, '') };
83+
}
84+
result-encoding: json
5385

5486
- name: Upload Release Asset
5587
id: upload-release-asset
5688
uses: actions/upload-release-asset@v1
5789
env:
58-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # 自动使用的 GitHub Token
59-
90+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
6091
with:
61-
upload_url: ${{ steps.create_release.outputs.upload_url }}
92+
upload_url: ${{ steps.create_or_update_release.outputs.upload_url }}
6293
asset_path: |
6394
./thingspanel-backend-community-windows-x86_64.zip
6495
./thingspanel-backend-community-linux-x86_64.tar.gz

0 commit comments

Comments
 (0)