概要
前回はGitHub ActionsでDBドキュメントをデプロイしたので、今回はAzure DevOps Pipeline でStorage Accountに公開する。
環境
- Windows 11
- azure-cli 2.71.0
StorageAccount準備
参考記事 ではIP制限しているが、ここでは行わない。Bicepで作成する。
#!/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
@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を確認にはひと手間必要。
下記のようなURLが取得できる。
https://download.microsoft.com/download/745da347-bd51-4bf0-a84e-3465608d8856/jpn/sqljdbc_12.10.0.0_jpn.tar.gz
SpySchema設定ファイル
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
パイプライン
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が必要。
IP制限をかける ( 2025/05/03 追記 )
IP制限はネットワークから追加できる。 現在接続のIPならクライアントIPアドレスの追加を押し、保存すればOK。
ただ、この状態だとパイプラインの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制限が解除されていないからである。
- 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 サイトを有効化する