Hi @sandip_devaraj,
Welcome to our community. Thank you for sharing your issue.
To pass data values from an Excel file in Azure Pipelines, I would like to suggest that:
1: Store the Excel File in Your Repository
Ensure your Excel file (e.g., data.xlsx
) is stored in your repository so that Azure Pipelines can access it during the pipeline run. You can also store it in Azure Storage or Azure DevOps Artifacts if itβs too large.
2: Install Dependencies
Since Azure Pipelines runs scripts in an agent, ensure that your pipeline includes dependencies for reading Excel files. For example:
- If using Python, install
pandas
and openpyxl
:- script: pip install pandas openpyxl
displayName: 'Install dependencies'
- If using PowerShell, install
ImportExcel
module:- script: Install-Module -Name ImportExcel -Scope CurrentUser -Force -AllowClobber
displayName: 'Install PowerShell Excel Module'
3: Read Excel File and Pass Data as Variables
You need to extract data from the Excel file and pass it to the pipeline.
Option 1: Using Python to Read and Pass Variables
You can create a Python script (read_excel.py
) to extract data and set them as environment variables:
import pandas as pd
import os
# Load the Excel file
df = pd.read_excel("data.xlsx", sheet_name="Sheet1")
# Extract specific values (e.g., column A, first row)
value = df.iloc[0, 0]
# Print the value in a way Azure Pipelines can capture
print(f"##vso[task.setvariable variable=excelValue;]{value}")
Then, in your YAML pipeline, run the script and use the variable:
- script: python read_excel.py
displayName: 'Read Excel Data'
- script: echo $(excelValue)
displayName: 'Use Extracted Value'
Option 2: Using PowerShell to Read Excel Data
If you prefer PowerShell, create a script (read_excel.ps1
):
$excelData = Import-Excel -Path "data.xlsx" -WorksheetName "Sheet1"
$excelValue = $excelData[0].Column1 # Replace Column1 with actual column name
Write-Host "##vso[task.setvariable variable=excelValue]$excelValue"
Then, in your YAML pipeline, run it:
- script: pwsh read_excel.ps1
displayName: 'Read Excel Data'
- script: echo $(excelValue)
displayName: 'Use Extracted Value'
Step 4: Run the Pipeline and Use the Data
- Every time the pipeline runs, it reads the latest Excel file data and passes it as variables to subsequent tasks.
- Modify the script if you need to extract multiple values or dynamically reference column names.