PowerShell スクリプトを使用してパイプラインをカスタマイズする
Azure DevOps Services | Azure DevOps Server 2020 | Azure DevOps Server 2019 | TFS 2018
注意
Microsoft Team Foundation Server (TFS) 2018 以前のバージョンでは、ビルドとリリースの "パイプライン" は "定義"、"実行" は "ビルド"、"サービス接続" は "サービス エンドポイント"、"ステージ" は "環境"、"ジョブ" は "フェーズ" と呼ばれます。
コードのコンパイルとテストの基本を超える準備ができたら、PowerShell スクリプトを使用して、チームのビジネス ロジックをビルド パイプラインに追加します。
PowerShell スクリプトを追加する
PowerShell Core を含める構文は、Windows PowerShellの構文とは若干異なります。 Windows ビルド エージェントでWindows PowerShellを実行できます。 PowerShell Core は、任意のプラットフォームで実行されます。
PowerShell スクリプトをリポジトリにプッシュします。
ステップを
pwsh追加しますpowershell。 キーワードはpwsh、PowerShell Core の PowerShell タスク のショートカットです。 キーワードはpowershellPowerShell タスクのもう 1 つのショートカットですが、Windows PowerShellされ、Windows エージェントでのみ機能します。
# for PowerShell Core
steps:
- pwsh: ./my-script.ps1
# for Windows PowerShell
steps:
- powershell: .\my-script.ps1
Windows ビルド エージェントで Windows PowerShell スクリプトを実行できます。
スクリプトをリポジトリにプッシュします。
PowerShell ビルド タスクを追加します。


