Replace kubernetes yaml environment variables in Azure pipeline?

11/24/2021

I have an azure devops pipeline that I'm using to deploy a kubernetes project to rancher. In my k8s deployment.yaml file I have environment variables defined like this:

      containers:
        - name: frontend
          env:
          - name: GIT_HASH
            value: dummy_value

I want to be able to replace the GIT_HASH with a value created in the azure yml pipeline. Specifically, I have a script to get a git commit e.g:

   - task: Bash@3
      displayName: Set the short git hash as a variable
      inputs:
        targetType: 'inline'
        script: |
          short_hash=$(git rev-parse --short=7 HEAD)
          echo "##vso[task.setvariable variable=git-hash;]$short_hash"

And I want to be able to inject this value into kubernetes as the GIT_HASH. Is there a way to do this? I've tried using the qetza.replacetokens.replacetokens-task.replacetokens@3 but can't get it to work.

-- Nespony
azure
azure-devops
kubernetes
rancher

1 Answer

12/7/2021

Posting comment as the community wiki answer for better visibility.


Have you considered using Helm or Kustomize to template your deployment manifests instead of relying on token replacement?

More information can be found on official Azure documentation

-- Bazhikov
Source: StackOverflow