-1

TFS Build 2013 を使用しており、ビルド戦略を構成しています。可能な限りデフォルトのままにしたいので、カスタム アクティビティを使用せずにデフォルトのビルド プロセス テンプレートを使用したいと考えています。代わりに、デフォルトのテンプレートに接続されているいくつかの Powershell スクリプトがあります。これらのスクリプトの 1 つは、ビルドするプロジェクトからバージョン番号を抽出します。

問題: ビルド番号でこのバージョン番号を解析して、ビルドに適切で意味のある名前 (例: Project - 2.1.2.46) を付け、リリースごとにビルド定義を変更する必要がないようにしたい。

私が基本的に達成したいのは、ビルド スクリプト (以下) の結果がビルド番号 (フォーマット) として使用されることです。

# Enable -Verbose option
[CmdletBinding()]
# Disable parameter
# Convenience option so you can debug this script or disable it in 
# your build definition without having to remove it from
# the 'Post-build script path' build process parameter.
param([switch]$Disable)
if ($PSBoundParameters.ContainsKey('Disable'))
{
    Write-Verbose "Script disabled; no actions will be taken on the files."
}

# Regular expression pattern to find the version in the assemblies 
# and then apply it to the build number
$AssemblyFileVersionRegex = 'AssemblyFileVersion\("\d+\.\d+\.\d+\.\d+"'

# Make sure path to source code directory is available
if (-not $Env:TF_BUILD_SOURCESDIRECTORY)
{
    Write-Error ("TF_BUILD_SOURCESDIRECTORY environment variable is missing.")
    exit 1
}
elseif (-not (Test-Path $Env:TF_BUILD_SOURCESDIRECTORY))
{
    Write-Error "TF_BUILD_SOURCESDIRECTORY does not exist: $Env:TF_BUILD_SOURCESDIRECTORY"
    exit 1
}
Write-Verbose "TF_BUILD_SOURCESDIRECTORY: $Env:TF_BUILD_SOURCESDIRECTORY"

# Make sure build definition name is available
if (-not $Env:TF_BUILD_BUILDDEFINITIONNAME)
{
    Write-Error ("TF_BUILD_BUILDDEFINITIONNAME environment variable is missing.")
    exit 1
}

# Apply the version to the assembly property files
$files = gci $Env:TF_BUILD_SOURCESDIRECTORY -recurse -include "*Properties*", "Solution Items" | 
    ?{ $_.PSIsContainer } | 
    foreach { gci -Path $_.FullName -Recurse -include CommonAssemblyInfo.*, AssemblyInfo.* }
if($files)
{
    foreach ($file in $files) {

        if(-not $Disable)
        {
            $fileContent = Get-Content $file
            $found = $fileContent -match $AssemblyFileVersionRegex

            if ($found) 
            {
                Write-Verbose "Found in files: $found"

                $VersionData = $found -replace [Regex]::Escape('[assembly: AssemblyFileVersion("'), ''
                $VersionData = $VersionData -replace [Regex]::Escape('")]'), ''
                Write-Verbose "Will apply $VersionData to build number"

                $Env:TF_BUILD_BUILDNUMBER = $Env:TF_BUILD_BUILDDEFINITIONNAME + ' - ' + $VersionData
                Write-Verbose "TF_BUILD_BUILDNUMBER: $Env:TF_BUILD_BUILDNUMBER"

                exit 0
            } 
            else
            {
                Write-Warning "No version number found in files. Build number will not be changed."   
            }         
        }
    }
}
else
{
    Write-Warning "Found no files."
}

詳細ログをオンにすると、PowerShell スクリプトが TF_BUILD_BUILDNUMBER 変数の正しいバージョン番号を解析していることがわかります。これで、スクリプトが機能していることがわかります。ただし、ビルドが終了すると、ビルド番号はビルド定義で指定されたデフォルト値に戻りました。

どんな助けでも大歓迎です!

4

2 に答える 2

0

PowerShell スクリプトを変更して、最新のフォルダー (ビルド フォルダー) 名を確認できます。姓が 1.2.45 の場合、powershell スクリプトでマイナー バージョン番号を 1 つ増やすことができるとします。

于 2015-09-21T12:31:36.990 に答える