使用CodeDeploy部署到EC2实例

时间:2019-10-20 22:54:12

标签: powershell asp.net-core web-deployment windows-server-2016 aws-code-deploy

我在AWS上有一个基于Windows Server 2016的AMI,我正在将其部署到CodeDeploy中。我正在使用CLI将来自github的核心asp.net网站部署到此EC2实例中。

aws deploy create-deployment --application-name my-App --deployment-config-name CodeDeployDefault.OneAtATime --deployment-group-name App-DepGrp --description "My GitHub deployment demo" --github-location repository=xxx-yyy/aws,commitId=d2c7cfcf90sfsfd2c7cfcfgs9e1e0f50eshshsgsgd2c7cfcfd2c7cgbsfgsbg

部署开始,appspec调用Install.ps1文件,该文件随即在Powershell脚本文件的file命令中创建并创建该文件夹。网站未创建。应用程序池也不会创建。 这是我的Appspec.yml文件的样子:

version: 0.0
os: windows
files:
  - source: \
    destination: c:\website-dropfolder
hooks:
 ApplicationStart:
    - location: .\Install.ps1
      timeout: 300
      runas: Administrator

这是Install.ps1

# Create folder to publish binaries from drop folder
mkdir c:\website-published

# Switch to drop folder
Set-Location C:\website-dropfolder   

# Restore the nuget references
& "C:\Program Files\dotnet\dotnet.exe" restore  

# Publish application with all of its dependencies and runtime for IIS to use
& "C:\Program Files\dotnet\dotnet.exe" publish --configuration release -o c:\website-published --runtime active  

# Create an IIS website and point it to the published folder
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command {Import-Module WebAdministration; New-WebSite -Name CoreWebsite -Port 80 -HostHeader CoreWebsite -PhysicalPath "$env:systemdrive\website-published"}


# Create AppPool for website and set CLR version to core-dotnet
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command {Import-Module WebAdministration; New-WebAppPool CoreWebsiteAppPool}

C:\ Windows \ System32 \ WindowsPowerShell \ v1.0 \ powershell.exe-命令{导入模块WebAdministration; Set-ItemProperty IIS:\ AppPools \ CoreWebsiteAppPoolmanagedRuntimeVersion“” -verbose}

我已将-ErrorAction SilentlyContinue放在dotnet恢复或发布有任何问题的情况下。是什么导致网站无法创建?我在Windows 10开发人员计算机上运行了相同的脚本,并使用应用程序池创建了相同的网站,甚至设置了其CLR版本。在Windows Server 2016上不一样吗?

编辑

我查看了用户数据日志,似乎从以下行开始出错:New-WebSite -Name CoreWebsite -Port 80 -HostHeader CoreWebsite -PhysicalPath "$env:systemdrive\website-published" -ErrorAction SilentlyContinue

错误是:

  

System.Management.Automation.PSCustomObjectSystem.Object1为第一个准备模块   use.0-1-1Completed-1   New-WebSite:无法检索   cmdlet的动态参数。 _x000D__x000A_为具有CLSID的组件检索COM类工厂   _x000D__x000A_ {688EEEE5-6A7E-422F-B2E1-6AF00DC944A6}由于以下错误而失败:_x000D__x000A_80040154类未注册(HRESULT的异常:   0x80040154 _x000D__x000A_(REGDB_E_CLASSNOTREG))._ x000D__x000A_At   行:1 char:34_x000D__x000A_ + ...   新网站-名称CoreWebsite-端口80 -HostHeader CoreWe   ..._ x000D__x000A _ +

当codedeploy代理调用powershell命令时,甚至认为该命令以管理员身份运行时,powershell命令似乎都不起作用,但是当我在powershell提示符上手动运行脚本文件时,该脚本可以按预期运行。我什至指定要仅调用powershell可执行文件的路径,以使其不使用该路径。 在C:\Windows\SysWOW64文件夹中。即使这样也不起作用。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

从共享的错误消息中:

  

“ HRESULT的异常:0x80040154   _x000D__x000A_(REGDB_E_CLASSNOTREG)“

...在我看来,CodeDeploy正在32位而不是64位模式下运行PS脚本。这是AWS文档[1]中经常提到的问题。

是否可以按照[1]中的建议修改PowerShell脚本,如下所示:

# 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
}

# Was restart successful?
Write-Warning "Hello from $PSHOME"
Write-Warning "  (\SysWOW64\ = 32-bit mode, \System32\ = 64-bit mode)"
Write-Warning "Original arguments (if any): $args"

# Your 64-bit script code follows here...
# ...

参考:

[0] http://kino505.blogspot.com/2016/12/aws-codedeploy-windows-3264-bit.html

[1]对EC2 /本地部署问题进行故障排除-Windows PowerShell脚本默认情况下无法使用64位版本的Windows PowerShell-https://docs.aws.amazon.com/codedeploy/latest/userguide/troubleshooting-deployments.html#troubleshooting-deployments-powershell