Azure DevOps流水线读取config.ps1变量并执行脚本的问题
改用config.ps1实现Azure DevOps流水线变量读取与环境变量设置
1. 创建config.ps1配置文件
在仓库根目录创建config.ps1,直接定义并设置需要的环境变量,无需额外依赖模块:
# 定义并设置环境变量 $env:TGRP_FOLDER_PATH = "你的目标文件夹路径" # 可根据需求添加其他变量,比如: # $env:OTHER_CUSTOM_VAR = "对应值"
2. 修改Azure DevOps流水线配置
替换原流水线中读取YAML的逻辑,改为调用config.ps1,同时保留原有部署流程:
trigger: - main - test pool: vmImage: 'windows-latest' variables: MES_SERVER_USERNAME: '$(SERVER_USERNAME)' MES_SERVER_PASSWORD: '$(SERVER_PASSWORD)' TEST_SERVER_IP: '$(TEST_IP)' PROD_SERVER_IP: '$(PROD_IP)' stages: - stage: DeployToServer jobs: - deployment: DeployToTest displayName: '部署到测试环境部署组' condition: eq(variables['Build.SourceBranch'], 'refs/heads/test') environment: 'TestEnvironment' strategy: runOnce: deploy: steps: - powershell: | # 调用config.ps1设置环境变量(路径根据实际存放位置调整) . $(System.DefaultWorkingDirectory)/config.ps1 $organization = "$(System.CollectionUri)" $project = "$(System.TeamProject)" $buildId = "$(Build.BuildId)" $patToken = "$(System.AccessToken)" $uri = "$organization/$project/_apis/build/builds/$buildId/changes?api-version=6.0" $headers = @{ Authorization = "Bearer $patToken" } $changedFiles = Invoke-RestMethod -Uri $uri -Method Get -Headers $headers | ForEach-Object { $_.value.items.path } Write-Host "变更文件列表:" $changedFiles | ForEach-Object { Write-Host $_ } $changedFilesPath = "$(System.DefaultWorkingDirectory)/changedFiles.txt" $changedFiles | Out-File -FilePath $changedFilesPath $scriptPath = "$(System.DefaultWorkingDirectory)/scripts/update-aip.ps1" Write-Host "执行脚本路径: $scriptPath" & $scriptPath -ChangedFilesPath $changedFilesPath displayName: '获取变更文件并执行PowerShell脚本' env: SYSTEM_DEFAULTWORKINGDIRECTORY: $(System.DefaultWorkingDirectory) MES_SERVER_USERNAME: $(MES_SERVER_USERNAME) MES_SERVER_PASSWORD: $(MES_SERVER_PASSWORD) TEST_SERVER_IP: $(TEST_SERVER_IP) PROD_SERVER_IP: $(PROD_SERVER_IP) - deployment: DeployToMain displayName: '部署到主环境部署组' condition: eq(variables['Build.SourceBranch'], 'refs/heads/main') environment: 'MainEnvironment' strategy: runOnce: deploy: steps: - powershell: | # 调用config.ps1设置环境变量 . $(System.DefaultWorkingDirectory)/config.ps1 $organization = "$(System.CollectionUri)" $project = "$(System.TeamProject)" $buildId = "$(Build.BuildId)" $patToken = "$(System.AccessToken)" $uri = "$organization/$project/_apis/build/builds/$buildId/changes?api-version=6.0" $headers = @{ Authorization = "Bearer $patToken" } $changedFiles = Invoke-RestMethod -Uri $uri -Method Get -Headers $headers | ForEach-Object { $_.value.items.path } Write-Host "变更文件列表:" $changedFiles | ForEach-Object { Write-Host $_ } $changedFilesPath = "$(System.DefaultWorkingDirectory)/changedFiles.txt" $changedFiles | Out-File -FilePath $changedFilesPath $scriptPath = "$(System.DefaultWorkingDirectory)/scripts/update-aip.ps1" Write-Host "执行脚本路径: $scriptPath" & $scriptPath -ChangedFilesPath $changedFilesPath displayName: '获取变更文件并执行PowerShell脚本' env: SYSTEM_DEFAULTWORKINGDIRECTORY: $(System.DefaultWorkingDirectory) MES_SERVER_USERNAME: $(MES_SERVER_USERNAME) MES_SERVER_PASSWORD: $(MES_SERVER_PASSWORD) TEST_SERVER_IP: $(TEST_SERVER_IP) PROD_SERVER_IP: $(PROD_SERVER_IP)
关键改动说明
- 移除了
Install-Module powershell-yaml和Import-Yaml相关代码,避免模块安装失败、路径解析错误等问题 - 通过PowerShell的点 sourcing(
.)调用config.ps1,直接在当前会话中设置环境变量,逻辑更简洁可靠 - 保持原流水线的部署逻辑和环境变量传递不变,仅替换配置读取部分
内容的提问来源于stack exchange,提问作者Tiago
相关产品推荐
相关产品推荐

