init_build.ps1 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #-------------------------------------------------------------------------------------------------------
  2. # Copyright (C) Microsoft. All rights reserved.
  3. # Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. #-------------------------------------------------------------------------------------------------------
  5. # Init Build Script
  6. #
  7. # Run this as the very first step in the build to configure the environment.
  8. # This is distinct from the Pre-Build script as there may be more non-script steps that need to be
  9. # taken before setting up and running the build.
  10. # For example, this script creates a cmd script which should be run to initialize environment variables
  11. # before running the Pre-Build script.
  12. param (
  13. [string]$envConfigScript = "ComputedEnvironment.cmd",
  14. [string[]]$supportedPogoBuildTypes = @("x64_release", "x86_release"),
  15. [Parameter(Mandatory=$True)]
  16. [string]$oauth
  17. )
  18. # If $Env:BuildType is specified, extract BuildPlatform and BuildConfiguration
  19. # Otherwise, if $Env:BuildPlatform and $Env:BuildConfiguration are specified, construct $BuildType
  20. $BuildPlatform = $Env:BuildPlatform
  21. $BuildConfiguration = $Env:BuildConfiguration
  22. $BuildType = $Env:BuildType
  23. $BuildSubtype = "default" # will remain as "default" or become e.g. "pogo", "codecoverage"
  24. if (Test-Path Env:\BuildType) {
  25. $BuildType = $Env:BuildType
  26. $buildTypeSegments = $BuildType.split("_")
  27. $BuildPlatform = $buildTypeSegments[0]
  28. $BuildConfiguration = $buildTypeSegments[1]
  29. if ($buildTypeSegments[2]) {
  30. # overwrite with new value if it exists, otherwise keep as "default"
  31. $BuildSubtype = $buildTypeSegments[2]
  32. }
  33. if ($BuildConfiguration -eq "codecoverage") {
  34. $BuildConfiguration = "test" # codecoverage builds are actually "test" configuration
  35. $BuildSubtype = "codecoverage" # keep information about codecoverage in the subtype
  36. }
  37. if (-not ($BuildSubtype -in @("default","pogo","codecoverage"))) {
  38. Write-Error "Unsupported BuildSubtype: $BuildSubtype"
  39. }
  40. } elseif ((Test-Path Env:\BuildPlatform) -and (Test-Path Env:\BuildConfiguration)) {
  41. $BuildPlatform = $Env:BuildPlatform
  42. $BuildConfiguration = $Env:BuildConfiguration
  43. $BuildType = "${BuildPlatform}_${BuildConfiguration}"
  44. } else {
  45. Write-Error (@"
  46. Not enough information about BuildType:
  47. BuildType={0}
  48. BuildPlatform={1}
  49. BuildConfiguration={2}
  50. "@ -f $Env:BuildType, $Env:BuildPlatform, $Env:BuildConfiguration)
  51. exit 1
  52. }
  53. # set up required variables and import pre_post_util.ps1
  54. $arch = $BuildPlatform
  55. $flavor = $BuildConfiguration
  56. $OuterScriptRoot = $PSScriptRoot # Used in pre_post_util.ps1
  57. . "$PSScriptRoot\pre_post_util.ps1"
  58. $gitExe = GetGitPath
  59. $BuildName = ConstructBuildName -arch $BuildPlatform -flavor $BuildConfiguration -subtype $BuildSubtype
  60. $branch = $Env:BUILD_SOURCEBRANCH
  61. if (-not $branch) {
  62. $branch = iex "$gitExe rev-parse --symbolic-full-name HEAD"
  63. }
  64. $BranchName = $branch.split('/',3)[2]
  65. $BranchPath = $BranchName.replace('/','\')
  66. $CommitHash = $Env:BUILD_SOURCEVERSION
  67. if (-not $CommitHash) {
  68. $CommitHash = iex "$gitExe rev-parse HEAD"
  69. }
  70. $Username = (iex "$gitExe log $CommitHash -1 --pretty=%ae").split('@')[0]
  71. $CommitDateTime = [DateTime]$(iex "$gitExe log $CommitHash -1 --pretty=%aD")
  72. $CommitTime = Get-Date $CommitDateTime -Format yyMMdd.HHmm
  73. #
  74. # Get Build Info
  75. #
  76. $info = GetBuildInfo $oauth $CommitHash
  77. $BuildPushDate = [datetime]$info.push.date
  78. $PushDate = Get-Date $BuildPushDate -Format yyMMdd.HHmm
  79. $buildPushId, $buildPushIdPart1, $buildPushIdPart2, $buildPushIdString = GetBuildPushId $info
  80. $VersionString = "${Env:VERSION_MAJOR}.${Env:VERSION_MINOR}.${buildPushIdString}"
  81. $PreviewVersionString = "${VersionString}-preview"
  82. # unless it is a build branch, subdivide the output directory by month
  83. if ($BranchPath.StartsWith("build")) {
  84. $YearAndMonth = ""
  85. } else {
  86. $YearAndMonth = (Get-Date $BuildPushDate -Format yyMM) + "\"
  87. }
  88. $BuildIdentifier = "${buildPushIdString}_${PushDate}_${Username}_${CommitHash}"
  89. $ComputedDropPathSegment = "${BranchPath}\${YearAndMonth}${BuildIdentifier}"
  90. $BinariesDirectory = "${Env:BUILD_SOURCESDIRECTORY}\Build\VcBuild"
  91. $ObjectDirectory = "${BinariesDirectory}\obj\${BuildPlatform}_${BuildConfiguration}"
  92. # Create a sentinel file for each build flavor to track whether the build is complete.
  93. # * ${arch}_${flavor}.incomplete # will be deleted when the build of this flavor completes
  94. $buildIncompleteFileContentsString = @"
  95. {0} is incomplete.
  96. This could mean that the build is in progress, or that it was unable to run to completion.
  97. The contents of this directory should not be relied on until the build completes.
  98. "@
  99. $DropPath = Join-Path $Env:DROP_ROOT $ComputedDropPathSegment
  100. New-Item -ItemType Directory -Force -Path $DropPath
  101. New-Item -ItemType Directory -Force -Path (Join-Path $Env:BUILD_SOURCESDIRECTORY "test\logs")
  102. New-Item -ItemType Directory -Force -Path (Join-Path $BinariesDirectory "buildlogs")
  103. New-Item -ItemType Directory -Force -Path (Join-Path $BinariesDirectory "logs")
  104. $FlavorBuildIncompleteFile = Join-Path $DropPath "${BuildType}.incomplete"
  105. if (-not (Test-Path $FlavorBuildIncompleteFile)) {
  106. ($buildIncompleteFileContentsString -f "Build of ${BuildType}") `
  107. | Out-File $FlavorBuildIncompleteFile -Encoding Ascii
  108. }
  109. $PogoConfig = $supportedPogoBuildTypes -contains "${Env:BuildPlatform}_${Env:BuildConfiguration}"
  110. # Write the $envConfigScript
  111. @"
  112. set BranchName=${BranchName}
  113. set BranchPath=${BranchPath}
  114. set YearAndMonth=${YearAndMonth}
  115. set BuildIdentifier=${BuildIdentifier}
  116. set buildPushIdString=${buildPushIdString}
  117. set VersionString=${VersionString}
  118. set PreviewVersionString=${PreviewVersionString}
  119. set PushDate=${PushDate}
  120. set CommitTime=${CommitTime}
  121. set Username=${Username}
  122. set CommitHash=${CommitHash}
  123. set ComputedDropPathSegment=${ComputedDropPathSegment}
  124. set BinariesDirectory=${BinariesDirectory}
  125. set DropPath=${DropPath}
  126. set BuildType=${BuildType}
  127. set BuildPlatform=${BuildPlatform}
  128. set BuildConfiguration=${BuildConfiguration}
  129. set BuildSubtype=${BuildSubtype}
  130. set BuildName=${BuildName}
  131. set FlavorBuildIncompleteFile=${FlavorBuildIncompleteFile}
  132. set PogoConfig=${PogoConfig}
  133. "@ `
  134. | Out-File $envConfigScript -Encoding Ascii
  135. # Use the VSTS environment vars to construct a backwards-compatible VSO build environment
  136. # for the sake of reusing the pre-build and post-build scripts as they are.
  137. @"
  138. set TF_BUILD_SOURCEGETVERSION=LG:${branch}:${CommitHash}
  139. set TF_BUILD_DROPLOCATION=${BinariesDirectory}
  140. set TF_BUILD_SOURCESDIRECTORY=${Env:BUILD_SOURCESDIRECTORY}
  141. set TF_BUILD_BUILDDIRECTORY=${ObjectDirectory}
  142. set TF_BUILD_BINARIESDIRECTORY=${BinariesDirectory}
  143. set TF_BUILD_BUILDDEFINITIONNAME=${Env:BUILD_DEFINITIONNAME}
  144. set TF_BUILD_BUILDNUMBER=${Env:BUILD_BUILDNUMBER}
  145. set TF_BUILD_BUILDURI=${Env:BUILD_BUILDURI}
  146. "@ `
  147. | Out-File $envConfigScript -Encoding Ascii -Append
  148. # Export VSO variables that can be consumed by other VSO tasks where the task
  149. # definition in VSO itself needs to know the value of the variable.
  150. # If the task definition itself doesn't need to know the value of the variables,
  151. # the variables that are added to the environment via the script generated above
  152. # will be interpolated when the tasks run the associated command line with the
  153. # given parameters.
  154. #
  155. # For example, for a Publish Artifacts task, VSO itself needs to know
  156. # the value of DropPath in order to construct links to the artifacts correctly.
  157. # Thus, we export a variable called VSO_DropPath (VSO_ prefix by convention)
  158. # that the VSO build definition, not just the command environment, will know about.
  159. #
  160. # Uses command syntax documented here:
  161. # https://github.com/Microsoft/vso-agent-tasks/blob/master/docs/authoring/commands.md
  162. # Lines written to stdout that match this pattern are interpreted with this command syntax.
  163. Write-Output "Setting VSO variable VSO_DropPath = ${DropPath}"
  164. Write-Output "##vso[task.setvariable variable=VSO_DropPath;]${DropPath}"
  165. Write-Output "Setting VSO variable VSO_VersionString = ${VersionString}"
  166. Write-Output "##vso[task.setvariable variable=VSO_VersionString;]${VersionString}"
  167. #
  168. # Clean up files that might have been left behind from a previous build.
  169. #
  170. if ((Test-Path Env:\BUILD_BINARIESDIRECTORY) -and (Test-Path "$Env:BUILD_BINARIESDIRECTORY"))
  171. {
  172. Remove-Item -Verbose "${Env:BUILD_BINARIESDIRECTORY}\*" -Recurse
  173. }