Azure DevOps 发布管道 Powershell 脚本 MSDeploy 错误

时间:2021-04-19 18:31:09

标签: azure powershell azure-pipelines azure-pipelines-release-pipeline msdeploy

我在 Azure DevOps 中有一个 .NET Web 解决方案。我正在尝试为我的开发子域设置 CI。到目前为止,我已经按照 this article 设置了似乎正在运行的构建管道。

因此,我按照 part 2 of that article 设置发布管道。这让我将以下内容放入我的 PowerShell 脚本 MSDeploy 任务中:

_MYSITE-ASP.NET-CI-Try2\drop\MYSITE.deploy.cmd /Y /M:hardcoded /U:MYUSERNAME-001 /P:$(smarteraspPassword) /G:False /A:Basic -allowUntrusted -enableRule:AppOffline

(适当替换 MYUSERNAME 和 MYSITE)

作为参考,MYSITE.deploy.cmd 文件很长,但以这个开头:

@rem ---------------------------------------------------------------------------------
@rem Copyright 2008 Microsoft Corporation. All rights reserved.
@rem This is provided as sample to deploy the package using msdeploy.exe
@rem For information about IIS Web Deploy technology,
@rem please visit https://go.microsoft.com/?linkid=9278654
@rem Note: This batch file assumes the package and setparametsrs.xml are in the same folder with this file
@rem ---------------------------------------------------------------------------------
@if %_echo%!==! echo off
setlocal
@rem ---------------------------------------------------------------------------------
@rem Please Make sure you have Web Deploy install in your machine. 
@rem Alternatively, you can explicit set the MsDeployPath to the location it is on your machine
@rem set MSDeployPath="C:\Program Files (x86)\IIS\Microsoft Web Deploy V3\"
@rem ---------------------------------------------------------------------------------
                      
@rem ---------------------------------------------------------------------------------
@rem if user does not set MsDeployPath environment variable, we will try to retrieve it from registry.
@rem ---------------------------------------------------------------------------------
if "%MSDeployPath%" == "" (
for /F "usebackq tokens=1,2,*" %%h  in (`reg query "HKLM\SOFTWARE\Microsoft\IIS Extensions\MSDeploy" /s  ^| findstr -i "InstallPath"`) do (
if /I "%%h" == "InstallPath" ( 
if /I "%%i" == "REG_SZ" ( 
if not "%%j" == "" ( 
if "%%~dpj" == "%%j" ( 
set MSDeployPath=%%j
))))))

@rem ------------------------------------------

@rem ------------------------------------------

                      
if not exist "%MSDeployPath%msdeploy.exe" (
echo. msdeploy.exe is not found on this machine. Please install Web Deploy before execute the script. 
echo. Please visit https://go.microsoft.com/?linkid=9278654
goto :usage
)

set RootPath=%~dp0
if /I "%_DeploySetParametersFile%" == "" (
set _DeploySetParametersFile=%RootPath%MYSITE.SetParameters.xml
)

@rem ------------------------------------------

@rem ------------------------------------------

                      
set _ArgTestDeploy=
set _ArgDestinationType=auto
set _ArgComputerNameWithQuote=""
set _ArgUserNameWithQuote=""
set _ArgPasswordWithQuote=""
set _ArgEncryptPasswordWithQuote=""
set _ArgIncludeAclsWithQuote="False"
set _ArgAuthTypeWithQuote=""
set _ArgtempAgentWithQuote=""
set _ArgLocalIIS=
set _ArgLocalIISVersion=
set _HaveArgMSDeployAdditonalFlags=
                      
                      
@rem ---------------------------------------------------------------------------------
@rem Simple Parse the arguments
@rem ---------------------------------------------------------------------------------
:NextArgument
set _ArgCurrent=%~1
set _ArgFlagFirst=%_ArgCurrent:~0,1%
set _ArgFlag=%_ArgCurrent:~0,3%
set _ArgValue=%_ArgCurrent:~3%

