Skip to content

Commit

Permalink
[CI] Start the process of cleaning up the config step. (#21134)
Browse files Browse the repository at this point in the history
We want to be able to export and load the variables in the cacading
pipelines. The way we have the scripts makes this process very
complicated to do. We need to clean up the script. Then export the steps
so that we can access them in the following pipelines.

This change is simply refactoring the way we load/export the default
VSTS variables in a pipeline.

---------

Co-authored-by: Rolf Bjarne Kvinge <rolf@xamarin.com>
  • Loading branch information
mandel-macaque and rolfbjarne authored Aug 28, 2024
1 parent f7da565 commit 1d0030a
Showing 1 changed file with 59 additions and 46 deletions.
105 changes: 59 additions & 46 deletions tools/devops/automation/scripts/VSTS.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,61 @@ class Vsts {

class BuildConfiguration {

# list of the default variables we are interested in
static [string[]] $defaultBuildVariables = $(
"BUILD_BUILDID",
"BUILD_BUILDNUMBER",
"BUILD_BUILDURI",
"BUILD_BINARIESDIRECTORY",
"BUILD_DEFINITIONNAME",
"BUILD_REASON",
"BUILD_REPOSITORY_ID",
"BUILD_REPOSITORY_NAME",
"BUILD_REPOSITORY_PROVIDER",
"BUILD_REPOSITORY_URI",
"BUILD_SOURCEBRANCH",
"BUILD_SOURCEBRANCHNAME"
)

<#
.SYNOPSIS
Stores the default variables in the current buld as PARENT_BUILD_* in the
configuration object. This allows cascading pipelines to access the configuration
of the pipeline that triggered them.
#>
[void] StoreParentBuildVariables ([PSCustomObject] $configuration) {
Write-Debug ("=> StoreParentBuildVariables")
foreach ($buildVariable in [BuildConfiguration]::defaultBuildVariables) {
$variableName = "PARENT_BUILD_$buildVariable"
$variableValue = [Environment]::GetEnvironmentVariable($buildVariable)
if ($variableValue) {
Write-Debug "$variableName = $variableValue"
$configuration | Add-Member -NotePropertyName $variableName -NotePropertyValue $variableValue
} else {
Write-Debug "$variableName not found."
}
}
}

<#
.SYNOPSIS
Exports the default variables in the current buld as PARENT_BUILD_* from the
configuration object.
#>
[void] ExportParentBuildVariables ([PSCustomObject] $configuration) {
Write-Debug ("=> ExportParentBuildVariables")
foreach ($buildVariable in [BuildConfiguration]::defaultBuildVariables) {
$variableName = "PARENT_BUILD_$buildVariable"
$variableValue = $configuration.$variableName
if ($variableValue) {
Write-Debug "$variableName = $variableValue"
Write-Host "##vso[task.setvariable variable=$variableName;isOutput=true]$variableValue"
} else {
Write-Debug "$variableName not found."
}
}
}

[PSCustomObject] Import([string] $configFile) {
if (-not (Test-Path -Path $configFile -PathType Leaf)) {
throw [System.InvalidOperationException]::new("Configuration file $configFile is missing")
Expand All @@ -253,31 +308,8 @@ class BuildConfiguration {
throw [System.InvalidOperationException]::new("Failed to load configuration file $configFile")
}

$defaultBuildVariables = @(
"BUILD_BUILDID",
"BUILD_BUILDNUMBER",
"BUILD_BUILDURI",
"BUILD_BINARIESDIRECTORY",
"BUILD_DEFINITIONNAME",
"BUILD_REASON",
"BUILD_REPOSITORY_ID",
"BUILD_REPOSITORY_NAME",
"BUILD_REPOSITORY_PROVIDER",
"BUILD_REPOSITORY_URI",
"BUILD_SOURCEBRANCH",
"BUILD_SOURCEBRANCHNAME"
)

# load the variable name from the parent
foreach ($buildVariable in $defaultBuildVariables) {
$variableName = "PARENT_BUILD_$buildVariable"
$variableValue = $config.$variableName
if ($variableValue) {
Write-Host "##vso[task.setvariable variable=$variableName;isOutput=true]$variableValue"
} else {
Write-Debug "Ignoring variable $variableName"
}
}
# load the variables from the config and export them to be accessable from others
$this.ExportParentBuildVariables($config)

$dotnetPlatforms = $config.DOTNET_PLATFORMS.Split(' ', [StringSplitOptions]::RemoveEmptyEntries)
Write-Host "##vso[task.setvariable variable=DOTNET_PLATFORMS;isOutput=true]$dotnetPlatforms"
Expand Down Expand Up @@ -339,6 +371,7 @@ class BuildConfiguration {
return $config
}


[PSCustomObject] Create([bool] $addTags, [string] $configFile) {
# we are going to use a custom object to store all the configuration of the build, this later
# will be uploaded as an artifact so that it can be easily shared with the cascade pipelines, we will
Expand All @@ -349,27 +382,7 @@ class BuildConfiguration {
DOTNET_PLATFORMS = "$Env:CONFIGURE_PLATFORMS_DOTNET_PLATFORMS"
}

$defaultBuildVariables = @(
"BUILD_BUILDID",
"BUILD_BUILDNUMBER",
"BUILD_BUILDURI",
"BUILD_BINARIESDIRECTORY",
"BUILD_DEFINITIONNAME",
"BUILD_REASON",
"BUILD_REPOSITORY_ID",
"BUILD_REPOSITORY_NAME",
"BUILD_REPOSITORY_PROVIDER",
"BUILD_REPOSITORY_URI",
"BUILD_SOURCEBRANCH",
"BUILD_SOURCEBRANCHNAME"
)

# loop over the default build enviroments and add them with a prefix to the configuration objects
foreach ($buildVariable in $defaultBuildVariables) {
$variableName = "PARENT_BUILD_$buildVariable"
$variableValue = [Environment]::GetEnvironmentVariable($buildVariable)
$configuration | Add-Member -NotePropertyName $variableName -NotePropertyValue $variableValue
}
$this.StoreParentBuildVariables($configuration)

# For each .NET platform we support, add a INCLUDE_DOTNET_<platform> variable specifying whether that platform is enabled or not.
$dotnetPlatforms = $configuration.DOTNET_PLATFORMS.Split(' ', [StringSplitOptions]::RemoveEmptyEntries)
Expand Down

2 comments on commit 1d0030a

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

Please sign in to comment.