😀

Azure DevOps の ビルドパイプラインを CLI で試してみた

に公開

背景と目的

Azure DevOps の CLI を試したことが無かったので、何回も作ったり消したり出来るように調べた結果を残しておきます。

前提条件

コマンドの実施環境は、Mac + Azure CLI です。

zsh
% sw_vers
ProductName:    macOS
ProductVersion: 11.3
BuildVersion:   20E232

% az version                            
{
  "azure-cli": "2.22.1",
  "azure-cli-core": "2.22.1",
  "azure-cli-telemetry": "1.0.6",
  "extensions": {}
}

実施内容

zsh
# 環境変数を設定します
prefix=mnrdevops
organization=https://dev.azure.com/mnrsdemo/

# DevOps 拡張を有効にします
az extension add \
  --name azure-devops

# デフォルトのオーガニゼーションを設定します
az devops configure \
  --defaults organization=$organization

# DevOps にプロジェクトを作成します
az devops project create \
  --name ${prefix}-prj

# Repos を新たに作ります
az repos create \
  --name ${prefix} \
  --project ${prefix}-prj

# Repos の一覧を表示します
az repos list \
  --project ${prefix}-prj \
  --output table

# プロジェクト作成時にデフォルトで作成された Repos を削除します
az repos delete \
  --project ${prefix}-prj \
  --id $(az repos show \
  --project ${prefix}-prj \
  --repository ${prefix}-prj \
  --query id \
  --output tsv)

# Repos が一つになったことを確認します
az repos list \
  --project ${prefix}-prj \
  --output table

# GitHub から Pipelines のサンプルプロジェクトをインポートします
az repos import create \
  --git-source-url https://github.com/MicrosoftDocs/pipelines-java.git \
  --repository ${prefix} \
  --project ${prefix}-prj

# ローカルにソースコードをダウンロードします
git clone $(az repos show \
  --project ${prefix}-prj \
  --repository ${prefix} \
  --query remoteUrl \
  --output tsv)

# ディレクトリを移動します
cd ${prefix}

# Pipelines を作成します
az pipelines create \
  --name first-pipeline \
  --skip-first-run true

# Pipelines の設定ウィザードでデフォルトを選択します
Please enter a choice [Default choice(1)]: Maven
Please enter a choice [Default choice(1)]: View or edit the yaml
# ここでエディタが起動するので変数の箇所を書き換えて保存します
{{ branch }} -> master
{{ pool }} -> vmImage: ubuntu-latest
# 残りの選択肢もデフォルトを選択します
Please enter a choice [Default choice(1)]: Proceed with this yaml
Please enter a choice [Default choice(1)]: Commit directly to the master branch.

# 作成された Pipelines を確認します
git pull
code azure-pipelines.yml

# Pipelines の一覧を表示します
az pipelines list \
  --output table

実施結果

作成した Pipelines を動かして結果を確認します。

zsh
# Pipelines を実行します
az pipelines run \
  --name first-pipeline \
  --output table

# 実行直後は Status が notStarted です
Run ID    Number      Status      Result    Pipeline ID    Pipeline Name    Source Branch    Queued Time                 Reason
--------  ----------  ----------  --------  -------------  ---------------  ---------------  --------------------------  --------
22        20210501.2  notStarted            16             first-pipeline   master           2021-05-01 21:46:27.441059  manual

# Pipelines の実行履歴を表示します
az pipelines build list \
  --output table

# この時点でも Status は notStarted です
ID    Number      Status      Result     Definition ID    Definition Name    Source Branch    Queued Time                 Reason
----  ----------  ----------  ---------  ---------------  -----------------  ---------------  --------------------------  --------
22    20210501.2  notStarted             16               first-pipeline     master           2021-05-01 21:46:27.441059  manual

# ビルド ID 22 を確認します
az pipelines build show \
  --id 22 \
  --output table

# この時点では Status は inProgress です
ID    Number      Status      Result    Definition ID    Definition Name    Source Branch    Queued Time                 Reason
----  ----------  ----------  --------  ---------------  -----------------  ---------------  --------------------------  --------
22    20210501.2  inProgress            16               first-pipeline     master           2021-05-01 21:46:27.441059  manual

# この時点では Status は completed になり、Result は succeeded です
ID    Number      Status     Result     Definition ID    Definition Name    Source Branch    Queued Time                 Reason
----  ----------  ---------  ---------  ---------------  -----------------  ---------------  --------------------------  --------
22    20210501.2  completed  succeeded  16               first-pipeline     master           2021-05-01 21:46:27.441059  manual

# 一通り試したのでプロジェクトごと削除します(リソースグループを削除するみたいですね)
az devops project delete \
  --id $(az devops project show \
  --project ${prefix}-prj \
  --query id \
  --output tsv)

参考

最初のパイプラインの作成

az devops

Discussion