1

Automatically set Azure Service Bus Queue Connection Strings during the Deployme...

 2 years ago
source link: https://www.programmingwithwolfgang.com/automatically-set-service-bus-queue-connection-string-during-deployment/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Automatically set Azure Service Bus Queue Connection Strings during the Deployment

4 days ago2021-11-08T00:00:00+01:00 by Wolfgang Ofner

In a previous post, I showed how to add the connection string of an Azure Service Bus Queue to your application using Azure DevOps pipelines. This was done using variables. The solution works, but it is not dynamic. In a cloud environment, you often delete and re-create environments. Therefore, you will have different access tokens which results in changing connection strings.

In this post, I will show you how you can read the connection string using Azure CLI and then pass it to your application inside an Azure DevOps pipeline.

This post is part of “Microservice Series - From Zero to Hero”.

Use Azure CLI to read the Azure Service Bus Queue Connection String

You can find the code of the demo on Github.

Using Azure CLI allows you to read the connection string of an Azure Service Bus Queue with a single command using the Azure CLI task in the Azure DevOps pipeline. After reading the connection string, use Write-Host to set it to the AzureServiceBusConnectionString variable.

- task: AzureCLI@2 displayName: "Read Connection String for Azure Service Bus Queue" inputs: azureSubscription: '${{ parameters.azureSubscription }}' scriptType: 'pscore' scriptLocation: 'inlineScript' inlineScript: | $connectionString = "$(az servicebus queue authorization-rule keys list -g '${{ parameters.resourceGroup }}' --namespace-name '${{ parameters.serviceBusNamespaceName }}' -n '${{ parameters.serviceBusQueueSasName }}' --queue-name '${{ parameters.serviceBusQueueName }}' --query primaryConnectionString)" Write-Host "##vso[task.setvariable variable=AzureServiceBusConnectionString;]$connectionString"

Use a Template inside the Azure DevOps Pipeline

In Improve Azure DevOps YAML Pipelines with Templates, I explained how to use templates to make your pipeline clearer. To continue using templates, I placed the code from above in a separate template named GetServiceBusConnectionString. Then I call this template during the deployment and pass the needed variables as parameters.

- deployment: Web_Test displayName: 'Deploy CustomerApi to the customerapi-test environment' environment: customerapi-test strategy: runOnce: deploy: steps: - download: CustomerApiBuild artifact: $(ArtifactName) - template: templates/GetServiceBusConnectionString.yml parameters: azureSubscription: $(AzureSubscription) resourceGroup: $(ClusterResourceGroup) serviceBusNamespaceName: $(ServiceBusNamespaceName) serviceBusQueueName: $(ServiceBusQueueName) serviceBusQueueSasName: $(ServiceBusQueueSasName)

Note that I use this template during the deployment of the test and production environment. The finished template looks as follows:

parameters: - name: azureSubscription type: string default: - name: resourceGroup type: string default: - name: serviceBusNamespaceName type: string default: - name: serviceBusQueueName type: string default: - name: serviceBusQueueSasName type: string default:

steps: - task: AzureCLI@2 displayName: "Read Connection String for Azure Service Bus Queue" inputs: azureSubscription: '${{ parameters.azureSubscription }}' scriptType: 'pscore' scriptLocation: 'inlineScript' inlineScript: | $connectionString = "$(az servicebus queue authorization-rule keys list -g '${{ parameters.resourceGroup }}' --namespace-name '${{ parameters.serviceBusNamespaceName }}' -n '${{ parameters.serviceBusQueueSasName }}' --queue-name '${{ parameters.serviceBusQueueName }}' --query primaryConnectionString)" Write-Host "##vso[task.setvariable variable=AzureServiceBusConnectionString;]$connectionString"

Lastly, add the following variables to your pipeline. Note to replace my values with your corresponding ones.

ServiceBusNamespaceName: microservicedemo ServiceBusQueueName: CustomerQueue ServiceBusQueueSasName: SendKey

That’s already all you have to change. You don’t have to change more because the output variable of the GetServiceBusConnectionString template has the same name as the previously used one. The Tokenizer task will read the variable and add it to the Helm chart. You can read more about that in Replace Helm Chart Variables in your CI/CD Pipeline with Tokenizer.

Run your application and you should still be able to access your queue.

Conclusion

Using a cloud environment allows your infrastructure and applications to be flexible. Therefore, your configuration also has to be flexible. This post showed how you can easily read the connection string of an Azure Service Bus Queue using Azure CLI. By doing so, your application will always read the correct connection string and will work no matter what happens to the underlying infrastructure (as long as the Azure Service Bus Queue exists obviously).

You can find the code of the demo on Github.

This post is part of “Microservice Series - From Zero to Hero”.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK