构建部署脚本的最佳方式?
Best way to structure deployment script?
我有一个 PowerShell 脚本来部署一些数据库代码。这是一个 SSDT 项目,其结构如下:
Root
\Deployment
\Scripts
Deploy.ps1
\Config
deploy.config
\SolutionRoot
\DatabaseProject
\UnitTestProject
为了在我的工作站上进行测试部署,我更改为 \Deployment\Scripts
和 运行 Deploy.ps1
,它工作正常。当我从我的 Jenkins 构建服务器 运行 它时,当前工作目录是根文件夹(不是脚本)。因此,Deploy.ps1
脚本在调用时无法 运行:powershell -File Deployment\Scripts\Deploy.ps1
例如,我在脚本顶部附近有这段代码作为调试:
$currentDirectory = (Get-Item -Path ".\" -Verbose).FullName
Write-Host "Current Working Directory: $currentDirectory"
当 运行 在我的工作站上时 returns:
Current Working Directory: C:\repos\MyRoot\Deployment\Scripts
当 运行 在构建服务器上 returns
Current Working Directory: D:\Jenkins\workspace\MyRootWithADifferentName
我希望脚本 运行 无论它在哪里,那么知道我在目录结构中行踪的最好方法是什么?构建服务器可能会有所不同,Root 目录的名称也可能会有所不同,但所有其他目录相对于 Root 都是静态的。
使用这个来终止当前脚本的路径:
$currentDirectory = split-path -parent $MyInvocation.MyCommand.Definition
我有一个 PowerShell 脚本来部署一些数据库代码。这是一个 SSDT 项目,其结构如下:
Root
\Deployment
\Scripts
Deploy.ps1
\Config
deploy.config
\SolutionRoot
\DatabaseProject
\UnitTestProject
为了在我的工作站上进行测试部署,我更改为 \Deployment\Scripts
和 运行 Deploy.ps1
,它工作正常。当我从我的 Jenkins 构建服务器 运行 它时,当前工作目录是根文件夹(不是脚本)。因此,Deploy.ps1
脚本在调用时无法 运行:powershell -File Deployment\Scripts\Deploy.ps1
例如,我在脚本顶部附近有这段代码作为调试:
$currentDirectory = (Get-Item -Path ".\" -Verbose).FullName
Write-Host "Current Working Directory: $currentDirectory"
当 运行 在我的工作站上时 returns:
Current Working Directory: C:\repos\MyRoot\Deployment\Scripts
当 运行 在构建服务器上 returns
Current Working Directory: D:\Jenkins\workspace\MyRootWithADifferentName
我希望脚本 运行 无论它在哪里,那么知道我在目录结构中行踪的最好方法是什么?构建服务器可能会有所不同,Root 目录的名称也可能会有所不同,但所有其他目录相对于 Root 都是静态的。
使用这个来终止当前脚本的路径:
$currentDirectory = split-path -parent $MyInvocation.MyCommand.Definition