【问题标题】:Using CodeDeploy to deploy to EC2 instance使用 CodeDeploy 部署到 EC2 实例
【发布时间】:2020-02-17 01:12:33
【问题描述】:

我在 AWS 上有一个基于 Windows Server 2016 的 AMI,我正在筹集它以供 CodeDeploy 部署到其中。我正在使用 CLI 将核心 asp.net 网站从 github 部署到此 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 -Command {Import-Module WebAdministration; Set-ItemProperty IIS:\AppPools\CoreWebsiteAppPool managedRuntimeVersion "" -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首先准备模块 使用.0-1-1Completed-1 新网站:无法检索 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 字符:34_x000D__x000A_+ ... 硝化; New-WebSite -Name CoreWebsite -Port 80 -HostHeader CoreWe ..._x000D__x000A_+

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

【问题讨论】:

  • 最好删除“SilentlyContinue”并检查“C:\ProgramData\Amazon\CodeDeploy\\\logs\scripts.log”下的日志 -他们可能会指出脚本出了什么问题。
  • @shariqmaws:按照你说的做后,我用更多信息更新了这个问题。

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


【解决方案1】:

来自共享的错误消息:

“来自 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

【讨论】:

    猜你喜欢
    • 2021-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-23
    • 2015-02-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多