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

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?

https://learn.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
You can do two options, write a script with yq (https://github.com/mikefarah/yq) to update the manifest files before deployment
Or use the KubernetesManifest patch option (https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/deploy/kubernetes-manifest?view=azure-devops#patch-action), for example
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

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.

Related

How to specify dynamic value as environment name for virtual machine resource in yaml pipelines

We are planning to move our release pipelines to yaml and we are ready with it.
I have multiple environments like dev, test and prod where I'm trying to use same deployment job templates for all environments.
jobs:
- deployment: deploy
displayName: Deploy
environment:
name: dev # This should be replaced with environment specific variable
resourceType: VirtualMachine
tags: WEB01
In above code, my intension is to provide name as environment specific variable. Could someone pls help?
Thank you!
You can do this with parameters, but you need to use the expression syntax which is evaluated when the pipeline is compiled
parameters:
- name: environment
type: string
default: dev
values:
- dev
- test
- preprod
- prod
jobs:
- deployment: deploy
displayName: Deploy
environment:
name: ${{ parameters.environment }}
resourceType: VirtualMachine
tags: WEB01
You might like my answer in another post. it does stages per environment with approvals.
https://stackoverflow.com/a/74159554/4485260

Using an Azure DevOps YAML deployment pipeline, the zip file does not unzip into the Azure App Service

I am building out a new yaml-based build process, provisioning via terraform, using Azure GIT repos. The release pipeline uses a yaml file (below) and I know all of the rest of it works because the whole deployment works except the deploy just drops the .zip file in the site/wwwroot directory instead of unzipping it. As far as I can tell it is all about the last two lines in the deploy section at the very bottom of the file.
Also, the package and zip themselves are fine, as I was able to manually deploy the code via Azure CLI in VS Code and it works.
Do I need something else to get the release to unzip the zip contents into the wwwroot dir?
name: Deploy $(Date:yyyyMMdd)$(Rev:.r) # build numbering format
trigger: none
resources:
repositories:
- repository: templates
type: git
name: repo/branch
ref: refs/heads/main
pipelines:
- pipeline: build
project: ADOProject
source: buildpipelinename
trigger:
branches:
- dev
variables:
- name: tier_prefix
value: D
- name: env_identifier
value: dev
- name: workstream
value: liehadmin
- name: region_prefix
value: C
- name: rgp_identifier
value: OURRGP
- group: terraform (non-prod)
stages:
- stage: infrastructure # our terraform methods are called from here
condition: and(succeeded(), in(variables['Build.Reason'], 'IndividualCI', 'BatchedCI', 'Manual', 'Schedule', 'ResourceTrigger'))
displayName: UPDATE INFRASTRUCTURE
jobs:
- template: 'core/terraform.yaml#templates'
parameters:
tier_prefix: $(tier_prefix)
tf_key: $(workstream)-$(env_identifier)
module_name: 'web_1tier1region_S'
variable_file_path: "$(System.DefaultWorkingDirectory)/$(Build.Repository.Name)/pipelines/$(workstream)/$(workstream).$(env_identifier).tfvars.json"
- stage: deploy
dependsOn: [infrastructure]
displayName: DEPLOY APP
jobs:
# Track deployments on the environment.
- deployment: DeployWeb
displayName: deploy Web App
pool:
vmImage: 'windows-latest'
# Creates an environment if it doesn't exist.
environment: 'site-dev'
strategy:
# Default deployment strategy, more coming...
runOnce:
deploy:
steps:
- checkout: self
- bash: ls $BUILD_ARTIFACTSTAGINGDIRECTORY
- bash: ls $PIPELINE_WORKSPACE
- task: AzureRmWebAppDeployment#4
displayName: 'deploy $(tier_prefix)-$(region_prefix)-$(workstream)-UI-01'
inputs:
ConnectionType: 'AzureRM'
azureSubscription: 'subname'
# SlotName: 'slot'
appType: 'webApp'
WebAppName: '$(tier_prefix)-$(region_prefix)-$(workstream)-UI-01'
ResourceGroupName: '$(region_prefix)-Y-Z-$(rgp_identifier)-$(tier_prefix)-XXX-XX'
# DeployToSlotOrASEFlag: true
Package: '$(Pipeline.Workspace)/build/repo_name'
enableCustomDeployment: true
deploymentMethod: 'zipDeploy'
I might have found the fix:
UseWebDeploy: true
enableCustomDeployment: true
deploymentMethod: 'runFromPackage'
Now, if you read the Microsoft documentation, this is weird, since it lists enableCustomDeployment as an argument alias for UseWebDeploy, but for some reason using both worked; my code deployed to site/wwwroot.
I too struggled with this and found this:
Go to App Service -> Configuration
Add a new Application Setting: WEBSITE_RUN_FROM_PACKAGE = 1
Go to App Service -> Console. Run the command dir
You should find, that the contents of your zip file is now shown, and that the site works.
You can read more here: https://github.com/projectkudu/kudu/wiki/WEBSITE_RUN_FROM_PACKAGE-and-WEBSITE_CONTENTAZUREFILECONNECTIONSTRING-Best-Practices

Why can't I use a variable to define the environment property in the Azure Pipeline YAML config file?

I'm trying to create a deploy pipeline YAML template for all environments/stages. I've set up the Environments on Azure DevOps so that I can add checks and approvals on the Test and Prod environments before they get deployed. I've set up a library group for each stage and each one of them has a variable called 'env' which defines the current stage running in the pipeline. For some reason, the environment property under the deployment job (see code snippet below) doesn't read that variable.
Has anyone faced this issue before, or is there a reason why the variable won't be read for that specific property?
Note: I've tested the variables and they do work, for example, the stage property outputs as 'deploy-dev/test/prod' (depending on the environment)
- stage: deploy-$(env)
jobs:
- deployment: DeployWeb
displayName: deploy Web App
pool:
vmImage: 'Ubuntu-latest'
# creates an environment if it doesn't exist
environment: 'smarthotel-$(env)'
strategy:
runOnce:
deploy:
steps:
- script: echo Hello world
You can't do this because it have to be know at compilation phase.
But you can try this (lets name this file deploy.yml):
parameters:
- name: env
type: string
default: 'dev'
stages:
- stage: deploy-${{ parameters.env }}
jobs:
- deployment: DeployWeb
displayName: deploy Web App
pool:
vmImage: 'Ubuntu-latest'
# creates an environment if it doesn't exist
environment: 'smarthotel-${{ parameters.env }}'
strategy:
runOnce:
deploy:
steps:
- script: echo Hello world
and then you need to run as follows (in build.yml file):
stages:
- template: deploy.yml
parameters:
env: dev
- template: deploy.yml
parameters:
env: qa
- template: deploy.yml
parameters:
env: prod

azure devops release number in CICD YML

I use CI/CD yml file to build and deploy stages, for the deployment part I want to use below sample template:
- stage: DevDeploy
displayName: Development Deployment stage
dependsOn: Build
jobs:
- deployment: Deploy
displayName: Deploy job
pool:
name: default
environment: 'Dev'
strategy:
rolling:
deploy:
steps:
- task: DownloadPipelineArtifact#1
inputs:
artifactName: 'deployment'
downloadPath: '$(System.ArtifactsDirectory)/deployment'
How can I add release number to the pipeline? [same as creation of classic release, you can see in below image:]
How can I add release number to the pipeline?
Azure devops provides a walkthrough of Multi-Stage YAML pipelines for CI/CD.
The YAML file contains two parts of CI/CD, and there is currently no separate YAML for Release pipeline. So we could not define a separate release name for Multi-Stage YAML pipelines like classic release.
If you want to custom the name of the Multi-Stage YAML pipeline, you could use the name property in the YAML to specify the pipeline instance name:
name: $(BuildDefinitionName)_$(Date:yyyyMMdd)$(Rev:.rr)
steps:
- script: echo hello world
You could check the official document YAML getting started - Pipeline instance name for some more details.

How to add Pre Deployment and Post deployment approvals in multistage YAML pipeline?

I have a Multistage YAML pipeline containing two stages 1) Build and 2) Deploy. Deploy stage is mentioned below and I want to add pre deploy approvals in that stage before deploy task. How can I add pre deployment and Post deployment approvals in multistage YAML pipeline?
stages:
- stage: 'Build'
# RESTORE
# Some task implementation
# BUILD
# Some task implementation
# PUBLISH
# Some task implementation
# DEPLOY STAGE
- stage: 'Dev'
displayName: 'Deploy to the dev environment'
dependsOn: Build
jobs:
- deployment: Deploy
pool:
vmImage: 'ubuntu-16.04'
environment: dev
variables:
- group: Release
strategy:
# HOW TO ADD PRE DEPLOYMENT AND POST DEPLOYMENT APPROVALS?
runOnce:
deploy:
steps:
- download: current
artifact: drop
- task: AzureWebApp#1
displayName: 'Azure App Service Deploy: website'
inputs:
azureSubscription: 'Resource Manager - Tailspin - Space Game'
appName: '$(WebAppNameDev)'
package: '$(Pipeline.Workspace)/drop/$(buildConfiguration)/*.zip'
For this issue ,currently, manual approval and evaluate artifact are the only available checks, and they can be configured on environments, service connections and agent pools only.
To define an approval on an environment:
In your Azure DevOps project, go to the environment that needs to be protected. (Learn more about creating an environment.)
Navigate to Approvals and Checks for the environment.
Select Create, provide the approvers and an optional message, and select Create again to to complete addition of the manual approval check.
Then use the environment: 'xxx' parameter in your yaml file. For example:
- stage: deploy
jobs:
- deployment: DeployWeb
displayName: deploy Web App
pool:
vmImage: 'Ubuntu-16.04'
# creates an environment if it doesn't exist
environment: 'multiStage'
The GUI and the yaml are interdependent in this case, it's not straight yaml.
For details ,please refer to this official document.