Skip to content

Commit b4636dc

Browse files
achauhan-sccscbedd
andauthored
Use a venv for build/test (#36340) (#36430)
* create `use-venv` yml which can utilize the current python to create a venv and prepend the PATH with it * use a freshly created venv `build` and `test` Co-authored-by: Scott Beddall <[email protected]>
1 parent b41877c commit b4636dc

File tree

7 files changed

+108
-30
lines changed

7 files changed

+108
-30
lines changed

eng/pipelines/templates/jobs/regression.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ jobs:
7979
displayName: 'Use Python 3.9'
8080
inputs:
8181
versionSpec: '3.9'
82+
83+
- template: /eng/pipelines/templates/steps/use-venv.yml
8284

8385
- task: DownloadPipelineArtifact@2
8486
inputs:

eng/pipelines/templates/steps/build-extended-artifacts.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ steps:
4747
inputs:
4848
versionSpec: '3.11'
4949

50+
- template: /eng/pipelines/templates/steps/use-venv.yml
51+
5052
- script: |
5153
python -m pip install setuptools==58.3.0
5254
python -m pip install -r eng/ci_tools.txt

eng/pipelines/templates/steps/build-package-artifacts.yml

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -49,27 +49,13 @@ steps:
4949
inputs:
5050
versionSpec: $(PythonVersion)
5151

52-
- bash: |
53-
python -m venv $(Build.SourcesDirectory)/venv
54-
source $(Build.SourcesDirectory)/venv/bin/activate
55-
which python
56-
python -m pip install --force -r eng/ci_tools.txt
57-
python -m pip freeze --all
58-
echo "##vso[task.setvariable variable=PATH]$PATH"
59-
displayName: 'Prep Environment Linux/Mac'
60-
condition: and(succeeded(), or(eq(variables['Agent.OS'], 'Linux'), eq(variables['Agent.OS'], 'Darwin')))
52+
- template: /eng/pipelines/templates/steps/use-venv.yml
6153

6254
- pwsh: |
63-
python -m venv $(Build.SourcesDirectory)/venv
64-
./venv/Scripts/Activate.ps1
6555
which python
6656
python -m pip install --force -r eng\ci_tools.txt
6757
python -m pip freeze --all
68-
69-
$path = $env:PATH
70-
Write-Host "##vso[task.setvariable variable=PATH]$path"
71-
displayName: 'Prep Environment Windows'
72-
condition: and(succeeded(), eq(variables['Agent.OS'], 'Windows_NT'))
58+
displayName: 'Prep Environment'
7359
7460
- template: set-dev-build.yml@self
7561
parameters:
@@ -94,28 +80,16 @@ steps:
9480
displayName: 'Install QEMU Dependencies'
9581
condition: and(succeeded(), eq(variables['Agent.OS'], 'Linux'))
9682
97-
- bash: |
98-
source $(Build.SourcesDirectory)/venv/bin/activate
99-
which python
100-
sdk_build -d "$(Build.ArtifactStagingDirectory)" "$(TargetingString)" --service=${{parameters.ServiceDirectory}} --inactive
101-
displayName: 'Generate Packages Linux/Mac'
102-
condition: and(succeeded(), or(eq(variables['Agent.OS'], 'Linux'), eq(variables['Agent.OS'], 'Darwin')))
103-
timeoutInMinutes: 80
104-
env:
105-
CIBW_BUILD_VERBOSITY: 3
106-
10783
- pwsh: |
108-
./venv/Scripts/Activate.ps1
10984
which python
11085
sdk_build -d "$(Build.ArtifactStagingDirectory)" "$(TargetingString)" --service=${{parameters.ServiceDirectory}} --inactive
111-
displayName: 'Generate Packages Windows'
112-
condition: and(succeededOrFailed(), eq(variables['Agent.OS'], 'Windows_NT'))
86+
displayName: 'Generate Packages'
11387
timeoutInMinutes: 80
11488
env:
11589
CIBW_BUILD_VERBOSITY: 3
11690
11791
- script: |
118-
pip install twine==4.0.2 importlib-metadata==7.2.1
92+
python -m pip install -r eng/release_requirements.txt
11993
twine check $(Build.ArtifactStagingDirectory)/**/*.whl
12094
twine check $(Build.ArtifactStagingDirectory)/**/*.tar.gz
12195
displayName: 'Verify Readme'

eng/pipelines/templates/steps/build-test.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ steps:
3333
parameters:
3434
versionSpec: '${{ parameters.PythonVersion }}'
3535

36+
- template: /eng/pipelines/templates/steps/use-venv.yml
37+
3638
- template: set-dev-build.yml
3739
parameters:
3840
ServiceDirectory: ${{ parameters.ServiceDirectory }}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
parameters:
2+
- name: VirtualEnvironmentName
3+
type: string
4+
default: "venv"
5+
6+
steps:
7+
- pwsh: |
8+
$(Build.SourcesDirectory)/eng/scripts/create-venv.ps1 `
9+
-VenvName "${{ parameters.VirtualEnvironmentName }}" `
10+
-RepoRoot "$(Build.SourcesDirectory)"
11+
displayName: Create virtual environment
12+
13+
- pwsh: |
14+
$(Build.SourcesDirectory)/eng/scripts/activate-venv.ps1 `
15+
-VenvName "${{ parameters.VirtualEnvironmentName }}" `
16+
-RepoRoot "$(Build.SourcesDirectory)"
17+
displayName: Use ${{ parameters.VirtualEnvironmentName }} Virtual Environment

eng/scripts/activate-venv.ps1

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<#!
2+
.SYNOPSIS
3+
Activates a virtual environment for a CI machine. Any further usages of "python" will utilize this virtual environment.
4+
5+
.DESCRIPTION
6+
When activating a virtual environment, only a few things are actually functionally changed on the machine.
7+
8+
# 1. PATH = path to the bin directory of the virtual env. "Scripts" on windows machines
9+
# 2. VIRTUAL_ENV = path to root of the virtual env
10+
# 3. VIRTUAL_ENV_PROMPT = the prompt that is displayed next to the CLI cursor when the virtual env is active
11+
# within a CI machine, we only need the PATH and VIRTUAL_ENV variables to be set.
12+
# 4. (optional and inconsistently) _OLD_VIRTUAL_PATH = the PATH before the virtual env was activated. This is not set in this script.
13+
14+
.PARAMETER VenvName
15+
The name of the virtual environment to activate.
16+
17+
.PARAMETER RepoRoot
18+
The root of the repository.
19+
#>
20+
param (
21+
[Parameter(Mandatory=$true)]
22+
[string]$VenvName,
23+
[Parameter(Mandatory=$true)]
24+
[string]$RepoRoot # mandatory here, but $(Build.SourcesDirectory) will be passed in the template yaml
25+
)
26+
27+
Set-StrictMode -Version 4
28+
$ErrorActionPreference = "Stop"
29+
30+
$venvPath = Join-Path $RepoRoot $VenvName
31+
$venvBinPath = Join-Path $venvPath "bin"
32+
$env:VIRTUAL_ENV = $venvPath
33+
34+
if (-not (Test-Path $venvPath)) {
35+
Write-Error "Virtual environment '$venvPath' does not exist at $venvPath"
36+
exit 1
37+
}
38+
39+
if ($IsWindows) {
40+
$venvBinPath = Join-Path $venvPath "Scripts"
41+
$env:PATH = "$venvBinPath;$($env:PATH)"
42+
}
43+
else {
44+
$env:PATH = "$venvBinPath`:$($env:PATH)"
45+
}
46+
47+
Write-Host "Activating virtual environment '$VenvName' at $venvPath via AzDO to the value '$($env:PATH)'"
48+
Write-Host "##vso[task.setvariable variable=VIRTUAL_ENV]$($env:VIRTUAL_ENV)"
49+
Write-Host "##vso[task.prependpath]$($env:PATH)"

eng/scripts/create-venv.ps1

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<#!
2+
.SYNOPSIS
3+
Creates a virtual environment for a CI machine.
4+
5+
.DESCRIPTION
6+
If the virtual environment directory already exists, it will skip the creation. The location of the virtual environment will be stored in a variable
7+
named <VenvName>_LOCATION. The location will be RepoRoot + VenvName.
8+
9+
.PARAMETER VenvName
10+
The name of the virtual environment which will be created.
11+
12+
.PARAMETER RepoRoot
13+
The root of the repository.
14+
#>
15+
param(
16+
[Parameter(Mandatory = $true)]
17+
[string] $VenvName,
18+
# The root of the repository should be $(Build.SourcesDirectory) passed in from template
19+
[Parameter(Mandatory = $true)]
20+
[string] $RepoRoot
21+
)
22+
23+
$venvPath = Join-Path $RepoRoot $VenvName
24+
if (!(Test-Path $venvPath)) {
25+
Write-Host "Creating virtual environment '$VenvName'."
26+
python -m venv "$venvPath"
27+
Write-Host "Virtual environment '$VenvName' created."
28+
Write-Host "##vso[task.setvariable variable=$($VenvName)_LOCATION]$venvPath"
29+
}
30+
else {
31+
Write-Host "Virtual environment '$VenvName' already exists. Skipping creation."
32+
}

0 commit comments

Comments
 (0)