0
votes

I need to deploy a Web API service on an Amazon EC2 instance. I have configured AWS CodeDeploy to download sources from the GitHub repository and put them on the target EC2. I have installed CodeDeploy Agent on the server with Windows Server 2012. Moreover, after the sources are successfully downloaded I am able to build the project and publish, if I do if manually in PowerShell console (run BuildApp.ps1).

What I need to do is to accomplish the project building and publishing through PowerShell scripts running automatically during the CodeDeploy agent work.

I have configured the appspec.yml file which has the following content:

version: 0.0
os: windows
files:
  - source: /My-Project/
    destination: C:\inetpub\my-project-dev\src
hooks:
  AfterInstall:
    - location: .\BuildApp.ps1

The BuildApp.ps1 has the following content:

#Restore Nuget packages nuget install C:\inetpub\my-project-dev\src\RestApi\packages.config -OutputDirectory .\packages

Set-Location C:\inetpub\my-project-dev\src\RestApi

#Build the project msbuild RestApi.csproj /p:Configuration=Release /p:OutputPath="publish"

However, if I perform a publish using Amazon CodeDeploy console the log files reveal the error as below:

nuget : The term 'nuget' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At C:\ProgramData\Amazon\CodeDeploy\ca6544b1-d2cb-4a81-86c0-68ae0f60764b\d-A2V2JCQ3G\deployment-archive\BuildApp.ps1:3 char:1
nuget install C:\inetpub\my-project-dev\src\RestApi\packages.config

msbuild : The term 'msbuild' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At C:\ProgramData\Amazon\CodeDeploy\ca6544b1-d2cb-4a81-86c0-68ae0f60764b\d-A2V2JCQ3G\deployment-archive\BuildApp.ps1:8 char:1
msbuild RestApi.csproj /p:Configuration=Release /p:OutputPath="publish"

What causes PowerShell not to find nuget and msbuild command?

1
Are you sure that script works when run manually outside of CodeDeploy?Rodrigo M

1 Answers

1
votes

The problem was in attempting to call 64-bit version of PowerShell commands (msbuild) whilst the Amazon CodeDeploy Windows utility is a 32-bit program and it executes 32-bit msbuild command. Therefore, I had to specify an absolute path to the msbuild.exe.

C:\Windows\sysnative\WindowsPowerShell\v1.0\powershell.exe -Command {&"C:\Program Files (x86)\MSBuild\14.0\Bin\msbuild.exe" MyProject.csproj /p:Configuration=Release /p:OutputPath=deployment}