VSTS - 替换

时间:2017-09-25 19:11:12

标签: azure-devops azure-pipelines-release-pipeline

寻找在发布时替换配置令牌的演练。我已经尝试了几个令牌替换任务/扩展,但只是不能让它们中的任何一个工作。我是VSTS的新手,所以他们可能希望获得某种程度的专业知识,或者只是知道在步骤x&年。我试过了

IIS Web App部署(开箱即用) 仅适用于某些部分,我必须对实际的web.config进行标记化(希望将web.config保持非标记化以进行本地调试)

Azure App Service Deploy(开箱即用)

按总计ALM进行标记

通过科林的ALM角落替换代币

由Guillaume Rouchon替换代币

而最后3名刚刚开始工作。任何人都有这方面的经验或知道一个演练?

1 个答案:

答案 0 :(得分:0)

最后3个工作是通过从发布定义的变量部分注入值,坦率地说我不喜欢句号 - 我更喜欢将应用程序配置保持为代码(密码除外),从源代码驱动控制配置文件。然后,我通过PowerShell进行令牌替换,作为应用程序部署脚本的一部分。

这是我编写的一个函数,通常用于执行令牌替换。如果将它放在PowerShell模块中,它可以很好地工作,但作为一个独立的功能可以很好。

function Invoke-ReplaceTokens {
param(
[string]$FileSpec,
[string]$FilePath,
[HashTable]$Values)

    try {
        $files = gci -Path $FilePath -Filter $FileSpec -Recurse
        Write-Output "Found files:"

        Write-Output ($files | select-object -expandproperty FullName)
        $files | % {
            $contents = Get-Content $_.FullName -Raw
            $changedContents = $false
            foreach ($key in $Values.Keys) {
                if ($contents.Contains($key)) {
                    Write-Output "$($_.FullName) contains `"$key`". Replacing it with `"$($Values[$key])`""
                    $contents = $contents.Replace($key, $Values[$key])
                    $changedContents = $true
                }
            }
            if ($changedContents) {
                set-content -Path $_.FullName -Value $contents
            }
        }
    }
    catch {
        Write-Output $_
        throw $_
    }
}

部署脚本中的用法:

Invoke-ReplaceTokens -FileSpec '*.config' -FilePath 'C:\Location\Of\My\Config\File' -Values @{ '__Token__' = 'ActualValue' }

此外,您提出的问题是Stack Overflow的偏离主题(异地资源建议偏离主题),所以我回答了您最初问题的自然演变:“配置文件的好方法是什么?部署期间的管理?“