Home > Mobile >  Conditional Approval in pipeline jobs in azure pipelines
Conditional Approval in pipeline jobs in azure pipelines

Time:03-31

I want to customize environment attribute to choose programmatically environment for approval (dev, preprod, prod). When I try to launch pipeline, I see this error. Is there an alternative?

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

CodePudding user response:

For this use case you should use parameters. This is because variables are not available during the initial parsing stage of the pipeline.

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

And then environment: ${{ parameters.environment }}

Or if you want to get fancy, you could do something like this:

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
  • Related