Welcome to JiKe DevOps Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
802 views
in Technique[技术] by (71.8m points)

continuous integration - How To Be 'DRY' In Azure Devops With Array-Like Parameters

In this YAML, I would like to trigger a build template based on the incoming branch name. My current solution involves using the '- ${{ if eq() }}' expression to gate the execution of each stage based on the triggering branch. one of the parameters for this workflow is a list of projects, which funtions like an array. Its important that this structure is preserved, since the invoked template will create jobs based on this parameters, so no string-array-split nonsense.

I want to avoid recording the parameter 'projects' multiple times, but variables do not support this kind of syntax (you get 'Error: unexpected sequence')

#Define your target repository (where your source lives). Must be in an Azure
resources:
  repositories:
    - repository: "ExampleRepo"
      type: git
      name: ExampleRepo
      trigger: 
        branches:
          include: 
            - development
            - staging
            - production

#Disable self-trigger
trigger:
  branches:
    exclude:
    - master
    - main

#Use a cloud image. If you need test executions longer than 1hr, you will need to use self-hosted agents
pool:
  vmImage: 'windows-latest'

#Define variables that are not specific to deployment environment 
variables:
  repository_name: 'ExampleRepo'


#Define environment/branch specific values (use variables for any constants)
jobs:
- ${{ if eq(variables['Build.SourceBranchName'], 'development') }}:
  - template: /build/example_template.yml
    parameters:
      deployment_environment: "Development"
      projects: [
        "Project1",
        "Project2",
        "Project3"
      ]
- ${{ if eq(variables['Build.SourceBranchName'], 'staging') }}:
  - template: /build/example_template.yml
    parameters:
      deployment_environment: "Staging"
      projects: [
        "Project1",
        "Project2",
        "Project3"
      ]
- ${{ if eq(variables['Build.SourceBranchName'], 'production') }}:
  - template: /build/example_template.yml
    parameters:
      deployment_environment: "Production"
      projects: [
        "Project1",
        "Project2",
        "Project3"
      ]
question from:https://stackoverflow.com/questions/65941957/how-to-be-dry-in-azure-devops-with-array-like-parameters

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

Please log in or register to answer this question.

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to JiKe DevOps Community for programmer and developer-Open, Learning and Share
...