使用变量的动态环境名称不起作用

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

我需要根据Docker镜像的tag来部署到特定的环境

到目前为止我所做的:

parameters:    
  - name: vmToDeploy
    displayName: Environment Resource VM
    type: string
    default: ''

stages:
- stage: Deployment
  displayName: 🚀 Deployment
  jobs:
  - job: DeploymentTypeJob
    displayName: 🕵️ Determine Deployment Type
    steps: 
      - task: Bash@3
        displayName: 🕵️ Determine deployment type
        continueOnError: true
        name: DeploymentTypeJobTask
        inputs:
          targetType: 'inline'
          script: |
            # All business logic here
            echo "##vso[task.setvariable variable=IsDocker;isOutput=true]true"
            echo "##vso[task.setvariable variable=DockerImage;isOutput=true]foo/bar:qa.1234"
            echo "##vso[task.setvariable variable=EnvironmentName;isOutput=true]QA"
          workingDirectory: '$(Pipeline.Workspace)'
          failOnStderr: true
  - deployment:
    dependsOn: DeploymentTypeJob
    variables:
      IsDocker: $[ dependencies.DeploymentTypeJob.outputs['DeploymentTypeJobTask.IsDocker'] ]
      EnvironmentName: $[ dependencies.DeploymentTypeJob.outputs['DeploymentTypeJobTask.EnvironmentName'] ]
      DockerImage: $[ dependencies.DeploymentTypeJob.outputs['DeploymentTypeJobTask.DockerImage'] ]
    displayName: Deploy on ${{ variables.Deployment.EnvironmentName }} environment
    environment: 
     name: ${{ variables.EnvironmentName }}
     ${{ if ne(parameters['vmToDeploy'], '') }}:
       resourceName: ${{ parameters.vmToDeploy }}
     resourceType: VirtualMachine
    # Ensures to find only one artifact
    workspace:
      clean: all
    strategy: 
      runOnce:
        deploy:
          steps:
          - tasks: ...

结果:

/deploy/docker.yml@templates (Line: 73, Col: 7): Environment name cannot contain resourceName 'DeploymentTypeJob' if resourceName is explicitly specified 'my-vm'.

有什么理由吗?我已经阅读了有关该主题的几个主题,但仍然无法让某些东西发挥作用。

azure-pipelines
1个回答
0
投票

环境是一种“受保护的资源”,可能定义了批准和检查,因此必须在“编译时”(在管道运行之前)知道其值。 您可以使用参数: parameters: - name: environment type: string default: dev values: - dev - qa - prod # ... stages # ... - stage: DeployStage jobs: - deployment: Deploy environment: ${{ parameters.environment }} # ...

或者编译时可用的变量:

trigger: none

pool:
  vmImage: 'ubuntu-latest'

variables:
  environment: prod

# ...

stages
  # ...
  - stage: DeployStage
    jobs:
      - deployment: Deploy
        environment: ${{ variables.environment }}
        # ...

模板表达式
,例如
${{ if ... }}

等在编译时进行评估,因此它们也可以用于设置变量或动态加载变量模板

variables:
  - template: /pipelines/variables/${{ parameters.environment }}-variables.yaml

    
	

© www.soinside.com 2019 - 2024. All rights reserved.