VSTS 扩展:“由于缺少一个或多个必需参数,无法处理命令:appdirectory webappname ResourceGroupName”

问题描述 投票:0回答:1

我正在为某个版本创建 VSTS 任务,但收到上述错误。我知道该错误意味着什么,但我不确定如何解决,因为这是我第一次创建自定义任务。

本质上,该任务需要一个源文件,并根据脚本中获得的已发布配置文件设置,通过 FTP 将其传输到 Azure。

这是 PowerShell 代码:

[CmdletBinding()]
param()

Trace-VstsEnteringInvocation $MyInvocation

$appdirectory = get-vstsinput -Name appdirectory
$webappname = get-vstsinput -Name webappname
$ResourceGroupName = get-vstsinput -Name ResourceGroupName

#write-host $appdirectory

$location="East US"
    try {
    # Get inputs.
    # Get publishing profile for the web app
    $xml = [xml](Get-AzureRmWebAppPublishingProfile -Name $webappname `
    -ResourceGroupName $ResourceGroupName `
    -OutputFile null)

    # Extracts connection information from publishing profile
    $username = $xml.SelectNodes("//publishProfile[@publishMethod=`"FTP`"]/@userName").value
    $password = $xml.SelectNodes("//publishProfile[@publishMethod=`"FTP`"]/@userPWD").value
    $url = $xml.SelectNodes("//publishProfile[@publishMethod=`"FTP`"]/@publishUrl").value

    Write-Output "$username"
    Write-Output "$password"
    Write-Output "$url"
    #Write-Output "$localpath"

    # Upload files recursively
    Set-Location $appdirectory
    $webclient = New-Object -TypeName System.Net.WebClient
    $webclient.Credentials = New-Object System.Net.NetworkCredential($username,$password)
    $files = Get-ChildItem -Path $appdirectory -Recurse | Where-Object{!($_.PSIsContainer)}
    foreach ($file in $files)
    {
        $relativepath = (Resolve-Path -Path $file.FullName -Relative).Replace(".\", "").Replace('\', '/')
        $uri = New-Object System.Uri("$url/$relativepath")
        "Uploading to " + $uri.AbsoluteUri
        $webclient.UploadFile($uri, $file.FullName)
    }
    $webclient.Dispose()

    } finally {
        Trace-VstsLeavingInvocation $MyInvocation
    }

与此相关的是一个 JSON 文件,用于 TFS 中的构建和发布管理器。这是代码:

    {
    "id": "LongStringofNumbers",
    "name": "NameOfTask",
    "friendlyName": "FriendlyNameOfTask",
    "description": "Build Task that will upload a file or folder to a destination in Azure using Published Profile Credentials.",
    "helpMarkDown": "",
    "category": "Build",
    "visibility": [
        "Build"
    ],
    "runsOn": [
        "Agent",
        "DeploymentGroup"
    ],
    "author": "ME",
    "version": {
        "Major": 0,
        "Minor": 0,
        "Patch": 16
    },
    "instanceNameFormat": "Uploads File Using Published Profile Credentials",
    "groups": [
        {
            "name": "advanced",
            "displayName": "Advanced",
            "isExpanded": true
        }
    ],
    "inputs": [
        {
            "name": "appdirectory",
            "type": "filePath",
            "label": "Source Path",
            "defaultValue": "",
            "required": true,
            "helpMarkDown": "Location of file(s) for uploading to Azure."
        },
        {
            "name": "webappname",
            "type": "string",
            "label": "Azure Webapp name",
            "defaultValue": "",
            "required": true,
            "helpMarkDown": "Azure App name; I.E. - 900014campuslinkapi."
        },
        {
            "name": "ResourceGroupName",
            "type": "string",
            "label": "Azure Resource Group name",
            "defaultValue": "",
            "required": true,
            "helpMarkDown": "Azure Resource Group Name I.E. - 900014-prod."
        }
    ],
    "execution": {
        "PowerShell3": {
            "target": "powershell.ps1",
            "platforms": [
                "windows"
            ],
            "argumentFormat": "",
            "workingDirectory": "$(currentDirectory)"
        }
    }
}

因此,错误表明我需要 JSON 文件中的三个强制参数,这些参数将作为发布管理器中的字段添加。但我认为脚本和 JSON 文件由于某种原因没有连接,因此即使我将项目放入发布管理器字段中,它仍然会失败并出现错误。这是图片:

azure powershell tfs azure-pipelines-release-pipeline
1个回答
0
投票

您仍然需要将这些值提取到脚本中。查看 参考任务 的功能,或查看 文档

您的脚本需要导入并使用任务 SDK 并调用

Get-VstsInput
来检索值。

无论如何,您应该使用

PowerShell3
处理程序,因为
PowerShell
是遗留的。

另一种选择是,如果自定义任务仅供内部使用,则不创建自定义任务,尽管我有 100% 的偏见。我不喜欢将简单的 PowerShell 脚本变成不透明的黑匣子,而更喜欢从源代码控制驱动所有内容。

© www.soinside.com 2019 - 2024. All rights reserved.