天蓝色管道中管道作业的有条件批准

Conditional Approval in pipeline jobs in azure pipelines

我想自定义环境属性以编程方式选择批准环境(dev、preprod、prod)。当我尝试启动管道时,我看到了这个错误。有其他选择吗?

Job : Environment $(environment) could not be found. The environment does not exist or has not been authorized for use.

    variables:
      environment: dev
    jobs:
      - deployment: test
        displayName: test
        timeoutInMinutes: 0
        # creates an environment if it doesn't exist
        environment: $(environment)
        strategy:
          runOnce:
            deploy:
              steps:
                - checkout: self
                  clean: true
                  displayName : Checkout repository
                - task: NodeTool@0
                  inputs:
                    versionSpec: '16.x'
                    checkLatest: true

对于这个用例,您应该使用参数。这是因为变量在管道的初始解析阶段不可用。

parameters:
- name: "environment"
  type: string
  default: "development"

然后environment: ${{ parameters.environment }}

或者如果你想花哨一些,你可以这样做:

parameters:
 - name: "environments"
   type: object
   default:
   - name: development
     param1: value
     param2: value
   - name: test
     param1: value
     param2: value

# This will look through the environment parameter and create a job for each environment.

- ${{ each environment in parameters.environments }} :
    jobs:
      - deployment: test
        #read in vars from a file in variables/development.yml
        variables: variables/${{ environment.name }}.yml
        displayName: test
        timeoutInMinutes: 0
        # creates an environment if it doesn't exist
        environment: ${{ environment.name }}
        strategy:
          runOnce:
            deploy:
              steps:
                - checkout: self
                  clean: true
                  displayName : Checkout repository
                - task: NodeTool@0
                  inputs:
                    versionSpec: '16.x'
                    checkLatest: true