Docker/Agent.ps1
Clear-Host $message = @" ___ ___ _ / _ \ / _ \ | | / /_\ \_____ _ _ __ ___ / /_\ \ __ _ ___ _ __ | |_ | _ |_ / | | | '__/ _ \ | _ |/ _` |/ _ \ '_ \| __| | | | |/ /| |_| | | | __/ | | | | (_| | __/ | | | |_ \_| |_/___|\__,_|_| \___| \_| |_/\__, |\___|_| |_|\__| __/ | |___/ "@ Write-Host $message -ForegroundColor Magenta Write-Host "" -ForegroundColor White Write-Host Agent - $Env:AZP_AGENT_NAME Write-Host PAT - $Env:AZP_TOKEN $ProgressPreference = 'SilentlyContinue' [string]$global:selectedSubscription = "" [string]$global:adoOrg = $Env:AZP_ORG [string]$global:selectedProject = $Env:AZP_PROJECT $global:dockerAgent = "" $global:adoURL = "" if (!(Test-Path "$env:APPDATA\Microsoft.PowerPlatform.Docker")) { $global:dockerAgent = New-Item -Path "$env:APPDATA\Microsoft.PowerPlatform.Docker" -ItemType Directory } if (!(Test-Path "\azp")) { New-Item -Path "\azp" -ItemType Directory } if (!(Test-Path "$env:APPDATA\Microsoft.PowerPlatform.Docker\$Env:AZP_AGENT_NAME.json")) { $global:dockerAgent = New-Item "$env:APPDATA\Microsoft.PowerPlatform.Docker\$Env:AZP_AGENT_NAME.json" -ItemType File $global:dockerAgent = Get-Content "$env:APPDATA\Microsoft.PowerPlatform.Docker\$Env:AZP_AGENT_NAME.json" | ConvertFrom-Json } else { $global:dockerAgent = Get-Content "$env:APPDATA\Microsoft.PowerPlatform.Docker\$Env:AZP_AGENT_NAME.json" | ConvertFrom-Json $global:selectedSubscription = $global:dockerAgent.AzureSubscription $global:adoOrg = $global:dockerAgent.adoOrg $global:selectedProject = $global:dockerAgent.selectedProject $global:adoURL = "https://dev.azure.com/$global:adoOrg" } if (!($global:dockerAgent.PreReqs)) { . \Installers\Install-PreRequisites.ps1 Clear-Host } Write-Host Azure DevOps Organisation - $global:adoOrg $global:adoURL = "https://dev.azure.com/$global:adoOrg" Write-Host Azure DevOps Project - $global:selectedProject $dockerProject = @([ordered]@{AgentName = $Env:AZP_AGENT_NAME; AzureSubscription = $global:selectedSubscription; AzureToken = ""; PreReqs = $true; adoOrg = $global:adoOrg; selectedProject = $global:selectedProject }) $dockerProject | ConvertTo-Json | Set-Content ("$env:APPDATA\Microsoft.PowerPlatform.Docker\$Env:AZP_AGENT_NAME.json") $global:dockerAgent = Get-Content "$env:APPDATA\Microsoft.PowerPlatform.Docker\$Env:AZP_AGENT_NAME.json" | ConvertFrom-Json $Env:AZP_TOKEN_FILE = "\azp\.token" $Env:AZP_TOKEN | Out-File -FilePath $Env:AZP_TOKEN_FILE if ((Test-Path Env:AZP_WORK) -and -not (Test-Path $Env:AZP_WORK)) { New-Item $Env:AZP_WORK -ItemType directory | Out-Null } New-Item "\azp\agent" -ItemType directory -ErrorAction SilentlyContinue | Out-Null # Let the agent ignore the token env variables $Env:VSO_AGENT_IGNORE = "AZP_TOKEN,AZP_TOKEN_FILE" Set-Location azp\agent Write-Host "1. Determining matching Azure Pipelines agent..." -ForegroundColor Cyan $ADOUrl = $global:adoURL Write-Host $ADOUrl $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$(Get-Content ${Env:AZP_TOKEN_FILE})")) $package = Invoke-RestMethod -Headers @{Authorization=("Basic $base64AuthInfo")} "$($ADOUrl)/_apis/distributedtask/packages/agent?platform=win-x64&`$top=1" $packageUrl = $package[0].Value.downloadUrl Write-Host $packageUrl $fragments = $packageUrl.Split('/') $filename = $fragments[$fragments.Length - 1] Write-Host FileName - $filename Write-Host "$(Get-Location)\$filename" if (!(Test-Path -Path "$(Get-Location)\$filename")) { Write-Host "2. Downloading and installing Azure Pipelines agent..." -ForegroundColor Cyan $wc = New-Object System.Net.WebClient $wc.DownloadFile($packageUrl, "$(Get-Location)\$filename") } else { Write-Host "2a. Agent already up to date" } Expand-Archive -Path $filename -DestinationPath "\azp\agent" -Force -ErrorAction SilentlyContinue try { Write-Host "3. Configuring Azure Pipelines agent..." -ForegroundColor Cyan .\config.cmd --unattended ` --agent "$(if (Test-Path Env:AZP_AGENT_NAME) { ${Env:AZP_AGENT_NAME} } else { ${Env:computername} })" ` --url "$global:adoURL" ` --auth PAT ` --token "$(Get-Content ${Env:AZP_TOKEN_FILE})" ` --pool "$(if (Test-Path Env:AZP_POOL) { ${Env:AZP_POOL} } else { 'Default' })" ` --work "$(if (Test-Path Env:AZP_WORK) { ${Env:AZP_WORK} } else { '_work' })" ` --replace Write-Host "4. Running Azure Pipelines agent..." -ForegroundColor Cyan .\run.cmd pause } finally { Write-Host "Cleanup. Removing Azure Pipelines agent..." -ForegroundColor Cyan .\config.cmd remove --unattended ` --auth PAT ` --token "$(Get-Content ${Env:AZP_TOKEN_FILE})" pause } |