|
| 1 | +name: Release |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + tags: |
| 8 | + - 'v*' |
| 9 | + workflow_dispatch: |
| 10 | + inputs: |
| 11 | + tag: |
| 12 | + description: Release tag, for example v26.07.02 |
| 13 | + required: true |
| 14 | + type: string |
| 15 | + |
| 16 | +permissions: |
| 17 | + contents: write |
| 18 | + |
| 19 | +jobs: |
| 20 | + windows-release: |
| 21 | + name: Build Windows exe and publish release |
| 22 | + runs-on: windows-latest |
| 23 | + |
| 24 | + steps: |
| 25 | + - name: Checkout |
| 26 | + uses: actions/checkout@v4 |
| 27 | + |
| 28 | + - name: Set up Node.js |
| 29 | + uses: actions/setup-node@v4 |
| 30 | + with: |
| 31 | + node-version: 24 |
| 32 | + |
| 33 | + - name: Set up pnpm |
| 34 | + run: | |
| 35 | + corepack enable |
| 36 | + corepack prepare pnpm@11.7.0 --activate |
| 37 | +
|
| 38 | + - name: Install dependencies |
| 39 | + run: pnpm install --frozen-lockfile |
| 40 | + |
| 41 | + - name: Run tests |
| 42 | + run: pnpm test |
| 43 | + |
| 44 | + - name: Build desktop executable |
| 45 | + env: |
| 46 | + CSC_IDENTITY_AUTO_DISCOVERY: false |
| 47 | + run: pnpm run release:desktop |
| 48 | + |
| 49 | + - name: Resolve release tag |
| 50 | + id: release |
| 51 | + shell: pwsh |
| 52 | + env: |
| 53 | + EVENT_NAME: ${{ github.event_name }} |
| 54 | + INPUT_TAG: ${{ inputs.tag }} |
| 55 | + REF_TYPE: ${{ github.ref_type }} |
| 56 | + REF_NAME: ${{ github.ref_name }} |
| 57 | + run: | |
| 58 | + [Console]::OutputEncoding = [System.Text.Encoding]::UTF8 |
| 59 | + $OutputEncoding = [System.Text.Encoding]::UTF8 |
| 60 | + $package = Get-Content package.json -Raw | ConvertFrom-Json |
| 61 | + if ($env:EVENT_NAME -eq 'workflow_dispatch' -and $env:INPUT_TAG) { |
| 62 | + $tag = $env:INPUT_TAG |
| 63 | + } elseif ($env:REF_TYPE -eq 'tag') { |
| 64 | + $tag = $env:REF_NAME |
| 65 | + } else { |
| 66 | + $tag = "v$($package.releaseVersion)" |
| 67 | + } |
| 68 | +
|
| 69 | + if (-not $tag.StartsWith('v')) { |
| 70 | + $tag = "v$tag" |
| 71 | + } |
| 72 | +
|
| 73 | + "tag=$tag" | Out-File -FilePath $env:GITHUB_OUTPUT -Append -Encoding utf8 |
| 74 | + "title=[FileOrganizer](https://github.com/cimorn/FileOrganizer)-$tag" | Out-File -FilePath $env:GITHUB_OUTPUT -Append -Encoding utf8 |
| 75 | + "sourceAssetName=文件整理-$tag.exe" | Out-File -FilePath $env:GITHUB_OUTPUT -Append -Encoding utf8 |
| 76 | + "assetName=FileOrganizer-$tag.exe" | Out-File -FilePath $env:GITHUB_OUTPUT -Append -Encoding utf8 |
| 77 | +
|
| 78 | + - name: Create or update GitHub release |
| 79 | + shell: pwsh |
| 80 | + env: |
| 81 | + GH_TOKEN: ${{ github.token }} |
| 82 | + RELEASE_TAG: ${{ steps.release.outputs.tag }} |
| 83 | + RELEASE_TITLE: ${{ steps.release.outputs.title }} |
| 84 | + SOURCE_ASSET_NAME: ${{ steps.release.outputs.sourceAssetName }} |
| 85 | + ASSET_NAME: ${{ steps.release.outputs.assetName }} |
| 86 | + run: | |
| 87 | + $ErrorActionPreference = 'Stop' |
| 88 | + [Console]::OutputEncoding = [System.Text.Encoding]::UTF8 |
| 89 | + $OutputEncoding = [System.Text.Encoding]::UTF8 |
| 90 | + $assetPath = Join-Path "dist" $env:SOURCE_ASSET_NAME |
| 91 | + if (-not (Test-Path -LiteralPath $assetPath)) { |
| 92 | + throw "Release asset not found: $assetPath" |
| 93 | + } |
| 94 | +
|
| 95 | + $headers = @{ |
| 96 | + Authorization = "Bearer $env:GH_TOKEN" |
| 97 | + Accept = "application/vnd.github+json" |
| 98 | + "X-GitHub-Api-Version" = "2022-11-28" |
| 99 | + } |
| 100 | + $apiBase = "https://api.github.com/repos/$env:GITHUB_REPOSITORY" |
| 101 | + $release = $null |
| 102 | +
|
| 103 | + try { |
| 104 | + $release = Invoke-RestMethod -Headers $headers -Uri "$apiBase/releases/tags/$env:RELEASE_TAG" |
| 105 | + } catch { |
| 106 | + if ($_.Exception.Response.StatusCode.value__ -ne 404) { |
| 107 | + throw |
| 108 | + } |
| 109 | + } |
| 110 | +
|
| 111 | + if ($release) { |
| 112 | + foreach ($releaseAsset in $release.assets) { |
| 113 | + if ($releaseAsset.name.EndsWith('.exe')) { |
| 114 | + Invoke-RestMethod -Method Delete -Headers $headers -Uri "$apiBase/releases/assets/$($releaseAsset.id)" |
| 115 | + } |
| 116 | + } |
| 117 | + $release = Invoke-RestMethod ` |
| 118 | + -Method Patch ` |
| 119 | + -Headers $headers ` |
| 120 | + -Uri "$apiBase/releases/$($release.id)" ` |
| 121 | + -ContentType "application/json" ` |
| 122 | + -Body (@{ name = $env:RELEASE_TITLE } | ConvertTo-Json) |
| 123 | + } else { |
| 124 | + $release = Invoke-RestMethod ` |
| 125 | + -Method Post ` |
| 126 | + -Headers $headers ` |
| 127 | + -Uri "$apiBase/releases" ` |
| 128 | + -ContentType "application/json" ` |
| 129 | + -Body (@{ |
| 130 | + tag_name = $env:RELEASE_TAG |
| 131 | + target_commitish = "${{ github.sha }}" |
| 132 | + name = $env:RELEASE_TITLE |
| 133 | + generate_release_notes = $true |
| 134 | + } | ConvertTo-Json) |
| 135 | + } |
| 136 | +
|
| 137 | + $encodedName = [System.Uri]::EscapeDataString($env:ASSET_NAME) |
| 138 | + Invoke-RestMethod ` |
| 139 | + -Method Post ` |
| 140 | + -Headers $headers ` |
| 141 | + -Uri "https://uploads.github.com/repos/$env:GITHUB_REPOSITORY/releases/$($release.id)/assets?name=$encodedName" ` |
| 142 | + -ContentType "application/octet-stream" ` |
| 143 | + -InFile $assetPath |
0 commit comments