Home > OS >  substituting variables in yml files in azure pipelines
substituting variables in yml files in azure pipelines

Time:10-16

i am declaring variables on azure pipeline and want inject them into deployment.yml file, used replacetoken@4 task inorder to achieve this but variables are not updated with vaules.

pipeline code: 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

below is the deployment.yaml config

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

so in above config variables {replicas} and {dockertag} need to be replaced, however this is not happening. could someone help me what is missing here. thanks in advance

CodePudding user response:

The default token pattern is #{some_variable_here}#. And in your deployment.yml, it is {some_variable}.

Change it to #{replicas}# and #{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
  • Related