|
| 1 | +name: Build Project |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [ main ] |
| 6 | + pull_request: |
| 7 | + branches: [ main ] |
| 8 | + |
| 9 | +jobs: |
| 10 | + build: |
| 11 | + runs-on: windows-latest |
| 12 | + permissions: |
| 13 | + actions: write |
| 14 | + contents: read |
| 15 | + |
| 16 | + steps: |
| 17 | + - name: Checkout repository |
| 18 | + uses: actions/checkout@v4 |
| 19 | + |
| 20 | + - name: Setup .NET 8 |
| 21 | + uses: actions/setup-dotnet@v4 |
| 22 | + with: |
| 23 | + dotnet-version: '8.0.x' |
| 24 | + |
| 25 | + - name: Restore dependencies |
| 26 | + run: dotnet restore |
| 27 | + |
| 28 | + - name: Publish Project |
| 29 | + if: github.event_name == 'push' |
| 30 | + run: | |
| 31 | + dotnet publish ` |
| 32 | + --configuration Release ` |
| 33 | + --output ./publish ` |
| 34 | + --self-contained false ` |
| 35 | + /p:Platform=x64 |
| 36 | +
|
| 37 | + - name: Package Output |
| 38 | + if: github.event_name == 'push' |
| 39 | + id: package |
| 40 | + shell: pwsh |
| 41 | + run: | |
| 42 | + $publishPath = "${{ github.workspace }}\publish" |
| 43 | + |
| 44 | + # 验证输出目录存在 |
| 45 | + if (-not (Test-Path $publishPath)) { |
| 46 | + Write-Error "Publish 目录不存在: $publishPath" |
| 47 | + exit 1 |
| 48 | + } |
| 49 | + |
| 50 | + Write-Host "=== 打包目录内容 ===" |
| 51 | + Get-ChildItem -Path $publishPath -Recurse | Select-Object FullName, Length | Format-Table -AutoSize |
| 52 | + |
| 53 | + # 生成带版本号的文件名 |
| 54 | + $timestamp = Get-Date -Format "yyyyMMdd-HHmmss" |
| 55 | + $commitSha = "${{ github.sha }}".Substring(0, 7) |
| 56 | + $artifactName = "build-${timestamp}-${commitSha}" |
| 57 | + |
| 58 | + # 打包为 zip 然后重命名为 .cipx |
| 59 | + $zipPath = "${{ github.workspace }}\${artifactName}.zip" |
| 60 | + $cipxPath = "${{ github.workspace }}\${artifactName}.cipx" |
| 61 | + |
| 62 | + # 压缩 publish 文件夹下的所有内容(包括 dll、配置文件、资源文件等) |
| 63 | + Compress-Archive -Path "$publishPath\*" -DestinationPath $zipPath -Force |
| 64 | + Rename-Item -Path $zipPath -NewName $cipxPath |
| 65 | + |
| 66 | + Write-Output "artifact_name=$artifactName" >> $env:GITHUB_OUTPUT |
| 67 | + Write-Output "artifact_path=$cipxPath" >> $env:GITHUB_OUTPUT |
| 68 | + Write-Output "Created: $cipxPath" |
| 69 | + |
| 70 | + Write-Host "=== 最终产物信息 ===" |
| 71 | + Get-Item $cipxPath | Select-Object Name, Length, LastWriteTime | Format-List |
| 72 | +
|
| 73 | + - name: Cleanup Old Artifacts |
| 74 | + if: github.event_name == 'push' |
| 75 | + uses: actions/github-script@v7 |
| 76 | + with: |
| 77 | + script: | |
| 78 | + const keepCount = 6; |
| 79 | + const artifactPrefix = 'build-'; |
| 80 | + |
| 81 | + const { data: artifacts } = await github.rest.actions.listArtifactsForRepo({ |
| 82 | + owner: context.repo.owner, |
| 83 | + repo: context.repo.repo, |
| 84 | + per_page: 100 |
| 85 | + }); |
| 86 | + |
| 87 | + const buildArtifacts = artifacts.artifacts |
| 88 | + .filter(a => a.name.startsWith(artifactPrefix)) |
| 89 | + .sort((a, b) => new Date(b.created_at) - new Date(a.created_at)); |
| 90 | + |
| 91 | + console.log(`Found ${buildArtifacts.length} build artifacts, keeping ${keepCount}`); |
| 92 | + |
| 93 | + const toDelete = buildArtifacts.slice(keepCount); |
| 94 | + for (const artifact of toDelete) { |
| 95 | + console.log(`Deleting old artifact: ${artifact.name} (ID: ${artifact.id}, created: ${artifact.created_at})`); |
| 96 | + await github.rest.actions.deleteArtifact({ |
| 97 | + owner: context.repo.owner, |
| 98 | + repo: context.repo.repo, |
| 99 | + artifact_id: artifact.id |
| 100 | + }); |
| 101 | + } |
| 102 | +
|
| 103 | + - name: Upload Artifact |
| 104 | + if: github.event_name == 'push' |
| 105 | + uses: actions/upload-artifact@v4 |
| 106 | + with: |
| 107 | + name: ${{ steps.package.outputs.artifact_name }} |
| 108 | + path: ${{ steps.package.outputs.artifact_path }} |
| 109 | + retention-days: 30 |
| 110 | + if-no-files-found: error |
0 commit comments