Skip to content

Commit

Permalink
[repo] Post-release workflow refactor (#5617)
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeBlanch committed May 14, 2024
1 parent cf8ca09 commit 2d2fcf9
Show file tree
Hide file tree
Showing 3 changed files with 176 additions and 138 deletions.
146 changes: 8 additions & 138 deletions .github/workflows/publish-packages-1.0.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ on:
tags:
- 'core-*'
- 'coreunstable-*'
- 'Instrumentation.*-'
workflow_call:
inputs:
tag:
Expand Down Expand Up @@ -79,87 +78,10 @@ jobs:
if: github.ref_type == 'tag' || inputs.tag
shell: pwsh
run: |
$packages = (Get-ChildItem -Path src/*/bin/Release/*.nupkg).Name
$notes = ''
$firstPackageVersion = ''
foreach ($package in $packages)
{
$match = [regex]::Match($package, '(.*)\.(\d+\.\d+\.\d+.*?)\.nupkg')
$packageName = $match.Groups[1].Value
$packageVersion = $match.Groups[2].Value
if ($firstPackageVersion -eq '')
{
$firstPackageVersion = $packageVersion
}
$changelogContent = Get-Content -Path "src/$packageName/CHANGELOG.md"
$headingWritten = $false
$started = $false
$content = ""
foreach ($line in $changelogContent)
{
if ($line -like "## $packageVersion" -and $started -ne $true)
{
$started = $true
}
elseif ($line -like "Released *" -and $started -eq $true)
{
continue
}
elseif ($line -like "## *" -and $started -eq $true)
{
break
}
else
{
if ($started -eq $true -and ([string]::IsNullOrWhitespace($line) -eq $false -or $content.Length -gt 0))
{
$content += " " + $line + "`r`n"
}
}
}
if ([string]::IsNullOrWhitespace($content) -eq $true)
{
$content = " No notable changes."
}
$content = $content.trimend()
$notes +=
@"
* NuGet: [$packageName v$packageVersion](https://www.nuget.org/packages/$packageName/$packageVersion)
$content
See [CHANGELOG](https://github.com/${{ github.repository }}/blob/${{ inputs.tag || github.ref_name }}/src/$packageName/CHANGELOG.md) for details.
"@
}
if ($firstPackageVersion -match '-alpha' -or $firstPackageVersion -match '-beta' -or $firstPackageVersion -match '-rc')
{
gh release create ${{ inputs.tag || github.ref_name }} `
--title ${{ inputs.tag || github.ref_name }} `
--verify-tag `
--notes "$notes" `
--prerelease `
--draft
}
else
{
gh release create ${{ inputs.tag || github.ref_name }} `
--title ${{ inputs.tag || github.ref_name }} `
--verify-tag `
--notes "$notes" `
--latest `
--draft
}
Import-Module .\build\scripts\post-release.psm1
CreateDraftRelease `
-tag '${{ inputs.tag || github.ref_name }}'
env:
GH_TOKEN: ${{ github.token }}

Expand All @@ -169,61 +91,9 @@ jobs:
|| (inputs.tag && startsWith(inputs.tag, 'core-') && !contains(inputs.tag, '-alpha') && !contains(inputs.tag, '-beta') && !contains(inputs.tag, '-rc'))
shell: pwsh
run: |
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git switch --create release/post-stable-${{ inputs.tag || github.ref_name }}-update main 2>&1 | % ToString
if ($LASTEXITCODE -gt 0)
{
Write-Error 'git switch failure'
Return
}
$match = [regex]::Match('${{ inputs.tag || github.ref_name }}', '.*?-(.*)')
$packageVersion = $match.Groups[1].Value
(Get-Content Directory.Packages.props) `
-replace '<OTelLatestStableVer>.*<\/OTelLatestStableVer>', "<OTelLatestStableVer>$packageVersion</OTelLatestStableVer>" |
Set-Content Directory.Packages.props
git add Directory.Packages.props 2>&1 | % ToString
if ($LASTEXITCODE -gt 0)
{
Write-Error 'git add failure'
Return
}
git commit -m "Update OTelLatestStableVer in Directory.Packages.props to $packageVersion." 2>&1 | % ToString
if ($LASTEXITCODE -gt 0)
{
Write-Error 'git commit failure'
Return
}
git push -u origin release/post-stable-${{ inputs.tag || github.ref_name }}-update 2>&1 | % ToString
if ($LASTEXITCODE -gt 0)
{
Write-Error 'git push failure'
Return
}
$body =
@"
Note: This PR was opened automatically by the [package workflow](https://github.com/${{ github.repository }}/actions/workflows/publish-packages-1.0.yml).
Merge once packages are available on NuGet and the build passes.
## Changes
* Sets `OTelLatestStableVer` in `Directory.Packages.props` to `$packageVersion`.
"@
gh pr create `
--title "[repo] Core stable release $packageVersion updates" `
--body $body `
--base main `
--head release/post-stable-${{ inputs.tag || github.ref_name }}-update `
--label infra `
--draft
Import-Module .\build\scripts\post-release.psm1
CreateStableVersionUpdatePullRequest `
-tag '${{ inputs.tag || github.ref_name }}'
env:
GH_TOKEN: ${{ github.token }}
1 change: 1 addition & 0 deletions OpenTelemetry.sln
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "scripts", "scripts", "{4498
ProjectSection(SolutionItems) = preProject
build\scripts\add-labels.ps1 = build\scripts\add-labels.ps1
build\scripts\finalize-publicapi.ps1 = build\scripts\finalize-publicapi.ps1
build\scripts\post-release.psm1 = build\scripts\post-release.psm1
build\scripts\prepare-release.psm1 = build\scripts\prepare-release.psm1
build\scripts\update-changelogs.ps1 = build\scripts\update-changelogs.ps1
EndProjectSection
Expand Down
167 changes: 167 additions & 0 deletions build/scripts/post-release.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
$gitHubBotUserName="github-actions[bot]"
$gitHubBotEmail="41898282+github-actions[bot]@users.noreply.github.com"

$repoViewResponse = gh repo view --json nameWithOwner | ConvertFrom-Json

$gitRepository = $repoViewResponse.nameWithOwner

function CreateDraftRelease {
param(
[Parameter(Mandatory=$true)][string]$tag
)

$packages = (Get-ChildItem -Path src/*/bin/Release/*.nupkg).Name

$notes = ''
$firstPackageVersion = ''

foreach ($package in $packages)
{
$match = [regex]::Match($package, '(.*)\.(\d+\.\d+\.\d+.*?)\.nupkg')
$packageName = $match.Groups[1].Value
$packageVersion = $match.Groups[2].Value

if ($firstPackageVersion -eq '')
{
$firstPackageVersion = $packageVersion
}

$changelogContent = Get-Content -Path "src/$packageName/CHANGELOG.md"

$headingWritten = $false
$started = $false
$content = ""

foreach ($line in $changelogContent)
{
if ($line -like "## $packageVersion" -and $started -ne $true)
{
$started = $true
}
elseif ($line -like "Released *" -and $started -eq $true)
{
continue
}
elseif ($line -like "## *" -and $started -eq $true)
{
break
}
else
{
if ($started -eq $true -and ([string]::IsNullOrWhitespace($line) -eq $false -or $content.Length -gt 0))
{
$content += " " + $line + "`r`n"
}
}
}

if ([string]::IsNullOrWhitespace($content) -eq $true)
{
$content = " No notable changes."
}

$content = $content.trimend()

$notes +=
@"
* NuGet: [$packageName v$packageVersion](https://www.nuget.org/packages/$packageName/$packageVersion)
$content
See [CHANGELOG](https://github.com/$gitRepository/blob/$tag/src/$packageName/CHANGELOG.md) for details.
"@
}

if ($firstPackageVersion -match '-alpha' -or $firstPackageVersion -match '-beta' -or $firstPackageVersion -match '-rc')
{
gh release create $tag `
--title $tag `
--verify-tag `
--notes $notes `
--prerelease `
--draft
}
else
{
gh release create $tag `
--title $tag `
--verify-tag `
--notes $notes `
--latest `
--draft
}
}

Export-ModuleMember -Function CreateDraftRelease

function CreateStableVersionUpdatePullRequest {
param(
[Parameter(Mandatory=$true)][string]$tag,
[Parameter()][string]$gitUserName=$gitHubBotUserName,
[Parameter()][string]$gitUserEmail=$gitHubBotEmail,
[Parameter()][string]$targetBranch="main"
)

$match = [regex]::Match($tag, '.*?-(.*)')
if ($match.Success -eq $false)
{
throw 'Could not parse version from tag'
}

$packageVersion = $match.Groups[1].Value

$branch="release/post-stable-${tag}-update"

git config user.name $gitUserName
git config user.email $gitUserEmail

git switch --create $branch origin/$targetBranch --no-track 2>&1 | % ToString
if ($LASTEXITCODE -gt 0)
{
throw 'git switch failure'
}

(Get-Content Directory.Packages.props) `
-replace '<OTelLatestStableVer>.*<\/OTelLatestStableVer>', "<OTelLatestStableVer>$packageVersion</OTelLatestStableVer>" |
Set-Content Directory.Packages.props

git add Directory.Packages.props 2>&1 | % ToString
if ($LASTEXITCODE -gt 0)
{
throw 'git add failure'
}

git commit -m "Update OTelLatestStableVer in Directory.Packages.props to $packageVersion." 2>&1 | % ToString
if ($LASTEXITCODE -gt 0)
{
throw 'git commit failure'
}

git push -u origin $branch 2>&1 | % ToString
if ($LASTEXITCODE -gt 0)
{
throw 'git push failure'
}

$body =
@"
Note: This PR was opened automatically by the [package workflow](https://github.com/$gitRepository/actions/workflows/publish-packages-1.0.yml).
Merge once packages are available on NuGet and the build passes.
## Changes
* Sets ``OTelLatestStableVer`` in ``Directory.Packages.props`` to ``$packageVersion``.
"@

gh pr create `
--title "[repo] Core stable release $packageVersion updates" `
--body $body `
--base $targetBranch `
--head $branch `
--label infra `
--draft
}

Export-ModuleMember -Function CreateStableVersionUpdatePullRequest

0 comments on commit 2d2fcf9

Please sign in to comment.