init_build.ps1 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. param (
  6. [string]$envconfig = "ComputedEnvironment.cmd",
  7. [Parameter(Mandatory=$True)]
  8. [string]$oauth
  9. )
  10. $branch = $env:BUILD_SOURCEBRANCH
  11. if (-not $branch) {
  12. $branch = $(git rev-parse --symbolic-full-name HEAD)
  13. }
  14. $BranchName = $branch.split('/',3)[2]
  15. $BranchPath = $BranchName.replace('/','\')
  16. $CommitHash = ${env:BUILD_SOURCEVERSION}
  17. if (-not $CommitHash) {
  18. $CommitHash = $(git rev-parse HEAD)
  19. }
  20. $Username = $(git log $CommitHash -1 --pretty=%ae).split('@')[0]
  21. $CommitDateTime = [DateTime]$(git log $CommitHash -1 --pretty=%aD)
  22. $CommitTime = Get-Date $CommitDateTime -Format yyMMdd.HHmm
  23. #
  24. # (borrowed from pre_build.ps1)
  25. # Get PushID and PushDate from VSO
  26. # TODO (doilij) refactor this into a method in a util script.
  27. #
  28. # Get the git remote path and construct the rest API URI
  29. $remote = (iex "git remote -v")[0].split()[1].replace("_git", "_apis/git/repositories");
  30. $remote = $remote.replace("mshttps", "https")
  31. # Get the pushId and push date time to use that for build number and build date time
  32. $uri = ("{0}/commits/{1}?api-version=1.0" -f $remote, $commitHash)
  33. $oauthToken = Get-Content $oauth
  34. $header = @{Authorization=("Basic {0}" -f $oauthToken) }
  35. $info = Invoke-RestMethod -Headers $header -Uri $uri -Method GET
  36. $BuildPushDate = [datetime]$info.push.date
  37. $PushDate = Get-Date $BuildPushDate -Format yyMMdd.HHmm
  38. $buildPushId = $info.push.pushId
  39. $buildPushIdPart1 = [int]([math]::Floor($buildPushId / 65536))
  40. $buildPushIdPart2 = [int]($buildPushId % 65536)
  41. $PushID = "{0}.{1}" -f $buildPushIdPart1.ToString("00000"), $buildPushIdPart2.ToString("00000")
  42. $FullVersionString = "${Env:VERSION_MAJOR}.${Env:VERSION_MINOR}.${PushID}"
  43. #
  44. # (end code borrowed from pre_build.ps1)
  45. #
  46. # unless it is a build branch, subdivide the output directory by month
  47. if ($BranchPath.StartsWith("build")) {
  48. $YearAndMonth = ""
  49. } else {
  50. $YearAndMonth = Get-Date $BuildPushDate -Format yyMM
  51. }
  52. $YearAndMonthSeparator = ""
  53. if ($YearAndMonth) {
  54. $YearAndMonthSeparator = "\"
  55. }
  56. $BuildIdentifier = "${PushID}_${PushDate}_${Username}_${CommitHash}"
  57. $OutputPath = "${BranchPath}\${YearAndMonth}${YearAndMonthSeparator}${BuildIdentifier}"
  58. $FlavorName = "${Env:BuildPlatform}_${Env:BuildConfiguration}"
  59. # Create a sentinel file for each build flavor and for the overall build
  60. # to track whether the build is complete.
  61. # * build.incomplete # will be deleted when the release task completes
  62. # * ${arch}_${flavor}.incomplete # will be deleted when the build of this flavor completes
  63. $buildIncompleteFileContentsString = @"
  64. {0} in progress.
  65. The contents of this directory should not be relied on until the build completes.
  66. "@
  67. $FullOutputPath = Join-Path ${env:DROP_ROOT} ${OutputPath}
  68. New-Item -ItemType Directory -Force -Path $FullOutputPath
  69. New-Item -ItemType Directory -Force -Path (Join-Path $Env:BUILD_BINARIESDIRECTORY "buildlogs")
  70. New-Item -ItemType Directory -Force -Path (Join-Path $Env:BUILD_BINARIESDIRECTORY "logs")
  71. $buildIncompleteFile = Join-Path $FullOutputPath "build.incomplete"
  72. $flavorBuildIncompleteFile = Join-Path $FullOutputPath "${FlavorName}.incomplete"
  73. if (-not (Test-Path $buildIncompleteFile)) {
  74. ($buildIncompleteFileContentsString -f "Build") `
  75. | Out-File $buildIncompleteFile -Encoding Ascii
  76. }
  77. if (-not (Test-Path $flavorBuildIncompleteFile)) {
  78. ($buildIncompleteFileContentsString -f "Build of ${FlavorName}") `
  79. | Out-File $flavorBuildIncompleteFile -Encoding Ascii
  80. }
  81. # Write the $envconfig script.
  82. @"
  83. set BranchName=${BranchName}
  84. set BranchPath=${BranchPath}
  85. set YearAndMonth=${YearAndMonth}
  86. set BuildIdentifier=${BuildIdentifier}
  87. set PushID=${PushID}
  88. set FullVersionString=${FullVersionString}
  89. set PushDate=${PushDate}
  90. set CommitTime=${CommitTime}
  91. set Username=${Username}
  92. set CommitHash=${CommitHash}
  93. set OutputPath=${OutputPath}
  94. set FlavorName=${FlavorName}
  95. set BuildIncompleteFile=${buildIncompleteFile}
  96. set FlavorBuildIncompleteFile=${flavorBuildIncompleteFile}
  97. "@ `
  98. | Out-File $envconfig -Encoding Ascii
  99. # Use the MBv2 environment to construct a MBv1 VSO environment
  100. # for the sake of reusing the pre-build and post-build scripts as they are.
  101. @"
  102. set TF_BUILD_SOURCEGETVERSION=LG:${branch}:${CommitHash}
  103. set TF_BUILD_DROPLOCATION=${Env:BUILD_BINARIESDIRECTORY}
  104. set TF_BUILD_BUILDDEFINITIONNAME=${Env:BUILD_DEFINITIONNAME}
  105. set TF_BUILD_BUILDNUMBER=${Env:BUILD_BUILDNUMBER}
  106. set TF_BUILD_BUILDURI=${Env:BUILD_BUILDURI}
  107. "@ `
  108. | Out-File $envconfig -Encoding Ascii -Append
  109. # Set VSO variables that can be consumed by other VSO tasks.
  110. # Uses command syntax documented here:
  111. # https://github.com/Microsoft/vso-agent-tasks/blob/master/docs/authoring/commands.md
  112. # Lines written to stdout that match this pattern are interpreted with this command syntax.
  113. Write-Output "Setting VSO variable VSO_OutputPath = ${OutputPath}"
  114. Write-Output "##vso[task.setvariable variable=VSO_OutputPath;]${OutputPath}"
  115. Write-Output "Setting VSO variable VSO_FullVersionString = ${FullVersionString}"
  116. Write-Output "##vso[task.setvariable variable=VSO_FullVersionString;]${FullVersionString}"
  117. # TODO (doilij): move this up and assign values
  118. # Inferable Environment (if not specified, inferred by pre_post_util.ps1):
  119. # $Env:TF_BUILD_SOURCESDIRECTORY (a.k.a. $srcpath)
  120. # $Env:TF_BUILD_BUILDDIRECTORY (a.k.a. $objpath)
  121. # $Env:TF_BUILD_BINARIESDIRECTORY (a.k.a. $binpath)