How to target a different Azure subscription without changing every task

I recently needed to take a complicated Azure DevOps deployment and target a new subscription. On the surface this is not difficult. You simply add a new service connection and update all tasks that require a service connection to use the new connection. The problem is the more tasks that rely on that service connection the more likely you are to miss one. This will cause some resources to attempt to deploy to the wrong subscription.

Because Azure DevOps supports variables, I figured there had to be a way to leverage them to allow me to change the value in one place. The first thing I did was create a new service connection pointing to my second Azure Subscription.

Then I defined a variable in my pipeline name ServiceConnection to store the name of this service connection. Do not use a GUID as the name of your service connection. I thought using the Subscription ID would be a good idea. It was not. The ID of the service connection is also a GUID and using the Subscription ID caused Azure DevOps to think that was the ID of the service connection and it would fail to find it. Set your ServiceConnection variable to the name of the desired service connection.

clip_image001

Review your pipeline and update any tasks that require a service connection to use your variable instead.

clip_image002

With all your tasks updated to target a new subscription you simply create the service connection and update this single variable.

Comments (1) -

  • Svaen

    7/15/2019 3:43:04 PM | Reply

    Thank you for sharing this, I thought this wasn't possible.
    I had to check "Allow all pipelines to use this service connection" in the service connections policies tab to make this work for me.

Add comment

Loading