Create changelog artifact in TeamCity

前端 未结 4 1583
半阙折子戏
半阙折子戏 2021-02-01 07:40

Is there a simple way to have TeamCity include a text or html change-log as one of its output artifacts?

Perhaps I need to go down the route of having msbuild or some ot

4条回答
  •  盖世英雄少女心
    2021-02-01 08:09

    You can generate a change log via the REST API of TeamCity. A PowerShell script for this can be found here

    For TeamCity v10.x & above:

    <#
    .SYNOPSIS
        Generates a project change log file.
    .LINK
        Script posted over:
        http://open.bekk.no/generating-a-project-change-log-with-teamcity-and-powershell
        Also See https://stackoverflow.com/questions/4317409/create-changelog-artifact-in-teamcity
    
    #>
    
    # Where the changelog file will be created
    $outputFile = "%system.teamcity.build.tempDir%\releasenotesfile_%teamcity.build.id%.txt"
    # the url of teamcity server
    $teamcityUrl = "%teamcity.serverUrl%"
    # username/password to access Teamcity REST API
    $authToken=[Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("%system.teamcity.auth.userId%:%system.teamcity.auth.password%"))
    # Build id for the release notes
    $buildId = %teamcity.build.id%
    
    # Get the commit messages for the specified change id
    # Ignore messages containing #ignore
    # Ignore empty lines
    Function GetCommitMessages($changeid)
    {
        $request = [System.Net.WebRequest]::Create("$teamcityUrl/httpAuth/app/rest/changes/id:$changeid")     
        $request.Headers.Add("AUTHORIZATION", "Basic $authToken");
        $xml = [xml](new-object System.IO.StreamReader $request.GetResponse().GetResponseStream()).ReadToEnd()    
        Microsoft.PowerShell.Utility\Select-Xml $xml -XPath "/change" |
            where { ($_.Node["comment"].InnerText.Length -ne 0) -and (-Not $_.Node["comment"].InnerText.Contains('#ignore'))} |
            foreach {"+ $($_.Node["user"].name) : $($_.Node["comment"].InnerText.Trim().Replace("`n"," "))`n"}
    }
    
    # Grab all the changes
    $request = [System.Net.WebRequest]::Create("$teamcityUrl/httpAuth/app/rest/changes?build=id:$($buildId)")
    $request.Headers.Add("AUTHORIZATION", "Basic $authToken");
    $xml = [xml](new-object System.IO.StreamReader $request.GetResponse().GetResponseStream()).ReadToEnd()
    
    # Then get all commit messages for each of them
    $changelog = Microsoft.PowerShell.Utility\Select-Xml $xml -XPath "/changes/change" | Foreach {GetCommitMessages($_.Node.id)}
    $changelog > $outputFile
    Write-Host "Changelog saved to ${outputFile}:"
    $changelog
    

    For versions before Teamcity v10.x:

    <#
    .SYNOPSIS
        Generates a project change log file.
    .LINK
        Script posted over:
        http://open.bekk.no/generating-a-project-change-log-with-teamcity-and-powershell
        Also See https://stackoverflow.com/questions/4317409/create-changelog-artifact-in-teamcity
    #>
    
    # Where the changelog file will be created
    $outputFile = "%system.teamcity.build.tempDir%\releasenotesfile_%teamcity.build.id%.txt"
    # the url of teamcity server
    $teamcityUrl = "%teamcity.serverUrl%"
    # username/password to access Teamcity REST API
    $authToken=[Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("%system.teamcity.auth.userId%:%system.teamcity.auth.password%"))
    # Build id for the release notes
    $buildId = %teamcity.build.id%
    
    # Get the commit messages for the specified change id
    # Ignore messages containing #ignore
    # Ignore empty lines
    Function GetCommitMessages($changeid)
    {
        $request = [System.Net.WebRequest]::Create("$teamcityUrl/httpAuth/app/rest/changes/id:$changeid")     
        $request.Headers.Add("AUTHORIZATION", "$authToken");
        $xml = [xml](new-object System.IO.StreamReader $request.GetResponse().GetResponseStream()).ReadToEnd()    
        Microsoft.PowerShell.Utility\Select-Xml $xml -XPath "/change" |
            where { ($_.Node["comment"].InnerText.Length -ne 0) -and (-Not $_.Node["comment"].InnerText.Contains('#ignore'))} |
            foreach {"+ $($_.Node["user"].name) : $($_.Node["comment"].InnerText.Trim().Replace("`n"," "))`n"}
    }
    
    # Grab all the changes
    $request = [System.Net.WebRequest]::Create("$teamcityUrl/httpAuth/app/rest/changes?build=id:$($buildId)")
    $request.Headers.Add("AUTHORIZATION", "$authToken");
    $xml = [xml](new-object System.IO.StreamReader $request.GetResponse().GetResponseStream()).ReadToEnd()
    
    # Then get all commit messages for each of them
    $changelog = Microsoft.PowerShell.Utility\Select-Xml $xml -XPath "/changes/change" | Foreach {GetCommitMessages($_.Node.id)}
    $changelog > $outputFile
    Write-Host "Changelog saved to ${outputFile}:"
    $changelog
    

提交回复
热议问题