首页 > 解决方案 > 如何在执行时引用 AWS CodeDeploy 包中的文件?

问题描述

我想在我的 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

标签: windowspowershellaws-code-deploy

解决方案


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

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 Server Amazon EC2 实例 - 添加应用程序规范文件 - https://docs.aws.amazon.com/codedeploy/latest/userguide/tutorials-windows-configure- content.html#tutorials-windows-configure-content-add-appspec-file


推荐阅读