ビルド タスクを実行する場所にドラッグします。
スクリプトの名前を指定します。
PowerShell スクリプトの例: バージョン アセンブリ
アセンブリのバージョンを設定するスクリプトの例を次に示します。 スクリプトを正常に実行するには、4 つのピリオド (例: $(BuildDefinitionName)_$(Year:yyyy).$(Month).$(DayOfMonth)$(Rev:.r)) の形式を使用するようにビルド番号を更新する必要があります。 ビルド番号は、実行番号とも呼ばれます。
プロパティ を使用して 、YAML パイプライン内でビルド番号を name カスタマイズできます。
name: $(BuildDefinitionName)_$(Year:yyyy).$(Month).$(DayOfMonth)$(Rev:.r)
pool:
vmImage: windows-latest
steps:
- script: echo $(Build.BuildNumber) //output updated build number
# If found use it to version the assemblies.
#
# For example, if the 'Build number format' build pipeline parameter
# $(BuildDefinitionName)_$(Year:yyyy).$(Month).$(DayOfMonth)$(Rev:.r)
# then your build numbers come out like this:
# "Build HelloWorld_2013.07.19.1"
# This script would then apply version 2013.07.19.1 to your assemblies.
# Enable -Verbose option
[CmdletBinding()]
# Regular expression pattern to find the version in the build number
# and then apply it to the assemblies
$VersionRegex = "\d+\.\d+\.\d+\.\d+"
# If this script is not running on a build server, remind user to
# set environment variables so that this script can be debugged
if(-not ($Env:BUILD_SOURCESDIRECTORY -and $Env:BUILD_BUILDNUMBER))
{
Write-Error "You must set the following environment variables"
Write-Error "to test this script interactively."
Write-Host '$Env:BUILD_SOURCESDIRECTORY - For example, enter something like:'
Write-Host '$Env:BUILD_SOURCESDIRECTORY = "C:\code\FabrikamTFVC\HelloWorld"'
Write-Host '$Env:BUILD_BUILDNUMBER - For example, enter something like:'
Write-Host '$Env:BUILD_BUILDNUMBER = "Build HelloWorld_0000.00.00.0"'
exit 1
}
# Make sure path to source code directory is available
if (-not $Env:BUILD_SOURCESDIRECTORY)
{
Write-Error ("BUILD_SOURCESDIRECTORY environment variable is missing.")
exit 1
}
elseif (-not (Test-Path $Env:BUILD_SOURCESDIRECTORY))
{
Write-Error "BUILD_SOURCESDIRECTORY does not exist: $Env:BUILD_SOURCESDIRECTORY"
exit 1
}
Write-Verbose "BUILD_SOURCESDIRECTORY: $Env:BUILD_SOURCESDIRECTORY"
# Make sure there is a build number
if (-not $Env:BUILD_BUILDNUMBER)
{
Write-Error ("BUILD_BUILDNUMBER environment variable is missing.")
exit 1
}
Write-Verbose "BUILD_BUILDNUMBER: $Env:BUILD_BUILDNUMBER"
# Get and validate the version data
$VersionData = [regex]::matches($Env:BUILD_BUILDNUMBER,$VersionRegex)
switch($VersionData.Count)
{
0
{
Write-Error "Could not find version number data in BUILD_BUILDNUMBER."
exit 1
}
1 {}
default
{
Write-Warning "Found more than instance of version data in BUILD_BUILDNUMBER."
Write-Warning "Will assume first instance is version."
}
}
$NewVersion = $VersionData[0]
Write-Verbose "Version: $NewVersion"
# Apply the version to the assembly property files
$files = gci $Env:BUILD_SOURCESDIRECTORY -recurse -include "*Properties*","My Project" |
?{ $_.PSIsContainer } |
foreach { gci -Path $_.FullName -Recurse -include AssemblyInfo.* }
if($files)
{
Write-Verbose "Will apply $NewVersion to $($files.count) files."
foreach ($file in $files) {
$filecontent = Get-Content($file)
attrib $file -r
$filecontent -replace $VersionRegex, $NewVersion | Out-File $file
Write-Verbose "$file.FullName - version applied"
}
}
else
{
Write-Warning "Found no files."
}
PowerShell スクリプトの例: REST API へのアクセス
この例では、変数をSYSTEM_ACCESSTOKEN使用して Azure Pipelines REST API にアクセスします。
YAML パイプラインのスクリプトで OAuth トークンにアクセスするために使用 $env:SYSTEM_ACCESSTOKEN できます。
- task: PowerShell@2
inputs:
targetType: 'inline'
script: |
$url = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$env:SYSTEM_TEAMPROJECTID/_apis/build/definitions/$($env:SYSTEM_DEFINITIONID)?api-version=5.0"
Write-Host "URL: $url"
$pipeline = Invoke-RestMethod -Uri $url -Headers @{
Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"
}
Write-Host "Pipeline = $($pipeline | ConvertTo-Json -Depth 100)"
env:
SYSTEM_ACCESSTOKEN: $(System.AccessToken)
よく寄せられる質問
スクリプトで使用できる変数は何ですか?
スクリプトで定義済みの変数を使用できます。 使用可能な変数とその使用方法の詳細については、「 定義済みの変数を使用する」を参照してください。
操作方法後続のスクリプトやタスクで読み取ることができるように変数を設定しますか?
スクリプトでのビルド変数の定義の詳細については、「スクリプトで ビルド変数を定義および変更する」を参照してください。
スクリプトでのリリース変数の定義の詳細については、「スクリプトでのリリース変数の定義と変更」を参照してください。
ビルドはどのブランチを実行しますか?
ビルドでは、コードのアクティブなブランチが使用されます。 パイプラインの実行でブランチが使用されている main 場合、スクリプトではブランチも使用 main されます。
どのような種類のパラメーターを使用できますか?
名前付きパラメーターを使用できます。 スイッチ パラメーターなど、他の種類のパラメーターはサポートされていません。 スイッチ パラメーターを使用しようとすると、エラーが表示されます。
TFS をオンプレミスで使用していますが、これらの機能の一部が表示されません。 なぜでしょうか。
これらの機能の一部は Azure Pipelines でのみ使用でき、オンプレミスではまだ使用できません。 TFS の最新バージョンにアップグレードした場合は、一部の機能をオンプレミスで使用できます。



