如何在执行时引用 AWS CodeDeploy 包中的文件?

How to reference a file in an AWS CodeDeploy package at execution?

我想在我的 AWS CodeDeploy 包中包含一个数据库升级脚本,但我不知道如何指定它的路径。

我成功触发了 PowerShell 脚本,我有一个调用 sqlcmd.exe 的脚本,我想将 sqlcmd 脚本传递给 运行。

当我使用 Get-Location 打印出当前工作目录时,它显示 C:\Windows\system32。因此,我能够引用它的唯一方法似乎是知道当前的包路径。

有没有办法确定部署脚本中当前的包路径?

编辑 - 我的设置

version: 0.0
os: windows
files:
  - source: .\source
    destination: C:\inetpub\gentest.vanhookservice.com\dev
hooks:
    BeforeInstall:
        - location: .\scripts\db-backup-test.ps1
          timeout: 6000
        - location: .\scripts\code-backup-gentest.ps1
          timeout: 6000
          runas: Administrator
        - location: .\scripts\db-upgrade-test.ps1
          timeout: 6000

db-upgrade-test.ps1:

# Fail on all errors
$ErrorActionPreference = 'Stop'

# Are you running in 32-bit mode?
#   (\SysWOW64\ = 32-bit mode)

if ($PSHOME -like "*SysWOW64*")
{
  Write-Warning "Restarting this script under 64-bit Windows PowerShell."

  # Restart this script under 64-bit Windows PowerShell.
  #   (\SysNative\ redirects to \System32\ for 64-bit mode)

  & (Join-Path ($PSHOME -replace "SysWOW64", "SysNative") powershell.exe) -File `
    (Join-Path $PSScriptRoot $MyInvocation.MyCommand) @args

  # Exit 32-bit script.

  Exit $LastExitCode
}

echo "Upgrading test database..."
sqlcmd.exe -S vhs-awsdev-sql1 -d JobManagerDev -i ..\database\upgrade.sql
# Test if the sqlcmd.exe command exited with a success or error condition
if ($? -eq "False")
{
   throw "sqlcmd.exe failed with exit code $LastExitCode"
}
echo "Test database upgrade complete." 

问题是 ..\database\upgrade.sql 引用。我也尝试了 .\database\upgrade.sql,因为这就是 appspec.yml 中引用脚本的方式,但真正的问题是当前工作目录实际上是 C:\Windows\system32

只需将脚本保存在源代码的根目录中并指定如下:

version: 0.0
os: windows
files:
  - source: \index.html
    destination: c:\inetpub\wwwroot
hooks:
  BeforeInstall:
    - location: \before-install.bat        # <====== script in source dir root
      timeout: 900

详情在这里:

[1] 第 2 步:配置您的源内容以部署到 Windows 服务器 Amazon EC2 实例 - 添加应用程序规范文件 - https://docs.aws.amazon.com/codedeploy/latest/userguide/tutorials-windows-configure-content.html#tutorials-windows-configure-content-add-appspec-file

64位逻辑中使用的$PSScriptRoot就是我想要的

当前的数据库升级脚本是这样的:

# Fail on all errors
$ErrorActionPreference = 'Stop'

# Are you running in 32-bit mode?
#   (\SysWOW64\ = 32-bit mode)

if ($PSHOME -like "*SysWOW64*")
{
  Write-Warning "Restarting this script under 64-bit Windows PowerShell."

  # Restart this script under 64-bit Windows PowerShell.
  #   (\SysNative\ redirects to \System32\ for 64-bit mode)

  & (Join-Path ($PSHOME -replace "SysWOW64", "SysNative") powershell.exe) -File `
    (Join-Path $PSScriptRoot $MyInvocation.MyCommand) @args

  # Exit 32-bit script.

  Exit $LastExitCode
}

echo "Upgrading production database..."
echo "PSScriptRoot: $PSScriptRoot"
sqlcmd.exe -S vhs-sql1 -d JobManager -i "$PSScriptRoot\..\database\upgrade.sql"
# Test if the sqlcmd.exe command exited with a success or error condition
if ($LastExitCode -ne 0)
{
    throw "sqlcmd.exe failed to run, exiting with the code $LastExitCode"
}
echo "Production database upgrade complete."

注意更新后的 sqlcmd.exe 行