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?

Azure DevOps PipelineでSchemaSpyを動かしSQLServerのER図を生成してStorageAccountの静的Webページにデプロイできるようにしたメモ

Last updated at Posted at 2025-05-03

概要

前回はGitHub ActionsでDBドキュメントをデプロイしたので、今回はAzure DevOps Pipeline でStorage Accountに公開する。

公開したER図

この時点のソース

環境

  • Windows 11
  • azure-cli 2.71.0

StorageAccount準備

参考記事 ではIP制限しているが、ここでは行わない。Bicepで作成する。

infra/bin/addWebStorageAccount.bash
#!/bin/bash

BIN_DIR=$(cd $(dirname $0) && pwd)
source $BIN_DIR/common.bash

cd $BICEP_DIR && az deployment group create \
  --name storageAccountForWebDeployment \
  --template-file addWebStorageAccount.bicep \
  -g $RESOURCE_GROUP_NAME
infra/biceps/addWebStorageAccount.bicep
@description('デプロイ先のリージョン')
param location string = resourceGroup().location

var storageAccountName = 'st${uniqueString(resourceGroup().id)}'

resource storageAccount 'Microsoft.Storage/storageAccounts@2024-01-01' = {
  name: storageAccountName
  location: location
  sku: { name: 'Standard_LRS' }
  kind: 'StorageV2'
  properties: {
    accessTier: 'Hot'
    minimumTlsVersion: 'TLS1_2'
    publicNetworkAccess: 'Enabled'
  }
}

resource staticWebsite 'Microsoft.Storage/storageAccounts/blobServices/containers@2024-01-01' = {
  name: '${storageAccount.name}/default/$web'
  properties: {
    publicAccess: 'None'
  }
}

