LoginSignup
2
0

More than 1 year has passed since last update.

AzureDevOpsパイプラインで、環境変数を設定する

Posted at

背景

リリース作業の一連のパイプラインの中で、Azureリソースに対する環境変数の設定を手動で行う必要があった

Azureリソースに対する環境変数の設定もパイプラインから実施する

対応

AzureDevOpsのLibraryに環境変数を登録する

Pipelines > Library > 「+ Variable group」を押下

設定したい環境変数を定義して「Save」を押下

image-20220520195327824.png

パイプライン(yaml)側で、登録したLibraryを読み込んでAzureリソースに設定する

下記でLibraryに登録したvariablesを読み込み

variables:
- group: environment-variable

読み込んだvariablesは下記のように使える

"value": "$(CONNECTION_STRING)",
# variables groupから環境設定を読み込み
variables:
- group: environment-variable

steps:
# 環境変数設定
- task: AzureAppServiceSettings@1
  condition: eq(variables['Build.SourceBranch'], 'refs/heads/develop')
  displayName: 環境変数設定
  inputs:
    azureSubscription: xxxxx
    resourceGroupName: xxxxx
    appName: xxxxx
    slotName: stage
    appSettings: |
      [
        {
          "name": "SQL_SERVER_CONNECTION_STRING",
          "value": "$(CONNECTION_STRING)",
          "slotSetting": true
        },
        {
          "name": "XXX_XXX_XXX",
          "value": "$(xxxxxxxx)",
          "slotSetting": true
        },
        {
          "name": "XXX_XXX_XXX",
          "value": "$(xxxxxxxx)",
          "slotSetting": true
        },

      ]

補足:

conditionプロパティで、該当のtaskの実行条件を定義できる。

下記はdevelopブランチでパイプラインが動作しているときのみtaskを実行する。

  condition: eq(variables['Build.SourceBranch'], 'refs/heads/develop')

↓こんな感じで保護ブランチをconditionに指定しておけば、開発/本番環境向けに設定する環境変数を分岐することができる

  condition: eq(variables['Build.SourceBranch'], 'refs/heads/production')
2
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
2
0