【问题标题】:substituting variables in yml files in azure pipelines在 azure 管道中替换 yml 文件中的变量
【发布时间】:2021-12-01 20:20:58
【问题描述】:

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

管道代码: azure-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} 需要被替换,但是这不会发生。有人可以帮我这里缺少什么。提前致谢

【问题讨论】:

    标签: kubernetes azure-devops azure-pipelines azure-aks


    【解决方案1】:

    默认令牌模式是#{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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-07
      • 2022-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-30
      • 2018-09-13
      • 1970-01-01
      相关资源
      最近更新 更多