😀

Azure Automation の PAT 連携を Azure CLI でやってみた

に公開

以前、下記の記事を書きました。

https://qiita.com/mnrst/items/d6e10891f11c4e73b813

今回は、Azure Automation のソース管理と同期ジョブ設定で Azure Repos との PAT 連携を Azure CLI でやってみました。

PAT 作成

https://learn.microsoft.com/ja-jp/azure/automation/source-control-integration#minimum-pat-permissions-for-azure-devops

こちらのドキュメント通りに、最小限のアクセス許可を付与した PAT を作成します。

Scope アクセスの種類
Code Read
Project and team Read
Identity Read
User profile Read
Work items Read
Service connections 読み取り、クエリの実行、および管理

ソース管理と同期ジョブを設定

bash
# 環境変数をセット
prefix=mnrauto
pat=your_pat_here
commitid=$(git log --pretty=format:"%H" -1)

# ソース管理を設定
az rest \
  --method put \
  --url "$(az automation account show \
  --automation-account-name ${prefix} \
  --resource-group ${prefix}-rg \
  --query id \
  --output tsv)/sourceControls/${prefix}?api-version=2019-06-01" \
  --body '{
    "properties": {
      "repoUrl": "https://mnrsdev@dev.azure.com/mnrsdev/mnrsdev/_git/'${prefix}'",
      "branch": "main",
      "folderPath": "/",
      "autoSync": true,
      "publishRunbook": true,
      "sourceType": "VsoGit",
      "securityToken": {
        "accessToken": "'$pat'",
        "tokenType": "PersonalAccessToken"
      },
      "description": ""
    }
  }'

# 同期ジョブを設定
az rest \
  --method put \
  --url "$(az automation account show \
  --automation-account-name ${prefix} \
  --resource-group ${prefix}-rg \
  --query id \
  --output tsv)/sourceControls/${prefix}/sourceControlSyncJobs/$(uuidgen)?api-version=2019-06-01" \
  --body '{
    "properties": {
      "commitId": "'$commitid'"
    }
  }'

# 同期ジョブの状態を確認
az rest \
  --method get \
  --url "$(az automation account show \
  --automation-account-name ${prefix} \
  --resource-group ${prefix}-rg \
  --query id \
  --output tsv)/sourceControls/${prefix}/sourceControlSyncJobs?api-version=2019-06-01"

参考

https://learn.microsoft.com/ja-jp/rest/api/automation/source-control/create-or-update?tabs=HTTP

Discussion