-
Notifications
You must be signed in to change notification settings - Fork 4
156 lines (139 loc) · 6.61 KB
/
dotnet-build-and-release.yml
File metadata and controls
156 lines (139 loc) · 6.61 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
name: Build dotnet solution and potentially upload the release
on:
workflow_call:
inputs:
release_version:
description: The release version to use when publishing a build
required: false
type: string
default: 1.0.0 # for non-released builds
release_url:
description: The url to upload a published release
required: false # leave empty for non-release build
type: string
release_dir:
description: The relative directory inside the repo where the build artifacts to publish for release will be located
required: true
type: string
release_project:
description: The relative file location for csproj
required: false
type: string
integration_type:
description: The extension integration_type
required: true
type: string
secrets:
token:
description: 'Secret token from caller workflow to access private packages'
required: true
jobs:
dotnet-build-and-release:
runs-on: windows-latest
steps:
- uses: keyfactor/checkout@v4
- name: Setup dotnet
uses: keyfactor/setup-dotnet@v4
with:
dotnet-version: |
3.1.x
6.0.x
8.0.x
- name: Setup MSBuild
uses: keyfactor/setup-msbuild@v2
- name: Setup build environment
id: setup_env
run: |
$repoName = "${{ github.repository }}".Split("/")
echo "Repo Name: $($repoName[-1])"
echo "REPO_NAME=$($repoName[-1])" | Out-File $env:GITHUB_ENV -Encoding utf8 -Append
$slnPath = (Get-ChildItem -Include *.sln -File -Recurse).fullname
echo "Solution File Path: ${slnPath}"
echo "SOLUTION_PATH=${slnPath}" | Out-File $env:GITHUB_ENV -Encoding utf8 -Append
echo "release_dir: ${{ inputs.release_dir }}"
$creatingRelease = "${{ inputs.release_url }}".Trim().Length -gt 0
echo "Flagged to create release: ${creatingRelease}"
echo "CREATE_RELEASE=${creatingRelease}" | Out-File $env:GITHUB_ENV -Encoding utf8 -Append
$isPreRelease = "${{ github.base_ref }}".StartsWith("release-") -and [System.Convert]::ToBoolean("${{ github.event_name == 'pull_request' && (github.event.action == 'opened' || github.event.action == 'synchronize') }}")
echo "Pre-release flagged: $($isPreRelease)"
echo "IS_PRE_RELEASE=$($isPreRelease)" | Out-File $env:GITHUB_ENV -Encoding utf8 -Append
dotnet nuget add source https://nuget.pkg.github.com/Keyfactor/index.json -n github -u ${{ github.actor }} -p ${{ secrets.token }} --store-password-in-clear-text
nuget restore $slnPath -Project2ProjectTimeout 240
- name: Increment Assembly Version
if: env.CREATE_RELEASE == 'True'
run: |
$VersionRegex = "\d+\.\d+\.\d+"
$assemblyInfoFiles = (Get-ChildItem -Include AssemblyInfo.cs -File -Recurse).fullname
if ($assemblyInfoFiles -ne $null)
{
$newVer = "${{ inputs.release_version || '1.0.0' }}".TrimStart('v').Split('-')[0]
echo "newver=${newVer}" | Out-File $env:GITHUB_ENV -Encoding utf8 -Append
echo "Prepared to overwrite Assembly version to: ${newVer}"
foreach ($assemblyInfoFile in $assemblyInfoFiles)
{
$filecontent = Get-Content($assemblyInfoFile)
attrib $assemblyInfoFile -r
$filecontent -replace $VersionRegex, $newVer | Out-File $assemblyInfoFile
}
}
- name: Execute MSBuild Commands
run: |
$newVer = "${{ inputs.release_version || '1.0.0' }}".TrimStart('v').Split('-')[0]
MSBuild.exe $Env:SOLUTION_PATH -p:RestorePackagesConfig=false -p:Configuration=Release -p:Version=$newVer
- name: Read Target Frameworks
id: read_target_frameworks
shell: pwsh
run: |
[xml]$csproj = Get-Content "${{ inputs.release_project }}"
$targetFrameworks = $csproj.Project.PropertyGroup.TargetFrameworks
if ($null -eq $targetFrameworks) {
$targetFrameworks = $csproj.Project.PropertyGroup.TargetFramework
}
echo "release_platforms: $targetFrameworks"
echo "release_platforms=$targetFrameworks" | Out-File $env:GITHUB_ENV -Encoding utf8 -Append
- name: Archive Files
if: success() && env.CREATE_RELEASE == 'True'
shell: pwsh
run: |
$platforms = "${{ env.release_platforms }}".Split(';')
$outputDir = "${{ github.workspace }}\zip\Keyfactor"
echo "outputDir=$outputDir" | Out-File $env:GITHUB_ENV -Encoding utf8 -Append
if (Test-Path $outputDir) {
Remove-Item -Recurse -Force $outputDir
}
md $outputDir
foreach ($platform in $platforms) {
$platform = $platform.Trim()
$sourcePath = "${{ github.workspace }}\${{ inputs.release_dir }}\$platform\"
$zipPath = "$outputDir\${{ env.REPO_NAME }}_${{ inputs.release_version }}_$platform.zip"
Get-ChildItem -File -Path $sourcePath
Compress-Archive -Path $sourcePath -DestinationPath $zipPath -Force -Verbose
}
Get-ChildItem -File -Path $outputDir
$buildFiles = Get-ChildItem -File -Path $outputDir -Name
$releaseArtifacts
foreach ($zipFile in $buildFiles) {
$releaseArtifacts = $releaseArtifacts + $outputDir + "\" + $zipFile + [Environment]::NewLine
}
echo "release_artifacts: $releaseArtifacts"
echo "Writing in as multiline github env variable"
echo "release_artifacts<<EOF" >> $env:GITHUB_ENV
echo "$releaseArtifacts" >> $env:GITHUB_ENV
echo "EOF" >> $env:GITHUB_ENV
- name: Release Asset(s)
uses: softprops/action-gh-release@v2
if: success() && env.CREATE_RELEASE == 'True'
with:
repository: ${{ github.repository }}
files: |
${{ env.release_artifacts }}
append_body: true
tag_name: ${{ inputs.release_version }}
token: ${{ secrets.token }}
# - name: Delete Failed Release
# if: failure() && env.CREATE_RELEASE == 'True'
# id: delete-failed-release
# env:
# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# run: |
# gh release delete ${{ inputs.release_version }} --yes --cleanup-tag