用Azure DevOps CLI 找出 Pipeline 相依的Variable Groups

Edward Kuo
May 17 · 4 min read
Image for post
Image for post

使用Azure DevOps時候,我們會把常用的Variable,透過Library設定成一組Variable Group,然後,在建立Pipeline時候,放入Variable Group到我們的Pipeline內

Image for post
Image for post

利用這方式就不需要在每個不同的Pipeline都要設定一次相同的Variable,但是,如果你今天有很多的Pipeline且又有很多組的Variable Group時候,以我來說只有Release Pipeline就超過50多種以上,Variable Group也有20種以上,要管理這些相對都是不容易的,最麻煩會在於由時候你不知道那些Variable Group有被那些Pipeline使用到,當你想刪除某個Variable Group時候,就有可能造成某些Pipeline發生異常。如果只是Variable Group的名稱,並不會影響到Pipeline。

而本身Azure DevOps目前並沒有辦法從介面看出Variable Group有被那些Pipeline參考使用,如果你是用YML撰寫,或許還有機會透過Azure DevOps Search方式找到。因此,這時候就可以透過CLI方式找到相依性

第一次要使用Azure DevOps CLI可以參考這篇[使用Azure CLI操作Azure DevOps]來上手。既然介面沒有提供這方式,我們就自己撰寫一個查詢的PowerShell,會用到下列指令

  • az pipelines release definition
  • az pipelines variable-group

這裡必須使用az pipelines release definition而不是使用az pipelines release 前者才可以顯示出Pipeline內的設定資訊。另外,使用az pipelines release definition list 是顯示的資訊雖然有VariableGroups和Variable,卻會看到資訊是空的,這並不是代表該Pipeline沒有設定,而必須透過az pipelines release definition Show 的方式才可以顯示

Image for post
Image for post

因此,如果今日我們想要Variable Groups有被那些Pipeline使用過,首先我們可以用下面指令找出Variable Groups的ID

az pipelines variable-group list

為什麼要用ID?主要在Pipeline對應到的Variable Groups內的設定都是使用ID

Image for post
Image for post

因此,必須把想要找的Group名字的ID找出來,一旦有了ID,就只需要找尋到在Release Pipeline有與這個Group相依的Pipeline就可以了,完整程式碼如下:

程式碼中的3,是代表某個Variable Groups的ID。今日要換成去找 Build Pipeline 的Variable Groups相依性,只要換成az pipelines build definition list 就可以了

參考資料

EK.Technology Learn

Design,Thinking,Coding & have fun every thing

Edward Kuo

Written by

Enterprise IT Manager / Microsoft Regional Director / Microsoft MVP / DevOps Expert / Speaker, About me: https://profile.edwardkuo.dev/about/

EK.Technology Learn

Design,Thinking,Coding & have fun every thing

Edward Kuo

Written by

Enterprise IT Manager / Microsoft Regional Director / Microsoft MVP / DevOps Expert / Speaker, About me: https://profile.edwardkuo.dev/about/

EK.Technology Learn

Design,Thinking,Coding & have fun every thing

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store