if /I "%_ArgFlag%" == "" goto :GetStarted
if /I "%_ArgFlag%" == "~0,3" goto :GetStarted
if /I "%_ArgFlag%" == "/T" set _ArgTestDeploy=true&goto :ArgumentOK
if /I "%_ArgFlag%" == "/Y" set _ArgTestDeploy=false&goto :ArgumentOK
if /I "%_ArgFlag%" == "/L" set _ArgLocalIIS=true&goto :ArgumentOK

if /I "%_ArgFlag%" == "/M:" set _ArgComputerNameWithQuote="%_ArgValue%"&goto :ArgumentOK
if /I "%_ArgFlag%" == "/U:" set _ArgUserNameWithQuote="%_ArgValue%"&goto :ArgumentOK
if /I "%_ArgFlag%" == "/P:" set _ArgPasswordWithQuote="%_ArgValue%"&goto :ArgumentOK
if /I "%_ArgFlag%" == "/E:" set _ArgEncryptPasswordWithQuote="%_ArgValue%"&goto :ArgumentOK
if /I "%_ArgFlag%" == "/I:" set _ArgIncludeAclsWithQuote="%_ArgValue%"&goto :ArgumentOK
if /I "%_ArgFlag%" == "/A:" set _ArgAuthTypeWithQuote="%_ArgValue%"&goto :ArgumentOK
if /I "%_ArgFlag%" == "/G:" set _ArgtempAgentWithQuote="%_ArgValue%"&goto :ArgumentOK

@rem Any addition flags, pass through to the msdeploy
if "%_HaveArgMSDeployAdditonalFlags%" == "" (
goto :Assign_ArgMsDeployAdditionalFlags
)
set _ArgMsDeployAdditionalFlags=%_ArgMsDeployAdditionalFlags:&=^&% %_ArgCurrent:&=^&%
set _HaveArgMSDeployAdditonalFlags=1
goto :ArgumentOK


:Assign_ArgMsDeployAdditionalFlags
set _ArgMsDeployAdditionalFlags=%_ArgCurrent:&=^&%
set _HaveArgMSDeployAdditonalFlags=1
goto :ArgumentOK

:ArgumentOK
shift
goto :NextArgument

:GetStarted
@rem ------------------------------------------

@rem ------------------------------------------
if /I "%_ArgTestDeploy%" == "" goto :usage
if /I "%_ArgDestinationType%" == ""  goto :usage

set _Destination=%_ArgDestinationType%
if not %_ArgComputerNameWithQuote% == "" set _Destination=%_Destination%,computerName=%_ArgComputerNameWithQuote%
if not %_ArgUserNameWithQuote% == "" set _Destination=%_Destination%,userName=%_ArgUserNameWithQuote%
if not %_ArgPasswordWithQuote% == "" set _Destination=%_Destination%,password=%_ArgPasswordWithQuote%
if not %_ArgAuthTypeWithQuote% == "" set _Destination=%_Destination%,authtype=%_ArgAuthTypeWithQuote%
if not %_ArgEncryptPasswordWithQuote% == "" set _Destination=%_Destination%,encryptPassword=%_ArgEncryptPasswordWithQuote%
if not %_ArgIncludeAclsWithQuote% == "" set _Destination=%_Destination%,includeAcls=%_ArgIncludeAclsWithQuote%
if not %_ArgtempAgentWithQuote% == "" set _Destination=%_Destination%,tempAgent=%_ArgtempAgentWithQuote%

@rem ------------------------------------------

@rem ------------------------------------------

                      
@rem ---------------------------------------------------------------------------------
@rem add -whatif when -T is specified                      
@rem ---------------------------------------------------------------------------------
if /I "%_ArgTestDeploy%" NEQ "false" (
set _MsDeployAdditionalFlags=-whatif %_MsDeployAdditionalFlags%
)

@rem ------------------------------------------

@rem ------------------------------------------

@rem ---------------------------------------------------------------------------------
@rem add flags for IISExpress when -L is specified                      
@rem ---------------------------------------------------------------------------------

