在 Azure 管道中替换 yml 文件中的变量

substituting variables in yml files in azure pipelines

我正在 azure 管道上声明变量,并希望将它们注入 deployment.yml 文件,使用 replacetoken@4 任务来实现这一点,但变量没有用 vaules 更新。

流水线代码: 天蓝色-pipelines.yaml

  stages:
  - stage: dockerbuild
  variables:
  - name: replicas
    value: 2
  jobs:
  - job: builddockerimageandpush
    pool: 
    name: Azure Pipelines
    steps:
        
      - task: Bash@3
        inputs:
        targetType: 'inline'
        script: |
          COMMAND="$(echo '$(build.sourceversion)' | cut -c-7)"
          echo "##vso[task.setvariable variable=dockertag]$COMMAND"
      displayName: GetCommitID
      
    
      - task: replacetokens@4
        inputs:
          rootDirectory: '$(System.DefaultWorkingDirectory)'
          targetFiles: 'deployment.yaml'
          encoding: 'auto'
          tokenPattern: 'default'
          writeBOM: true
          actionOnMissing: 'warn'
          keepToken: false
          actionOnNoFiles: 'continue'
          enableTransforms: false
          useLegacyPattern: false
          enableTelemetry: true

下面是 deployment.yaml 配置

apiVersion: apps/v1
kind: Deployment
metadata:
  name: messagesender
  labels:
  app: messagesender
spec:
  selector:
    matchLabels:
      app: messagesender
replicas: {replicas}
template:
  metadata:
    labels:
      app: messagesender    
  spec:
    containers:
    - name: devcontainer
      image: telefonicamdnidevcontainer.azurecr.io/messagesender:{dockertag}
      imagePullPolicy: Always

所以在上面的配置变量 {replicas} 和 {dockertag} 需要被替换,但是这并没有发生。有人可以帮我这里缺少什么吗?提前致谢

默认令牌模式是#{some_variable_here}#。在你的 deployment.yml 中,它是 {some_variable}.

将其更改为 #{replicas}##{dockertag}#

apiVersion: apps/v1
kind: Deployment
metadata:
  name: messagesender
  labels:
  app: messagesender
spec:
  selector:
    matchLabels:
      app: messagesender
replicas: #{replicas}#
template:
  metadata:
    labels:
      app: messagesender    
  spec:
    containers:
    - name: devcontainer
      image: telefonicamdnidevcontainer.azurecr.io/messagesender:#{dockertag}#
      imagePullPolicy: Always