Skip to content

add: 文件整理桌面版与自动发布流程 #10

add: 文件整理桌面版与自动发布流程

add: 文件整理桌面版与自动发布流程 #10

Workflow file for this run

name: Release
on:
push:
branches:
- main
tags:
- 'v*'
workflow_dispatch:
inputs:
tag:
description: Release tag, for example v26.07.02
required: true
type: string
permissions:
contents: write
jobs:
windows-release:
name: Build Windows exe and publish release
runs-on: windows-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: 24
- name: Set up pnpm
run: |
corepack enable
corepack prepare pnpm@11.7.0 --activate
- name: Install dependencies
run: pnpm install --frozen-lockfile
- name: Run tests
run: pnpm test
- name: Build desktop executable
env:
CSC_IDENTITY_AUTO_DISCOVERY: false
run: pnpm run release:desktop
- name: Resolve release tag
id: release
shell: pwsh
env:
EVENT_NAME: ${{ github.event_name }}
INPUT_TAG: ${{ inputs.tag }}
REF_TYPE: ${{ github.ref_type }}
REF_NAME: ${{ github.ref_name }}
run: |
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
$OutputEncoding = [System.Text.Encoding]::UTF8
$package = Get-Content package.json -Raw | ConvertFrom-Json
if ($env:EVENT_NAME -eq 'workflow_dispatch' -and $env:INPUT_TAG) {
$tag = $env:INPUT_TAG
} elseif ($env:REF_TYPE -eq 'tag') {
$tag = $env:REF_NAME
} else {
$tag = "v$($package.releaseVersion)"
}
if ($tag.StartsWith('V')) {
$tag = "v$($tag.Substring(1))"
} elseif (-not $tag.StartsWith('v')) {
$tag = "v$tag"
}
$releaseLabel = "V$($tag.Substring(1))"
"tag=$tag" | Out-File -FilePath $env:GITHUB_OUTPUT -Append -Encoding utf8
"title=FileOrganizer-$releaseLabel" | Out-File -FilePath $env:GITHUB_OUTPUT -Append -Encoding utf8
"assetName=FileOrganizer-$releaseLabel-win-x64.zip" | Out-File -FilePath $env:GITHUB_OUTPUT -Append -Encoding utf8
- name: Create or update GitHub release
shell: pwsh
env:
GH_TOKEN: ${{ github.token }}
RELEASE_TAG: ${{ steps.release.outputs.tag }}
RELEASE_TITLE: ${{ steps.release.outputs.title }}
ASSET_NAME: ${{ steps.release.outputs.assetName }}
run: |
$ErrorActionPreference = 'Stop'
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
$OutputEncoding = [System.Text.Encoding]::UTF8
$assetPath = Join-Path "dist" $env:ASSET_NAME
if (-not (Test-Path -LiteralPath $assetPath)) {
throw "Release asset not found: $assetPath"
}
$headers = @{
Authorization = "Bearer $env:GH_TOKEN"
Accept = "application/vnd.github+json"
"X-GitHub-Api-Version" = "2022-11-28"
}
$apiBase = "https://api.github.com/repos/$env:GITHUB_REPOSITORY"
$release = $null
try {
$release = Invoke-RestMethod -Headers $headers -Uri "$apiBase/releases/tags/$env:RELEASE_TAG"
} catch {
if ($_.Exception.Response.StatusCode.value__ -ne 404) {
throw
}
}
if ($release) {
foreach ($releaseAsset in $release.assets) {
if ($releaseAsset.name.EndsWith('.exe') -or $releaseAsset.name.EndsWith('.zip')) {
Invoke-RestMethod -Method Delete -Headers $headers -Uri "$apiBase/releases/assets/$($releaseAsset.id)"
}
}
$release = Invoke-RestMethod `
-Method Patch `
-Headers $headers `
-Uri "$apiBase/releases/$($release.id)" `
-ContentType "application/json" `
-Body (@{ name = $env:RELEASE_TITLE } | ConvertTo-Json)
} else {
$release = Invoke-RestMethod `
-Method Post `
-Headers $headers `
-Uri "$apiBase/releases" `
-ContentType "application/json" `
-Body (@{
tag_name = $env:RELEASE_TAG
target_commitish = "${{ github.sha }}"
name = $env:RELEASE_TITLE
generate_release_notes = $true
} | ConvertTo-Json)
}
$encodedName = [System.Uri]::EscapeDataString($env:ASSET_NAME)
Invoke-RestMethod `
-Method Post `
-Headers $headers `
-Uri "https://uploads.github.com/repos/$env:GITHUB_REPOSITORY/releases/$($release.id)/assets?name=$encodedName" `
-ContentType "application/octet-stream" `
-InFile $assetPath