if /I "%_ArgLocalIIS%" == "true" (
call :SetIISExpressArguments
)
if /I "%_ArgLocalIIS%" == "true" (
if not exist "%IISExpressPath%%IISExpressManifest%" (
echo. IISExpress is not found on this machine. Please install through Web Platform Installer before execute the script. 
echo. or remove /L flag
echo. Please visit https://go.microsoft.com/?linkid=9278654
goto :usage
)
if not exist "%IISExpressUserProfileDirectory%" (
echo. %IISExpressUserProfileDirectory% is not exists
echo. IISExpress is found on the machine. But the user have run IISExpress at least once.
echo. Please visit https://go.microsoft.com/?linkid=9278654 for detail
goto :usage
)
                      
set _MsDeployAdditionalFlags=%_MsDeployAdditionalFlags% -appHostConfigDir:%IISExpressUserProfileDirectory% -WebServerDir:"%IISExpressPath%" -webServerManifest:"%IISExpressManifest%"
)

@rem ---------------------------------------------------------------------------------
@rem check the existence of the package file
@rem ---------------------------------------------------------------------------------
if not exist "%RootPath%MYSITE.zip" (
echo "%RootPath%MYSITE.zip" does not exist. 
echo This batch file relies on this deploy source file^(s^) in the same folder.
goto :usage
)

@rem ---------------------------------------------

@rem ---------------------------------------------

@rem ---------------------------------------------------------------------------------
@rem Execute msdeploy.exe command line
@rem ---------------------------------------------------------------------------------
call :CheckParameterFile
echo. Start executing msdeploy.exe
echo -------------------------------------------------------
if  not exist "%_DeploySetParametersFile%" (
set _MSDeployCommandline="%MSDeployPath%msdeploy.exe" -source:package='%RootPath%MYSITE.zip' -dest:%_Destination% -verb:sync -disableLink:AppPoolExtension -disableLink:ContentExtension -disableLink:CertificateExtension
) else (
set _MSDeployCommandline="%MSDeployPath%msdeploy.exe" -source:package='%RootPath%MYSITE.zip' -dest:%_Destination% -verb:sync -disableLink:AppPoolExtension -disableLink:ContentExtension -disableLink:CertificateExtension -setParamFile:"%_DeploySetParametersFile%"
)

if "%_HaveArgMSDeployAdditonalFlags%" == "" (
goto :MSDeployWithOutArgMsDeployAdditionalFlag
) 
goto :MSDeployWithArgMsDeployAdditionalFlag
goto :eof

但是任务每次都失败并显示此输出(使用调试信息更新):

