1
votes

My application's azure-pipeline.yml is using template defined in another repository.

I want to run this pipeline for 'dev' & 'master' branches under respective environment. So I have defined respective pipeline environments (DEV & PROD) in ADO > pipeline > Environment

Here under Approval & checks i added branch control so that 'dev' branch pipeline can run deploy stage in DEV environment & 'master' branch pipeline can run deploy stage in PROD environment.

My template is in another repository and has only 'master' branch.

Now when i run this pipeline for 'dev' branch it fails on check for 'Branch Control' as template file is 'master' branch. below is the error.

Check inputs:
 Allowed branches:refs/heads/dev
 Ensure consumed branch is protected: true
 Allow deployment of branches with unknown protection state: false

<br>Verifying branch for self:
Repository: my_app_repo
Branch: refs/heads/dev
Protection status: True
Result for evaluation: Successful 

<br>Verifying branch for templates:
Repository: my_template_repo
Branch: refs/heads/master
Protection status: True
Result for evaluation: Unsuccessful 
<br>Check evaluation failed

How can i handle it ?

Pipeline > Environment >

DEV_ENV > Approvals & Check > Branch Control > refs/heads/dev

STG_ENV > Approvals & Check > Branch Control > refs/heads/stage

PRD_ENV > Approvals & Check > Branch Control > refs/heads/master

My pipeline should run deploy stage in a specific env passed as parameter to the template. In pipeline we are sending the Env name as parameter.

resources:
  repositories:
    - repository: templates
      type: git
      name: project_name/my_template_repo
trigger:
  branches:
    include:
    - dev
    - stage
    - master
stages:
  - stage: "CheckOut"
    displayName: Checkout
    jobs:
      - job: Checkout
        displayName: Checkout Application
        pool:
          name: $(agent_name)
        workspace:
          clean: all 
        steps:
          - checkout: self
  - template: template_pipeline/pipeline_template.yml@templates
    parameters:
      agent_name: $(agent_name)
      env_name_dev: DEV_ENV<br>      env_name_stg: STG_ENV<br>      env_name_prod: STG_PRD
2

2 Answers

0
votes

You need to create deployment strategy to have the Environment embedded into the pipeline

So something similar to this. Refer to the docs

    jobs:
    - deployment: a_unique_name
      displayName: A Unique Name
      environment: ${{ parameters.target_environment }} # target_environment is the name of the Environment, DEV_ENV or STG_ENV etc
      strategy:
        runOnce:
          deploy:
            steps:
              - checkout: self
            ….

0
votes

I think as a solution

In branch control I should allow combination of branches (comma separated) for DEV & STG Env & for PROD Env allow only master branch.

Example: DEV_ENV allow branches " refs/heads/dev, refs/heads/master" STG_ENV allow branches " refs/heads/stg, refs/heads/master" PRD_ENV allow branches " refs/heads/master"