0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Azure DevOpsのPipelineを使ってAzure App Configurationを更新する

Posted at

Azure DevOpsのPipelineを使って、GitリポジトリのJSOnファイルからAzure App Configurationを更新する方法を紹介します。

環境

  • Azure DevOps
    • Repos
    • Pipelines (YAML)

ReposにGitリポジトリを作成し、そこにコミットされたJSONファイルを元にApp Configurationを更新します。
この記事ではYAML方式のPipelineの使い方やAzure DevOpsのReposの使い方については触れません。

導入するメリット

Gitリポジトリに設定ファイルを置くことで、

  • App Configurationを手作業で更新しなくて済む。
  • 設定変更をGitのプルリクでチェックできるようになる。

などのメリットがあります。

JSONファイルの例

{
    "Integer": 1,
    "String": "string",
    "Object": {
        "Property1": 1,
        "Property2": "property",
        "Array": [
            1,
            2,
            3
        ],
        "ArrayObject": [
            {
                "Aaa": 1,
                "Bbb": 2
            },
            {
                "Aaa": 3,
                "Bbb": 4
            }
        ]
    }
}

Azure CLIコマンドを使って、App Configurationを更新する

App Configurationの更新は、Pipelineの用意されたタスクを実行するわけではなく、Azure CLIコマンドのaz appconfig kvを使います。

指定の仕方は色々ありますが、下記がコマンド例です。

az appconfig kv import --source file --name [App Configurationの名前] -s file --format json --path "[JSONファイルのパス" --content-type "application/json" --separator : --yes

簡単に説明すると、JSONを「:」でセパレートしながらインポートするようになっています。実際インポートされた後の状態については

パイプラインテンプレートの例

次のようにAzure CLIコマンドを呼び出してあげれば、Pipelineから流すことができます。

parameters:
  - name: subscriptionName
  - name: appConfigName
  - name: configFilePath

steps:
- task: AzureCLI@2
  displayName: 'Import: ${{ parameters.configFilePath }}'
  inputs:
    azureSubscription: '${{ parameters.subscriptionName }}'
    scriptType: ps
    scriptLocation: inlineScript
    inlineScript: |
      az appconfig kv import --source file --name ${{ parameters.appConfigName }} -s file --format json --path "${{ parameters.configFilePath }}" --content-type "application/json" --separator : --yes

インポートした結果

次の画像の様に、「:」(コロン)で区切られたキーで各設定値が格納されます。配列はパースされず、丸ごと挿入される点は注意です。

image.png

読み込み

ここではApp Configurationからの設定読み込みについては省略します。

ただ、「:」で分割するメリットは読み込み側にもあり、例えば "Object" の部分しか読み込みたくない場合、次のような指定をすることで余計な読み込みを避けることができます。

builder.ConfigurationBuilder
    .AddAzureAppConfiguration(options =>
    {
        options
            .Connect("App Configurationの接続文字列")
            .Select("Object:*");
    })
    .Build();

※厳密にはApp Configurationの接続文字列ではなくAAD認証に切り替えるべきかと思います

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?