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