resource blobService 'Microsoft.Storage/storageAccounts/blobServices@2024-01-01' = {
  parent: storageAccount
  name: 'default'
  properties: {
    deleteRetentionPolicy: { enabled: false }
}

resource customScript 'Microsoft.Resources/deploymentScripts@2023-08-01' = {
  name: 'enableStaticWebsiteScript'
  location: location
  kind: 'AzurePowerShell'
  properties: {
    azPowerShellVersion: '13.0'
    environmentVariables: [
      {
        name: 'storageAccountKey'
        value: storageAccount.listKeys().keys[0].value
      }
    ]
    arguments: '-storageAccountName ${storageAccount.name}'
    scriptContent: '''
      param(
        [string] $storageAccountName
      )

      $context = New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $env:storageAccountKey
      Enable-AzStorageStaticWebsite -Context $context -IndexDocument "index.html" -ErrorDocument404Path "404.html"
    '''
    cleanupPreference: 'OnSuccess'
    retentionInterval: 'PT1H'
  }
}

output staticWebsiteUrl string = 'https://${storageAccount.name}.z11.web.${environment().suffixes.storage}/'

実行。実行結果からjqでURLを取り出している。

$ ./infra/bin/addWebStorageAccount.bash | jq '.properties.outputs'

実行結果

{
  "staticWebsiteUrl": {
    "type": "String",
    "value": "https://<作成したStorageAccountの名前>.z11.web.core.windows.net/"
  }
}

パイプライン構築手順

参考記事 に従う。schemaspy.propertiesをセキュアに扱っているが、今回は外部DBに接続せず、DockerのDBを参照するのでそこまでしていない。

JCDBドライバ確認

ダウンロードページから確認する。リダイレクトのURLなので、実際のURLを確認にはひと手間必要。

image.png
image.png
image.png

image.png

下記のようなURLが取得できる。

https://download.microsoft.com/download/745da347-bd51-4bf0-a84e-3465608d8856/jpn/sqljdbc_12.10.0.0_jpn.tar.gz

SpySchema設定ファイル

schemaspy.properties
schemaspy.t=mssql08
schemaspy.dp=/drivers/mssql-jdbc-12.0.0.jre11.jar
schemaspy.host=host.docker.internal
schemaspy.port=1433
schemaspy.u=sa
schemaspy.p=MyPassword@123
schemaspy.db=test
schemaspy.schemas=atrpg
schemaspy.o=/output

パイプライン

Service Connection作成手順

.azure-devops/db-docs-pipeline.yaml
trigger:
  branches:
    include:
      - develop
  paths:
    include:
      - database/*
      - .azure-devops/db-docs-pipeline.yaml
pool:
  vmImage: ubuntu-latest
steps:
- checkout: self
- script: |
    chmod 777 database/initdb.d/entrypoint.sh
  displayName: 'permissions for initdb.d'
- task: DockerCompose@1
  displayName: 'Build and run SQL Server container'
  inputs:
    containerregistrytype: 'Azure Container Registry'
    dockerComposeFile: 'database/docker-compose.yml'
    action: 'Run services'
    detached: true

- script: |
    mkdir jdbc
    wget -P jdbc/ https://download.microsoft.com/download/745da347-bd51-4bf0-a84e-3465608d8856/jpn/sqljdbc_12.10.0.0_jpn.tar.gz
    tar -zxvf jdbc/sqljdbc_12.10.0.0_jpn.tar.gz
  displayName: 'Download JDBC Driver'

- script: |
    mkdir -m 777 output
    ls -R
  displayName: 'Create output directory'

- script: |
    docker pull schemaspy/schemaspy
    docker run \
    -v "$(Build.SourcesDirectory)/output:/output" \
    -v "$(Build.SourcesDirectory)/schemaspy.properties:/schemaspy.properties" \
    -v "$(Build.SourcesDirectory)/sqljdbc_12.10/jpn/jars/:/drivers" \
    --add-host=host.docker.internal:host-gateway \
    schemaspy/schemaspy:latest -connprops encrypt\\=false
  displayName: 'Run SchemaSpy'
- task: PublishPipelineArtifact@1
  displayName: 'Publish SchemaSpy output'
  inputs:
    targetPath: '$(Build.SourcesDirectory)/output'
    artifact: 'SchemaSpy'
    publishLocation: 'pipeline'

- task: DownloadPipelineArtifact@2
  displayName: 'Download SchemaSpy artifact'
  inputs:
    artifact: 'SchemaSpy'
    path: '$(Pipeline.Workspace)/SchemaSpy'

- task: AzureCLI@2
  inputs:
    azureSubscription:  <作成したServiceCoonnectionの名前> 
    scriptType: bash
    scriptLocation: inlineScript
    inlineScript: |
      az storage blob upload-batch \
        --account-name <作成したStorageAccountの名前> \
        --destination '$web' \
        --destination-path 'dbdocs' \
        --source "$(Pipeline.Workspace)/SchemaSpy" \
        --overwrite

dockerで建てたSQLServerに接続するために、schemaspy/schemaspy:latest -connprops encrypt\\=falseで認証をスキップしている。

また、プロパティで設定したhost.docker.internalがLinuxでも有効になるよう、-add-host=host.docker.internal:host-gatewayのオプションを追加している。

パイプラインの実行

パイプラインを最初に流すときにはPermitが必要。

image.png

IP制限をかける ( 2025/05/03 追記 )

IP制限はネットワークから追加できる。 現在接続のIPならクライアントIPアドレスの追加を押し、保存すればOK。
image.png

ただ、この状態だとパイプラインのIPも制限してしまって更新に失敗する。

In addition, setting the corresponding environment variables can avoid inputting credentials in your command. Please use --help to get more information about environment variable usage.
ERROR: 
The request may be blocked by network rules of storage account. Please check network rule set using 'az storage account show -n accountname --query networkRuleSet'.
If you want to change the default action to apply when no rule matches, please use 'az storage account update'.

そのため、パイプラインのIPを追加するように修正する。( 参考: しばやん雑記 - Azure Pipelines の Hosted Agent が持っている Outbound IP アドレスを知りたい)
ネットワーク追加後に10秒sleepをかけているのは、追加直後だとまだIP制限が解除されていないからである。

.azure-devops/db-docs-pipeline.yaml
- task: AzureCLI@2
  inputs:
    azureSubscription: 'azure-dev-hobby'
    scriptType: bash
    scriptLocation: inlineScript
    inlineScript: |
      STORAGE_ACCOUNT_NAME=<作成したStorageAccountの名前>
      IP_ADDRESS=$(curl -s https://ipinfo.io/json | jq -r '.ip')
      echo "Your IP address is: $IP_ADDRESS"

      az storage account network-rule add \
        --account-name $STORAGE_ACCOUNT_NAME \
        --ip-address $IP_ADDRESS
      
      sleep 10

      az storage blob upload-batch \
        --account-name $STORAGE_ACCOUNT_NAME \
        --destination '$web' \
        --destination-path 'dbdocs' \
        --source "$(Pipeline.Workspace)/SchemaSpy" \
        --overwrite

この時点のソース

参考

GitHub ActionsでSchemaSpyのER図を生成してGitHubPagesにデータベースのドキュメントをデプロイできるようにしたメモ
【SchemaSpy】データベース仕様書の作成と公開を自動化してみた【Azure Pipelines】
Bicep を使用して Azure ストレージの静的な Web サイトを有効化する

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?