从 .ps1 文件中读取变量并使用 Git Repo 在 Pipeline 中运行 .ps1 脚本

问题描述 投票:0回答:2

我正在使用 Azure DevOps 存储库,并且正在开发管道。我希望它从 config.ps1 文件中读取变量并用它设置一个环境变量。之后我想运行一个 powershell 脚本。它给出了路径和读取参数的错误。

我不想阅读这个 config.yml 并安装这个模块,我想要一些更简单的东西,比如 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: 'Deploy to Test Deployment Group'
        condition: eq(variables['Build.SourceBranch'], 'refs/heads/test')
        environment: 'TestEnvironment'
        strategy:
          runOnce:
            deploy:
              steps:
                - powershell: |
                    Install-Module -Name powershell-yaml -Force -Scope CurrentUser
                    $config = Import-Yaml -Path "../config.yml"
                    $env:TGRP_FOLDER_PATH = $config.variables.TGRP_FOLDER_PATH

                    $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 "Changed files:"
                    $changedFiles | ForEach-Object { Write-Host $_ }

                    $changedFilesPath = "$(System.DefaultWorkingDirectory)/changedFiles.txt"
                    $changedFiles | Out-File -FilePath $changedFilesPath

                    $scriptPath = "$(System.DefaultWorkingDirectory)/scripts/update-aip.ps1"
                    Write-Host "Running PowerShell script at: $scriptPath"
                    & $scriptPath -ChangedFilesPath $changedFilesPath
                  displayName: 'Get Changed Files and Run PowerShell script'
                  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: 'Deploy to Main Deployment Group'
        condition: eq(variables['Build.SourceBranch'], 'refs/heads/main')
        environment: 'MainEnvironment'
        strategy:
          runOnce:
            deploy:
              steps:
                - powershell: |
                    Install-Module -Name powershell-yaml -Force -Scope CurrentUser
                    $config = Import-Yaml -Path "../config.yml"
                    $env:TGRP_FOLDER_PATH = $config.variables.TGRP_FOLDER_PATH

                    $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 "Changed files:"
                    $changedFiles | ForEach-Object { Write-Host $_ }

                    $changedFilesPath = "$(System.DefaultWorkingDirectory)/changedFiles.txt"
                    $changedFiles | Out-File -FilePath $changedFilesPath

                    $scriptPath = "$(System.DefaultWorkingDirectory)/scripts/update-aip.ps1"
                    Write-Host "Running PowerShell script at: $scriptPath"
                    & $scriptPath -ChangedFilesPath $changedFilesPath
                  displayName: 'Get Changed Files and Run PowerShell script'
                  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)
powershell azure-devops azure-pipelines devops
2个回答
0
投票

我建议不要通过双步方法使过程复杂化。相反,您可以直接在管道环境变量中设置值。鉴于您使用 YAML 来获取此值,因此不需要动态检索它。

但是,如果您确实更喜欢采用这种替代方法,则可以创建一个 Environment.psm1 文件并在其中定义全局变量。具体方法如下:

  1. 创建Environment.psm1文件:

    $global:TGRP_FOLDER_PATH = 'path'

  2. 在脚本中导入模块:

    Import-Module -Name Environment -Force

通过执行这些步骤,您将设置一个可以在脚本中轻松导入和使用的全局变量。

总而言之,虽然直接在管道中设置环境变量是更直接、更有效的方法,但如果您的用例需要,替代方法可以提供灵活性。


0
投票

我希望它从 config.ps1 文件中读取变量并用它设置一个环境变量。

我想从文件中读取静态值

而不是使用以下代码片段:

$config = Import-Yaml -Path "../config.yml"
$env:TGRP_FOLDER_PATH = $config.variables.TGRP_FOLDER_PATH

尝试使用管道中定义的变量或变量模板

例如定义如下的变量:

variables:
  - name: TGRP_FOLDER_PATH
    value: /my/folder/

可以使用宏语法作为环境变量进行访问

$(TGRP_FOLDER_PATH)

示例

为每个环境使用变量模板:

# /pipelines/variables/main-variables.yaml

variables:
  - name: TGRP_FOLDER_PATH
    value: /my/main/folder/
# /pipelines/my-pipeline.yaml

variables:
  - name: TGRP_FOLDER_PATH
    value: /my/prod/folder/

在不同的作业中使用每个模板:

stages:
  - stage: DeployToServer
    jobs:
      - deployment: DeployToTest
        displayName: 'Deploy to Test Deployment Group'
        condition: eq(variables['Build.SourceBranch'], 'refs/heads/test')
        environment: 'TestEnvironment'
        variables:
          - template: variables/test-variables.yaml # <---------- reference test variables template here
        strategy:
          runOnce:
            deploy:
              steps:
                - checkout: none
                - powershell: |

                    echo "$(TGRP_FOLDER_PATH)" # Output: /my/test/folder/

                    # rest of the script here
                  displayName: 'Get Changed Files and Run PowerShell script'

      - deployment: DeployToMain
        displayName: 'Deploy to Main Deployment Group'
        condition: eq(variables['Build.SourceBranch'], 'refs/heads/main')
        environment: 'MainEnvironment'
        variables:
          - template: variables/main-variables.yaml # <---------- reference test variables template here
        strategy:
          runOnce:
            deploy:
              steps:
                - checkout: none
                - powershell: |

                    echo "$(TGRP_FOLDER_PATH)" # Output: /my/main/folder/

                    # rest of the script here
                  displayName: 'Get Changed Files and Run PowerShell script'
© www.soinside.com 2019 - 2024. All rights reserved.