首页 > 解决方案 > 如何使用 yml 文件为 azure 管道中的不同分支设置不同的参数

问题描述

这是我正在尝试的 YML 代码。我有一个要求,在每个分支中都需要设置不同的参数。

例如,声纳分析只应在自动集成和部署时在开发分支中运行。我想知道如何根据分支将默认属性配置为 true。

parameters:
- name: sonar_analysis
  displayName: Sonarqube Analysis
  type: boolean
  default: true
- name: docker_build_push
  displayName: Docker Build and Push
  type: boolean
  default: false
- name: sonar_publish
  displayName: Sonarqube Publish
  type: boolean
  default: false
- name: owasp_dep_check
  displayName: OWASP Dependency Check
  type: boolean
  default: false
- name: angular_build
  displayName: Angular Build
  type: boolean
  default: false
- name: angular_build_gzip
  displayName: Angular Build GZIP
  type: boolean
  default: false



# specific path build
trigger:
  batch: true
  branches:
    include:
    - master
    - development

# YAML file in the main branch
schedules:
- cron: "0 0 * * *"
  displayName: Daily midnight build
  branches:
    include:
    - main
pool:
  vmImage: ubuntu-latest

variables:
  docker_ng_tag: 0.0.1
  

steps:

- task: NodeTool@0
  inputs:
    versionSpec: '10.16.0'

- ${{ if eq(parameters.angular_build, true) }}:
  - script: |
      npm install -g @angular/cli@11.2.2
      npm install
      npm link @angular/cli   
      ng build --prod --aot  --base-href /app/tms/ --deploy-url /app/tms/
    displayName: 'npm install and build'

- ${{ if eq(parameters.angular_build_gzip, true) }}:
  - script: |
      npm i gzipper@4.4.0 -g
      gzipper compress ./dist
    displayName: 'GZIP'

标签: azureazure-pipelinesangular-cli

解决方案


您可以像这样定义变量:

variables:
  isDevelopment: $[eq(variables['Build.SourceBranch'], 'refs/heads/development')]

stages:
- stage: A
  jobs:
  - job: A1
    steps:
      - script: echo Hello Stage A!

- stage: B
  condition: and(succeeded(), eq(variables.isDevelopment, 'true'))
  jobs:
  - job: B1
    steps:
      - script: echo Hello Stage B!
      - script: echo $(isDevelopment)

您也可以在步骤级别上使用条件。

我们知道在 cron 中您使用main分支和在 ci 触发器master中。我很确定你两者都没有。


推荐阅读