Created
April 19, 2023 07:08
-
-
Save jeroensmink98/0abcaa9c9424f1efd8cb2cc6972bb47f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############################################# | |
# Variables # | |
############################################# | |
# Set the folder path where the YAML files are stored | |
$folderPath = "C:\tres-internet-nuget-umbraco8\.pipelines" | |
# Set the name of the Azure DevOps pipeline folder | |
$azureDevOpsPipelineFolder = "My Pipeline Folder" | |
# Set the Azure DevOps repository ID | |
$azureDevOpsRepoId = "" | |
# Set the Azure DevOps repository name | |
$azureDevOpsRepoName = "" | |
# Set the Azure DevOps repository type | |
$azureDevOpsRepoType = "azureReposGit" | |
# Get the name of the Git repository | |
$repoName = git rev-parse --show-toplevel | Split-Path -Leaf | |
############################################# | |
# Get the YAML files # | |
############################################# | |
# Get all files in the folder with the .yml extension | |
$files = Get-ChildItem -Path $folderPath -Filter *.yml | |
# Create an empty array to store the results | |
$results = @() | |
# Loop through each file | |
foreach ($file in $files) { | |
# Read the contents of the file and convert it to a PowerShell object | |
$contents = Get-Content $file.FullName -Raw | ConvertFrom-Yaml | |
# Get the NuggetType value based on the key: value in the YAML file | |
$nugetType = $contents.extends.parameters.nugetType | |
# Check if the NuggetType value is either "Framework" or "Core" | |
if ($nugetType -eq "Framework" -or $nugetType -eq "Core") { | |
# Get the relative path of the file | |
$relativePath = Split-Path -Path $file.FullName -Parent | |
$relativePath = ".pipelines" # Set the relative path to the pipelines folder | |
$pipelineName = ($file.BaseName + "-" + $nugetType.ToLower() + "-nuget").Replace("-nuget", "") | |
$pipelineName += "-nuget" | |
# Create a new object with the path, NuggetType, and Pipeline name values | |
$result = [PSCustomObject]@{ | |
Path = "$relativePath\$($file.Name)".Replace("\", "/") # Replace \ with / | |
NuggetType = $nugetType # Set the NuggetType value based on the YAML file contents# Set the NuggetType value based on the YAML file contents | |
PipelineName = $pipelineName # Set the Pipeline name value based on the file name and NuggetType | |
} | |
# Add the object to the results array | |
$results += $result | |
} | |
} | |
# Set the output file path | |
$outputFilePath = ".\output.json" | |
# Convert the results array to JSON and output it to a file | |
$results | ConvertTo-Json | Out-File -Encoding utf8 -FilePath $outputFilePath | |
# Read the JSON file | |
$jsonFilePath = ".\output.json" | |
# Check if the JSON file exists, create it if it doesn't | |
if (-not (Test-Path $jsonFilePath)) { | |
New-Item -ItemType File -Path $jsonFilePath -Force | |
} | |
# Read the JSON file | |
$jsonContents = Get-Content $jsonFilePath | ConvertFrom-Json | |
# Loop through each object in the JSON array | |
foreach ($item in $jsonContents) { | |
# Get the path of the YAML file relative to the pipelines folder | |
# Create a new object with the correct values for the Azure DevOps API request | |
$newObject = [PSCustomObject]@{ | |
folder = $azureDevOpsPipelineFolder | |
name = $item.PipelineName | |
configuration = @{ | |
type = "yaml" | |
path = $item.Path | |
repository = @{ | |
id = $azureDevOpsRepoId | |
name = $azureDevOpsRepoName | |
type = $azureDevOpsRepoType | |
} | |
} | |
} | |
# Convert the new object to JSON and output it | |
$json = $newObject | ConvertTo-Json -Depth 4 | |
######################################### | |
# Azure DevOps API # | |
######################################### | |
$organization = "" | |
$project = "T" | |
$PAT = "" | |
# Endpoint URL | |
$baseUrl = "https://dev.azure.com" | |
$endpointUrl = "$baseUrl/$organization/$project/_apis/pipelines?api-version=7.0" | |
# Headers | |
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes((":$PAT"))) | |
$headers = @{ | |
Authorization = "Basic $base64AuthInfo" | |
} | |
# Invoke REST method | |
$response = Invoke-RestMethod -Method Post -Uri $endpointUrl -Headers $headers -ContentType "application/json" -Body $json | |
# Display response | |
$response | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment