Best way to structure deployment script?

时间:2016-10-15 17:27:40

标签: powershell relative-path

I have a PowerShell script to deploy some database code. It is a SSDT project and it is structured thus:

Root
    \Deployment
        \Scripts
            Deploy.ps1
        \Config
            deploy.config
    \SolutionRoot
        \DatabaseProject
        \UnitTestProject

To do a test deployment on my workstation I change to \Deployment\Scripts and run Deploy.ps1 and it works fine. When I run it from my Jenkins build server, the current working directory is the root folder (not scripts). So the Deploy.ps1 scripts fails to run when invoked like: powershell -File Deployment\Scripts\Deploy.ps1

For example I have this code near the top of the script as debug:

$currentDirectory = (Get-Item -Path ".\" -Verbose).FullName
Write-Host "Current Working Directory: $currentDirectory"

and when run on my workstation it returns:

Current Working Directory: C:\repos\MyRoot\Deployment\Scripts

when run on the build server it returns

Current Working Directory: D:\Jenkins\workspace\MyRootWithADifferentName

I would like the script to run no matter where it is, so what's the best way to know whereabouts in the directory structure I am? Build servers may vary and the name of the Root directory may vary too, but all the others are static relative to Root.

1 个答案:

答案 0 :(得分:1)

使用此术语作为当前脚本的路径:

$currentDirectory = split-path -parent $MyInvocation.MyCommand.Definition