如何使用输出变量作为同一作业中另一个任务的字段值?

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

我对管道中的最后一个任务存在问题,在

kubernetesServiceConnection
的值中,它似乎正在将
$(GetServiceConnection.serviceconnection)
读取为文字值。这是我收到的错误消息:

管道无效。作业 ProcessApp_FileGen:步骤 KubernetesManifest 输入 kubernetesServiceEndpoint 引用服务 连接 $(GetServiceConnection.serviceconnection) 无法 被发现。服务连接不存在、已被禁用或 尚未获得使用授权

但是如果我删除最后 2 个任务,第二个任务可以正确回显

$(GetServiceConnection.serviceconnection)
的实际值。我还确保项目中的所有管道都可以访问服务连接。我什至尝试用单引号和双引号括起来,但我仍然收到相同的错误。

stages:
- stage: ProcessApps
  jobs:
  - job: DummyJob

  - ${{ each app in parameters.apps }}:
    - job: ProcessApp_${{ app.application }}
      displayName: "Processing App '${{ app.application }}'"      
      steps:

      - ${{ each overlay in app.environments }}:
        - task: Bash@3
          name: GetServiceConnection
          displayName: Get Service Connection for '${{ overlay }}'
          inputs:
            targetType: inline
            script: |
              # simplified code
              service_connection="sc-aks-dev"

              echo "##vso[task.setvariable variable=serviceconnection;isOutput=true]${service_connection}"              

        - task: Bash@3
          displayName: Processing Overlay '${{ overlay }}'
          inputs:            
            targetType: inline
            script: |
              echo $(GetServiceConnection.serviceconnection)

        - task: KubernetesManifest@1
          name: bake_${{ app.application }}_${{ overlay }}
          displayName: Baking ${{ app.application }} - ${{ overlay }}
          inputs:
            action: bake
            renderType: kustomize
            kustomizationPath: '$(System.DefaultWorkingDirectory)/01_applications/${{ app.application }}/overlays/${{ overlay }}'

        - task: KubernetesManifest@1
          displayName: Deploying
          inputs:
            kubernetesServiceConnection: $(GetServiceConnection.serviceconnection)
            manifests: $(bake_${{ app.application }}_${{ overlay }}.manifestsBundle)

但是如果我将其更改为

${{ GetServiceConnection.serviceconnection }}
,我在运行管道时会收到此错误:

无法识别的值:“GetServiceConnection”。位于位置1 在表达式内:GetServiceConnection.serviceconnection

azure-devops azure-pipelines
1个回答
0
投票

您看到的错误消息:

The pipeline is not valid. Job ProcessApp_FileGen: Step KubernetesManifest input kubernetesServiceEndpoint references service connection $(GetServiceConnection.serviceconnection) which could not be found. The service connection does not exist, has been disabled or has not been authorized for use

表示

kubernetesServiceConnection
字段按字面意思获取变量引用,而不是将其扩展为
GetServiceConnection
任务中设置的值。根据官方文档运行时表达式语法中的表格,这是
$(GetServiceConnection.serviceconnection)
在找不到时的渲染方式。

出现此问题的原因是该变量在解析服务连接时不可用,这发生在管道开始执行其任务之前。

换句话说,我们不能使用在管道执行期间通过

task.setvariable
字段中的
kubernetesServiceConnection
命令设置的变量。 服务连接在管道开始执行之前解析,因此它们无法访问管道运行期间设置的变量。

解决方法:

我注意到这个问题是您提出的另一个问题的后续问题。要解决此问题,您还可以使用该问题答案中相同的方式将服务连接名称作为参数传递给第二个管道。

假设您需要的服务连接的格式是前缀

sc-aks
(或者不同的app可以不同),加上各个环境的名称,比如
dev
,就变成了
sc-aks-dev

管道一:

stages:
  - stage: Initialize
    displayName: Initialize
    jobs:
    - job: AppsList
      steps:
      - task: Bash@3
        name: Applications
        inputs:
          targetType: inline
          script: |
   
            applications=(
              '{"application": "app1","serviceconnectionprefix": "sc-aks", "environments": ["dev", "test", "production"]},'
              '{"application": "app2", "serviceconnectionprefix": "sc-aks","environments": ["dev", "staging"]}'
            )
            echo "##vso[task.setvariable variable=apps;isOutput=true]${applications[@]}"

      - task: Bash@3
        inputs:
            targetType: 'inline'
            script: |
              az pipelines run {PipelineID} --organization $(System.CollectionUri) --project {ProjectName} --parameters apps="[$(Applications.apps)]"
        env:
          AZURE_DEVOPS_EXT_PAT: $(system.accesstoken)

管道二:


parameters:
- name: apps
  type: object
  default: []

stages:
- stage: ProcessApps
  jobs:
  - job: DummyJob

  - ${{ each app in parameters.apps }}:
    - job: ProcessApp_${{ app.application }}
      displayName: "Processing App '${{ app.application }}'"      
      steps:

      - ${{ each overlay in app.environments }}:
        - task: Bash@3
          displayName: Processing Overlay '${{ overlay }}'
          inputs:            
            targetType: inline
            script: |
              echo ${{ app.serviceconnectionprefix }}-${{ overlay }}

        - task: KubernetesManifest@1
          name: bake_${{ app.application }}_${{ overlay }}
          displayName: Baking ${{ app.application }} - ${{ overlay }}
          inputs:
            action: bake
            renderType: kustomize
            kustomizationPath: '$(System.DefaultWorkingDirectory)/01_applications/${{ app.application }}/overlays/${{ overlay }}'

        - task: KubernetesManifest@1
          displayName: Deploying
          inputs:
            kubernetesServiceConnection: '${{ app.serviceconnectionprefix }}-${{ overlay }}'
            manifests: $(bake_${{ app.application }}_${{ overlay }}.manifestsBundle)
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.