|
| 1 | +$ErrorActionPreference = "Continue" |
| 2 | +$VerbosePreference = "Continue" |
| 3 | + |
| 4 | +$parentDir = "C:\Jenkins" |
| 5 | +$condaInstallationDir = "$parentDir\Miniconda3" |
| 6 | + |
| 7 | +# Install Chocolatey |
| 8 | +[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12 |
| 9 | +$env:chocolateyUseWindowsCompression = 'true' |
| 10 | +Invoke-WebRequest https://community.chocolatey.org/install.ps1 -UseBasicParsing | Invoke-Expression |
| 11 | + |
| 12 | +# Add Chocolatey to powershell profile |
| 13 | +$ChocoProfileValue = @' |
| 14 | +$ChocolateyProfile = "$env:ChocolateyInstall\helpers\chocolateyProfile.psm1" |
| 15 | +if (Test-Path($ChocolateyProfile)) { |
| 16 | + Import-Module "$ChocolateyProfile" |
| 17 | +} |
| 18 | +Remove-Item Alias:curl |
| 19 | +Remove-Item Alias:wget |
| 20 | +refreshenv |
| 21 | +'@ |
| 22 | + |
| 23 | +# https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_profiles |
| 24 | +$PS_PROFILE = "$PsHome\Microsoft.PowerShell_profile.ps1" |
| 25 | + |
| 26 | +# Write it to the $profile location |
| 27 | +Set-Content -Path $PS_PROFILE -Value $ChocoProfileValue -Force |
| 28 | + |
| 29 | +$PYTHON_PATH = '$Env:PATH += ' + "';$condaInstallationDir'" |
| 30 | +# Add conda path to the powershell profile to make its commands, i.e. python, available when logging |
| 31 | +# in to Windows runners or when the CI uses powershell |
| 32 | +Add-Content "$PS_PROFILE" "$PYTHON_PATH" |
| 33 | + |
| 34 | +# Source it |
| 35 | +. $PS_PROFILE |
| 36 | + |
| 37 | +$condaHook = "$condaInstallationDir\shell\condabin\conda-hook.ps1" |
| 38 | +if (-Not (Test-Path -Path $condaHook -PathType Leaf)) { |
| 39 | + Write-Error "Miniconda installation failed, no hook found at $condaHook" |
| 40 | + exit 1 |
| 41 | +} |
| 42 | + |
| 43 | +# Load conda into powershell |
| 44 | +& $condaHook |
| 45 | + |
| 46 | +# According to https://docs.conda.io/en/latest/miniconda.html, Miniconda have only one built-in |
| 47 | +# python executable, and it can be Python3 or 2 depending on which installation package is used |
| 48 | +try { |
| 49 | + $PYTHON = (Get-Command python).Source |
| 50 | +} catch { |
| 51 | + $PYTHON = "" |
| 52 | +} |
| 53 | + |
| 54 | +If ("$PYTHON" -eq "") { |
| 55 | + Write-Output "Found no Python in $Env:PATH. Double check that Miniconda3 is setup correctly in the AMI" |
| 56 | +} |
| 57 | +Else { |
| 58 | + Write-Output "Found Python command at $PYTHON" |
| 59 | +} |
| 60 | + |
| 61 | +try { |
| 62 | + $PYTHON3 = (Get-Command python3).Source |
| 63 | +} catch { |
| 64 | + $PYTHON3 = "" |
| 65 | +} |
| 66 | + |
| 67 | +If ("$PYTHON3" -eq "") { |
| 68 | + Write-Output "Found no Python 3 in $Env:PATH. This is expected for Miniconda3, and the command will be an alias to Python" |
| 69 | +} |
| 70 | +Else { |
| 71 | + Write-Output "Found Python 3 command at $PYTHON3" |
| 72 | +} |
| 73 | + |
| 74 | +If (("$PYTHON3" -eq "") -and ("$PYTHON" -ne "")) { |
| 75 | + # Setup an alias from Python3 to Python when only the latter exists in Miniconda3 |
| 76 | + Add-Content "$PS_PROFILE" "Set-Alias -Name python3 -Value $PYTHON" |
| 77 | +} |
0 commit comments