Windows-powershell / PowerShell-master /.github /instructions /onebranch-restore-phase-pattern.instructions.md
metadata
applyTo: .pipelines/**/*.{yml,yaml}
OneBranch Restore Phase Pattern
Overview
When steps need to run in the OneBranch restore phase (before the main build phase), the ob_restore_phase environment variable must be set in the env: block of each individual step.
Pattern
✅ Correct (Working Pattern)
parameters:
- name: "ob_restore_phase"
type: boolean
default: true # or false if you don't want restore phase
steps:
- powershell: |
# script content
displayName: 'Step Name'
env:
ob_restore_phase: ${{ parameters.ob_restore_phase }}
The key is to:
- Define
ob_restore_phaseas a boolean parameter - Set
ob_restore_phase: ${{ parameters.ob_restore_phase }}directly in each step'senv:block - Pass
trueto run in restore phase,falseto run in normal build phase
❌ Incorrect (Does Not Work)
steps:
- powershell: |
# script content
displayName: 'Step Name'
${{ if eq(parameters.useRestorePhase, 'yes') }}:
env:
ob_restore_phase: true
Using conditionals at the same indentation level as env: causes only the first step to execute in restore phase.
Parameters
Templates using this pattern should accept an ob_restore_phase boolean parameter:
parameters:
- name: "ob_restore_phase"
type: boolean
default: true # Set to true to run in restore phase by default
Reference Examples
Working examples of this pattern can be found in:
.pipelines/templates/insert-nuget-config-azfeed.yml- Demonstrates the correct pattern.pipelines/templates/SetVersionVariables.yml- Updated to use this pattern
Why This Matters
The restore phase in OneBranch pipelines runs before signing and other build operations. Steps that need to:
- Set environment variables for the entire build
- Configure authentication
- Prepare the repository structure
Must run in the restore phase to be available when subsequent stages execute.
Common Use Cases
- Setting
REPOROOTvariable - Configuring NuGet feeds with authentication
- Setting version variables
- Repository preparation and validation
Troubleshooting
If only the first step in your template is running in restore phase:
- Check that
env:block exists for each step - Verify the conditional
${{ if ... }}:is inside theenv:block - Confirm indentation is correct (conditional is indented under
env:)