将变量从Azure DevOps的Stage1导出并在Stage2中重新使用。

huangapple go评论71阅读模式
英文:

exporting & re-using variables from Stage1 to Stage2 in Azure devops

问题

  • stage: publish_service_names
    dependsOn: build_code
    jobs:

    • job: get_services
      pool:
      ${{ if eq(parameters.useCustomAgentPool, true) }}:
      name: ${{ parameters.customAgentPoolName }}
      ${{ if eq(parameters.useCustomAgentPool, false) }}:
      vmImage: ubuntu-latest
      steps:

      • bash: |
        services=$(cat $(Pipeline.Workspace)/variables/services.txt)
        echo "service List : $services"
        echo "##vso[task.setvariable variable=serviceName;]$services"
        name: getservices
  • ${{ if and(eq(parameters.buildDockerImages, true), eq(parameters.buildSingleServiceDockerImages, true)) }}:
    • stage: publish_docker_images
      dependsOn: publish_service_names
      variables:
      services: $[stageDependencies.publish_service_names.get_services.outputs['getservices.serviceName']]
      jobs:

      • ${{ each service in split(variables.services, ',') }}:
        • template: ./build-push-single-docker.yaml
          parameters:
          acrServiceConnection: ${{ parameters.acrServiceConnection}}
          serviceName: "${{ service }}"
          friendlyName: "${{ service }}"
          repositoryPathPrefix: ${{ parameters.repositoryPathPrefix }}

build-push-single-docker.yaml

jobs:

  • job: publish_${{ parameters.friendlyName }}
    pool:
    ${{ if eq(parameters.useCustomAgentPool, true) }}:
    name: ${{ parameters.customAgentPoolName }}
    ${{ if eq(parameters.useCustomAgentPool, false) }}:
    vmImage: ubuntu-latest
    steps:

    • bash: |
      echo "##vso[task.setvariable variable=buildArtifact;]ailens_services_supplemented_with_client_package"
      condition: eq('${{ parameters.addClientToSingleboxAndGUI }}', true)
      displayName: "Set artifact name"
英文:
  - stage: publish_service_names
    dependsOn: build_code
    jobs:
      - job: get_services
        pool:
          ${{ if eq(parameters.useCustomAgentPool, true) }}:
            name: ${{ parameters.customAgentPoolName }}
          ${{ if eq(parameters.useCustomAgentPool, false) }}:
            vmImage: ubuntu-latest
        steps:
          - bash: |
              services=$(cat $(Pipeline.Workspace)/variables/services.txt)
              echo "service List : $services"
              echo "##vso[task.setvariable variable=serviceName;]$services"
            name: getservices
  - ${{ if and(eq(parameters.buildDockerImages, true), eq(parameters.buildSingleServiceDockerImages, true)) }}:
      - stage: publish_docker_images
        dependsOn: publish_service_names
        variables:
          services: $[stageDependencies.publish_service_names.get_services.outputs['getservices.serviceName']]
        jobs:
**          - ${{ each service in split(variables.services, ',') }}:
**            - template: ./build-push-single-docker.yaml
              parameters:
                acrServiceConnection: ${{ parameters.acrServiceConnection}}
                serviceName: "${{ service }}"
                friendlyName: "${{ service }}"
                repositoryPathPrefix: ${{ parameters.repositoryPathPrefix }}

build-push-single-docker.yaml

jobs:
  - job: publish_${{ parameters.friendlyName }}
    pool:
      ${{ if eq(parameters.useCustomAgentPool, true) }}:
        name: ${{ parameters.customAgentPoolName }}
      ${{ if eq(parameters.useCustomAgentPool, false) }}:
        vmImage: ubuntu-latest
    steps:
      - bash: |
          echo "##vso[task.setvariable variable=buildArtifact;]ailens_services_supplemented_with_client_package"
        condition: eq('${{ parameters.addClientToSingleboxAndGUI }}', true)
        displayName: "Set artifact name"

Tried direct use of

  1. the stageDependencies variable directly in the template loop expression
  2. assigning it to variable and using the variable

both of them didnt solve my purpose.

答案1

得分: 1

看起来你缺少了 isOutput=true 属性。
尝试

echo "##vso[task.setvariable variable=serviceName;isOutput=true]$services"

更多信息请查看这里:https://learn.microsoft.com/en-us/azure/devops/pipelines/process/set-variables-scripts?view=azure-devops&tabs=bash#set-an-output-variable-for-use-in-future-stages

英文:

It looks like you are missing isOutput=true property.
Try

echo "##vso[task.setvariable variable=serviceName;isOutput=true]$services"

More info here: https://learn.microsoft.com/en-us/azure/devops/pipelines/process/set-variables-scripts?view=azure-devops&tabs=bash#set-an-output-variable-for-use-in-future-stages

答案2

得分: 0

jobs:

  • ${{ each service in split(variables.services, ',') }}:
    • template: ./build-push-single-docker.yaml
      parameters:
      acrServiceConnection: ${{ parameters.acrServiceConnection}}
      serviceName: "${{ service }}"
      friendlyName: "${{ service }}"
      repositoryPathPrefix: ${{ parameters.repositoryPathPrefix }}

The job execution is not looping over all service names.

  • publish S1
  • publish S2
英文:
jobs:
      - ${{ each service in split(variables.services, ',') }}:
        - template: ./build-push-single-docker.yaml
          parameters:
            acrServiceConnection: ${{ parameters.acrServiceConnection}}
            serviceName: "${{ service }}"
            friendlyName: "${{ service }}"
            repositoryPathPrefix: ${{ parameters.repositoryPathPrefix }}

The job execution is not looping over all service names.

  • publish S1
  • publish S2

huangapple
  • 本文由 发表于 2023年7月10日 17:35:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76652480.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定