LoginSignup
0
0

More than 5 years have passed since last update.

KuduでGitPushするときに起動するスクリプトを書く

Last updated at Posted at 2018-02-09

AuzreのWebAppsのインスタンスを複数たてて、アプリケーション自体は同じだけどweb.configを変えたいということがあった場合の対処法です。
アプリケーション設定などで定義を変えるということもできますが
config自体を変えたいというときに利用価値があります。

① デプロイスクリプトの元ねたを取得

これらの元ねたは、Kuduが初回Push時に自動生成したものを流用する。
以下からダウンロードできる。
kudu1.png

② デプロイスクリプトの配置

リリースセットを格納しているローカルGITの直下に
.deployment
deploy.cmd
を配置する。

③ Windowsのバッチイメージでやりたい事を記述する

今回は web.config.webappsitename という感じでサイトごとのWeb.configを配置時に切り替えたいというニーズがあったのでいかのような処理を追加してます
それぞれのWebAppがsite01,site02,site03 とあった場合以下のように3種類用意して、それぞれのサーバで動作するとそれぞれ向けに用意したweb.configになる感じです。
web.config.site01
web.config.site02
web.config.site03

web.config

追加する場所は SYNCが終わったタイミングです。


@if "%SCM_TRACE_LEVEL%" NEQ "4" @echo off

:: ----------------------
:: KUDU Deployment Script
:: Version: 1.0.15
:: ----------------------

:: Prerequisites
:: -------------

echo yamas custom deploycmd

:: Verify node.js installed
where node 2>nul >nul
IF %ERRORLEVEL% NEQ 0 (
  echo Missing node.js executable, please install node.js, if already installed make sure it can be reached from current environment.
  goto error
)

:: Setup
:: -----

setlocal enabledelayedexpansion

SET ARTIFACTS=%~dp0%..\artifacts

IF NOT DEFINED DEPLOYMENT_SOURCE (
  SET DEPLOYMENT_SOURCE=%~dp0%.
)

IF NOT DEFINED DEPLOYMENT_TARGET (
  SET DEPLOYMENT_TARGET=%ARTIFACTS%\wwwroot
)

IF NOT DEFINED NEXT_MANIFEST_PATH (
  SET NEXT_MANIFEST_PATH=%ARTIFACTS%\manifest

  IF NOT DEFINED PREVIOUS_MANIFEST_PATH (
    SET PREVIOUS_MANIFEST_PATH=%ARTIFACTS%\manifest
  )
)

IF NOT DEFINED KUDU_SYNC_CMD (
  :: Install kudu sync
  echo Installing Kudu Sync
  call npm install kudusync -g --silent
  IF !ERRORLEVEL! NEQ 0 goto error

  :: Locally just running "kuduSync" would also work
  SET KUDU_SYNC_CMD=%appdata%\npm\kuduSync.cmd
)

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Deployment
:: ----------

echo Handling Basic Web Site deployment.

:: 1. KuduSync
IF /I "%IN_PLACE_DEPLOYMENT%" NEQ "1" (
  call :ExecuteCmd "%KUDU_SYNC_CMD%" -v 50 -f "%DEPLOYMENT_SOURCE%" -t "%DEPLOYMENT_TARGET%" -n "%NEXT_MANIFEST_PATH%" -p "%PREVIOUS_MANIFEST_PATH%" -i ".git;.hg;.deployment;deploy.cmd"
  IF !ERRORLEVEL! NEQ 0 goto error

)
::::: 追加ここから
echo replace web.config.sitename to web.config
  call move %DEPLOYMENT_TARGET%\web.config.%WEBSITE_SITE_NAME% %DEPLOYMENT_TARGET%\web.config
echo add read only web.config
  call attrib +R %DEPLOYMENT_TARGET%\web.config
echo remove another web.config
  call del %DEPLOYMENT_TARGET%\web.config*
echo remove read only web.config
  call attrib -R %DEPLOYMENT_TARGET%\web.config

call dir %DEPLOYMENT_TARGET%
::::ここまで::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
goto end

:: Execute command routine that will echo out when error
:ExecuteCmd
setlocal
set _CMD_=%*
call %_CMD_%
if "%ERRORLEVEL%" NEQ "0" echo Failed exitCode=%ERRORLEVEL%, command=%_CMD_%
exit /b %ERRORLEVEL%

:error
endlocal
echo An error has occurred during web site deployment.
call :exitSetErrorLevel
call :exitFromFunction 2>nul

:exitSetErrorLevel
exit /b 1

:exitFromFunction
()

:end
endlocal
echo Finished successfully.

④ PUSHする。
実際動作した際の出力としては以下のような感じです。
(追加したところ部分)


remote: replace web.config.sitename to web.config
remote:         1 file(s) moved.
remote: add read only web.config
remote: remove another web.config
remote: Access is denied.
remote: D:\home\site\wwwroot\web.config
remote: remove read only web.config
remote:  Volume in drive D is Windows
remote:  Volume Serial Number is 7E3A-AA6C
remote:
remote:  Directory of D:\home\site\wwwroot
remote:
remote: 09/21/2017  07:55 AM    <DIR>          .
remote: 09/21/2017  07:55 AM    <DIR>          ..
remote: 09/21/2017  07:35 AM             2,594 default.html
remote: 09/21/2017  07:26 AM               305 error.html
remote: 09/21/2017  07:26 AM    <DIR>          img
remote: 09/21/2017  07:26 AM    <DIR>          maintain
remote: 09/21/2017  07:55 AM                 0 web.config
remote:                3 File(s)          2,899 bytes
remote:                4 Dir(s)     980,099,072 bytes free
remote: Finished successfully.

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