How to pass in environment variables when deploying to AKS from Azure DevOps

12/11/2020

I want to deploy a custom SQL Server image, which needs 4 environment variables passed in to AKS using the following pipeline definition:

  jobs:
  - deployment: Deploy
    condition: and(succeeded(), not(startsWith(variables['Build.SourceBranch'], 'refs/pull/')))
    displayName: Deploy
    pool:
      vmImage: $(vmImageName)
    environment: 'xxxx.default'
    strategy:
      runOnce:
        deploy:
          steps:
          - task: KubernetesManifest@0
            displayName: Create imagePullSecret
            inputs:
              action: createSecret
              namespace: $(k8sNamespace)
              secretName: $(imagePullSecret)
              dockerRegistryEndpoint: $(dockerRegistryServiceConnection)
              
          - task: KubernetesManifest@0
            displayName: Deploy to Kubernetes cluster
            inputs:
              action: deploy
              namespace: $(k8sNamespace)
              manifests: |
                $(Pipeline.Workspace)/manifests/deployment.yml
                $(Pipeline.Workspace)/manifests/service.yml
              imagePullSecrets: |
                $(imagePullSecret)
              containers: |
                $(containerRegistry)/$(imageRepository):$(tag)

The manifest files are created by Azure DevOps in this instance, so how would I go along, if I wanted to inject the SA_Password / inistial user configuration for this container?

-- Marco
azure-aks
azure-devops
kubernetes

2 Answers

12/12/2020

https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/deploy/kubernetes-manifest?view=azure-devops#deploy-action

kubernetes-manifest deploy action doesn't have the option to add extra environment variables. Feel free to open a feature request at https://github.com/microsoft/azure-pipelines-tasks/issues

steps:
- task: KubernetesManifest@0
  displayName: Patch
  inputs: 
    action: patch
    kind: pod
    name: demo-5fbc4d6cd9-pgxn4
    mergeStrategy: strategic
    patch: '{"spec":{"template":{"spec":{"containers":[{"env":[{"name":"SA_Password","value":"1234"}]}]}}}}'
    kubernetesServiceConnection: someK8sSC
    namespace: default
-- Tummala Dhanvi
Source: StackOverflow

12/17/2020

My contribution to Tummala comment is that if you have control over how the docker image is built, I suggest adding env variables from there instead. So if you have a docker build that is triggered when commiting on the develop branch, you can just pass that env to that docker image.

I have a dedicated post talking about CI/CD in Azure DevOps, in case you're interested in: Building CI/CD pipelines for Kubernetes with Azure DevOps and GitFlow.

-- Giang Phạm
Source: StackOverflow