Here we are going to create a release pipeline for the API and the Website which will be triggered from our build pipelines. Essentially these pipelines are using deploying the YAML files from our build artifacts using the Kubernetes CLI as we did manually in the Working with the Kubernetes CLI Stage
Other than the fact its all automated the main difference here, is that we will change the placeholder values in the configs and YAML templates with variables we set for each stage of the deplpyment (Dev, UAT, Pre-Prod, Prod, etc)
https://marketplace.visualstudio.com/items?itemName=qetza.replacetokens
This will create Stage 1, you can rename this stage to whatever you like, for example Dev, UAT, Pre-Prod, etc
Once created click on the little pen where it says "New release pipeline" and rename the release to "API Release"
Click the plus icon next to "Agent Job" to add a new task and search for "replace"
This addon needs to have been installed - https://marketplace.visualstudio.com/items?itemName=qetza.replacetokens
In the List of tasks on the left, drag the Replace Tokens to the top
Set the target files to equal
**/*.yaml
**/*.json
**/*.xml
This task will find all files in our artifacts that are YAML, Json or XML and then replace any values that match a pattern #{somethingToBeReplaced}#
with the variables we have set
Click on the variables tab and add two variables
- registrylocation - set the value to your ACR login server uri
- namespace - set this to
dev
and change the scope to the name of your first stage
Click on the Tasks table and edit the Kubectl Apply Task
- Choose your subscription from the Azure Subscription drop down menu
- Choose the Resource Group that your AKS cluster belongs to in the Resource Group drop down menu
- Choose your cluster from the Kubernetes cluster drop down menu
- Type
$(namespace)
into the Namespace (this will be replaced by the variable for this stage which is 'dev'. You should have already have created a dev namespace during the creating your environment section) - Tick the box labelled "use configuration files" and then select the deployment-api.yaml file
Click on the 'Pipeline' tab and hove over the dev stage. Two buttons should appear and once will say clone. Click this button. Then rename the new stage to 'Prod Stage'
You should now see that a second namespace variable has been added for the new Prod-Stage.
Set this to prod
This means the API will be deployed into the Dev namespace in the first stage of our deployment and the prod namespace in the second stage of our deployment.
As this has not been triggered by a build, you will have to select which build to release in the popup.
Going forwarded, everytime a new build of the API is completed, this release pipeline will now deploy the API to both the dev and prod namespaces in you AKS cluster.
The release for the website is a little more complex as the service has more configuration, which we will inject into the container using config maps and secrets.
Upto and including the replace tokens task, but this time choose the Website build as the artifact and name the release "Website Release"
These keys need to exactly match the placeholders in the YAML templates, JSON and XML files in our build artifacts
- apiKey - set this to whatever you like and click the lock button to hide it
- apiSecret - set this to whatever you like and click the lock button to hide it
- apiname - set this to
api
as that is the name of the API service in the deployment-api.yaml template - EnvironmentName -
Kubernetes
- namespace -
dev
- change the scope to the name of your first stage - registrylocation - set the value to your ACR login server uri
- theme - choose from blue, red or default - change the scope to the name of your first stage
Add a new task to the stage by clicking the plus icon next to the Agent Job and search for Kubernetes
Edit the new task and set the following properties:
- Name the task
delete secret
- Choose your subscription from the Azure Subscription drop down menu
- Choose the Resource Group that your AKS cluster belongs to in the Resource Group drop down menu
- Choose your cluster from the Kubernetes cluster drop down menu
- Type
$(namespace)
into the Namespace (you should have already created a dev namespace during the creating your environment section) - Command - delete
- Arguments -
secret website-secret
- Output format - keep this empty
- Check the box labelled "Continue on error"
Add a new task Kubernetes task and Edit it with the following:
- Name the task
create secret
- Choose your subscription from the Azure Subscription drop down menu
- Choose the Resource Group that your AKS cluster belongs to in the Resource Group drop down menu
- Choose your cluster from the Kubernetes cluster drop down menu
- Type
$(namespace)
into the Namespace (you should have already created a dev namespace during the creating your environment section) - Command - Create
- Arguments -
secret generic website-secret --from-file=<Location of secret-website.json here>
Tip - To find the location of secret-website.json check the box labelled use configuration files and then select the secret-website.json from the popup window. Copy and paste this into the arguments and then un-check the check box again. Make sure clear the box with the file path before unchecking the check box again
Tip - to find the correct path of the secret-website.json file, check the tick box labelled "use configurations" box above and select the file, copy the location back into the arguments, then un-check the use configurationsbox
Add a new task to the stage by clicking the plus icon next to the Agent Job and search for Kubernetes
Edit the new task and set the following properties:
- Name the task
delete xml config map
- Choose your subscription from the Azure Subscription drop down menu
- Choose the Resource Group that your AKS cluster belongs to in the Resource Group drop down menu
- Choose your cluster from the Kubernetes cluster drop down menu
- Type
$(namespace)
into the Namespace (you should have already created a dev namespace during the creating your environment section) - Command - delete
- Arguments - ```configmap config-xml-website``
- Output format - keep this empty
- Check the box labelled "Continue on error"
Add a new task Kubernetes task and Edit it with the following:
- Name the task
create xml config map
- Choose your subscription from the Azure Subscription drop down menu
- Choose the Resource Group that your AKS cluster belongs to in the Resource Group drop down menu
- Choose your cluster from the Kubernetes cluster drop down menu
- Type
$(namespace)
into the Namespace (you should have already created a dev namespace during the creating your environment section) - Command - Create
- Arguments -
configmap config-xml-website --from-file=<Location of configmap-website.xml here>
Again use the "Use configuration files" check box to discover the location of the file
Add a new task Kubernetes task and Edit it with the following:
- Name the task
create json config map
- Choose your subscription from the Azure Subscription drop down menu
- Choose the Resource Group that your AKS cluster belongs to in the Resource Group drop down menu
- Choose your cluster from the Kubernetes cluster drop down menu
- Type
$(namespace)
into the Namespace (you should have already created a dev namespace during the creating your environment section) - Command - apply
- Check the "Use Configuration Files" box and select the configmap-website.yaml file
Add a new task Kubernetes task and Edit it with the following:
- Name the task
deploy website
- Choose your subscription from the Azure Subscription drop down menu
- Choose the Resource Group that your AKS cluster belongs to in the Resource Group drop down menu
- Choose your cluster from the Kubernetes cluster drop down menu
- Type
$(namespace)
into the Namespace (you should have already created a dev namespace during the creating your environment section) - Command - apply
- Check the "Use Configuration Files" box and select the deployment-website.yaml file
Click on the 'Pipeline' tab and hove over the dev stage. Two buttons should appear and once will say clone. Click this button. Then rename the new stage to 'Prod Stage'
You should now see that a second namespace variable has been added for the new Prod-Stage.
Set this to prod
This means the Website will be deployed into the Dev namespace in the first stage of our deployment and the prod namespace in the second stage of our deployment.
Finally save the changes and click on the Release button to trigger a new release. When triggering a release choose the latest build.
After following these steps you should now have two release pipelines that are triggered from your builds. These pipelines replace values in your YAML build artifacts with variables in your release (Per Environment) and then deploy the config maps, secrets and services into the dev namespace of your cluster.