2021-04-20T19:07:28.6148827Z ##[debug]Evaluating condition for step: 'PowerShell Script MSDeploy'
2021-04-20T19:07:28.6150730Z ##[debug]Evaluating: succeeded()
2021-04-20T19:07:28.6151232Z ##[debug]Evaluating succeeded:
2021-04-20T19:07:28.6152462Z ##[debug]=> True
2021-04-20T19:07:28.6152995Z ##[debug]Result: True
2021-04-20T19:07:28.6153704Z ##[section]Starting: PowerShell Script MSDeploy
2021-04-20T19:07:28.6272897Z ==============================================================================
2021-04-20T19:07:28.6273258Z Task         : PowerShell
2021-04-20T19:07:28.6273535Z Description  : Run a PowerShell script on Linux, macOS, or Windows
2021-04-20T19:07:28.6273811Z Version      : 2.180.1
2021-04-20T19:07:28.6274061Z Author       : Microsoft Corporation
2021-04-20T19:07:28.6274475Z Help         : https://docs.microsoft.com/azure/devops/pipelines/tasks/utility/powershell
2021-04-20T19:07:28.6274845Z ==============================================================================
2021-04-20T19:07:31.1490744Z ##[debug]VstsTaskSdk 0.11.0 commit 7ff27a3e0bdd6f7b06690ae5f5b63cb84d0f23f4
2021-04-20T19:07:31.2797508Z ##[debug]Entering D:\a\_tasks\PowerShell_e213ff0f-5d5c-4791-802d-52ea3e7be1f1\2.180.1\powershell.ps1.
2021-04-20T19:07:31.2897797Z ##[debug]Loading resource strings from: D:\a\_tasks\PowerShell_e213ff0f-5d5c-4791-802d-52ea3e7be1f1\2.180.1\task.json
2021-04-20T19:07:31.3295159Z ##[debug]Loaded 11 strings.
2021-04-20T19:07:31.3297151Z ##[debug]SYSTEM_CULTURE: 'en-US'
2021-04-20T19:07:31.3302500Z ##[debug]Loading resource strings from: D:\a\_tasks\PowerShell_e213ff0f-5d5c-4791-802d-52ea3e7be1f1\2.180.1\Strings\resources.resjson\en-US\resources.resjson
2021-04-20T19:07:31.3558737Z ##[debug]Loaded 11 strings.
2021-04-20T19:07:31.3818248Z ##[debug]INPUT_ERRORACTIONPREFERENCE: 'continue'
2021-04-20T19:07:31.3892919Z ##[debug]INPUT_SHOWWARNINGS: 'false'
2021-04-20T19:07:31.3894971Z ##[debug] Converted to bool: False
2021-04-20T19:07:31.3920563Z ##[debug]INPUT_FAILONSTDERR: 'false'
2021-04-20T19:07:31.3938665Z ##[debug] Converted to bool: False
2021-04-20T19:07:31.3964198Z ##[debug]INPUT_IGNORELASTEXITCODE: 'false'
2021-04-20T19:07:31.3981567Z ##[debug] Converted to bool: False
2021-04-20T19:07:31.4006233Z ##[debug]INPUT_PWSH: 'false'
2021-04-20T19:07:31.4023771Z ##[debug] Converted to bool: False
2021-04-20T19:07:31.4047897Z ##[debug]INPUT_WORKINGDIRECTORY: 'D:\a\r1\a'
2021-04-20T19:07:31.4197124Z ##[debug]Asserting container path exists: 'D:\a\r1\a'
2021-04-20T19:07:31.4223108Z ##[debug]INPUT_TARGETTYPE: 'inline'
2021-04-20T19:07:31.4258556Z ##[debug]INPUT_SCRIPT: '_MYSITE-ASP.NET-CI-Try2\drop\MYSITE.deploy.cmd /Y /M:hardcoded /U:MYUSERNAME-001 /P:*** /G:False /A:Basic -allowUntrusted -enableRule:AppOffline'
2021-04-20T19:07:31.4326516Z Generating script.
2021-04-20T19:07:31.4532300Z ##[debug]AGENT_VERSION: '2.185.1'
2021-04-20T19:07:31.4621404Z ##[debug]AGENT_TEMPDIRECTORY: 'D:\a\_temp'
2021-04-20T19:07:31.4643517Z ##[debug]Asserting container path exists: 'D:\a\_temp'
2021-04-20T19:07:31.5057854Z ##[debug]Asserting leaf path exists: 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe'
2021-04-20T19:07:31.5069825Z ========================== Starting Command Output ===========================
2021-04-20T19:07:31.5289411Z ##[debug]Entering Invoke-VstsTool.
2021-04-20T19:07:31.5414063Z ##[debug] Arguments: '-NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command ". 'D:\a\_temp\b215ff73-d7c3-450f-8d2e-ba084962fdad.ps1'"'
2021-04-20T19:07:31.5432905Z ##[debug] FileName: 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe'
2021-04-20T19:07:31.5451721Z ##[debug] WorkingDirectory: 'D:\a\r1\a'
2021-04-20T19:07:31.5519638Z ##[command]"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command ". 'D:\a\_temp\b215ff73-d7c3-450f-8d2e-ba084962fdad.ps1'"
2021-04-20T19:07:32.8288668Z '@rem' is not recognized as an internal or external command,
2021-04-20T19:07:32.8289010Z 
2021-04-20T19:07:32.8303028Z operable program or batch file.
2021-04-20T19:07:32.8304928Z D:\a\r1\a>@rem --------------------------------------------------------------------------------- 
2021-04-20T19:07:32.9412552Z SetParameters from:
2021-04-20T19:07:32.9413166Z "D:\a\r1\a\_MYSITE-ASP.NET-CI-Try2\drop\MYSITE.SetParameters.xml"
2021-04-20T19:07:32.9414351Z You can change IIS Application Name, Physical path, connectionString
2021-04-20T19:07:32.9414841Z or other deploy parameters in the above file.
2021-04-20T19:07:32.9415646Z -------------------------------------------------------
2021-04-20T19:07:32.9416057Z  Start executing msdeploy.exe
2021-04-20T19:07:32.9416816Z -------------------------------------------------------
2021-04-20T19:07:32.9418653Z  "C:\Program Files\IIS\Microsoft Web Deploy V3\msdeploy.exe" -source:package='D:\a\r1\a\_MYSITE-ASP.NET-CI-Try2\drop\MYSITE.zip' -dest:auto,computerName="https://dev.MYSITE.com:8172/MsDeploy.axd?site=MYUSERNAME-001-subsite4",userName="MYUSERNAME-001",password="$(smarteraspnetPassword)",authtype="Basic",includeAcls="False",tempAgent="False" -verb:sync -disableLink:AppPoolExtension -disableLink:ContentExtension -disableLink:CertificateExtension -setParamFile:"D:\a\r1\a\_MYSITE-ASP.NET-CI-Try2\drop\MYSITE.SetParameters.xml"  -allowUntrusted -enableRule:AppOffline
2021-04-20T19:07:34.8012263Z Info: Using ID 'AGUIDWASHERE' for connections to the remote server.
2021-04-20T19:07:35.5683414Z Error Code: ERROR_USER_UNAUTHORIZED
2021-04-20T19:07:35.5685211Z More Information: Connected to the remote computer ("dev.MYSITE.com") using the Web Management Service, but could not authorize. Make sure that you are using the correct user name and password, that the site you are connecting to exists, and that the credentials represent a user who has permissions to access the site.  Learn more at: http://go.microsoft.com/fwlink/?LinkId=221672#ERROR_USER_UNAUTHORIZED.
2021-04-20T19:07:35.5686550Z Error: The remote server returned an error: (401) Unauthorized.
2021-04-20T19:07:35.5687340Z Error count: 1.
2021-04-20T19:07:36.4391584Z ##[debug]$LASTEXITCODE: 1
2021-04-20T19:07:36.5105601Z ##[debug]Exit code: 1
2021-04-20T19:07:36.5166560Z ##[debug]Leaving Invoke-VstsTool.
2021-04-20T19:07:36.5599203Z ##[error]PowerShell exited with code '1'.
2021-04-20T19:07:36.5609057Z ##[debug]Processed: ##vso[task.logissue type=error]PowerShell exited with code '1'.
2021-04-20T19:07:36.5636517Z ##[debug]Processed: ##vso[task.complete result=Failed]Error detected
2021-04-20T19:07:36.5695707Z ##[debug]Leaving D:\a\_tasks\PowerShell_e213ff0f-5d5c-4791-802d-52ea3e7be1f1\2.180.1\powershell.ps1.
2021-04-20T19:07:36.6389680Z ##[section]Finishing: PowerShell Script MSDeploy

更新:我现在认为这与密码有关。我已经验证了正确的密码在 smarteraspPassword 变量中(对于每个范围,以防万一)。我尝试将它直接硬编码到 powershell 任务中,我也尝试创建一个查找/替换任务,如文章中的前两个任务,以将其直接替换到 deploy.cmd 文件本身。无论我做什么,当我尝试回显该变量时,我总是看到三个星号。当我在本地尝试此操作时,我会看到传入的实际密码。我是在做某事,还是由于 DevOps 知道要掩盖它或其他原因,这是一个疯狂的追逐?

0 个答案:

没有答案