Search code examples
powershellazure-devopspublishing

Unable to publish to Azure devops Nuget


I have built a module that uses both PSGallery and Azure Devops (I have a free repo up and I have been playing with it). This is the module: https://www.powershellgallery.com/packages/xanderu.helpers/0.1.9

The problem I'm running into is that when I try to leverage the secure repo, I keep getting a forbidden error. This is the way I'm calling the module:

Publish-ToPSGallery -PSGalleryKey $PSGalleryKey `
    -secureRepoName 'PowershellAZDO' `
    -secureRepopublishApiKey $PATToken `
    -secureRepopublishApiURL "https://pkgs.dev.azure.com/$orgName/$projectName/_packaging/$feedName/nuget/v2/" `
    -secureRepoUser $secureRepoUser `
    -secureRepoPass $PATToken `
    -codePath .

Its a pretty strait forward module but basically create a module (or script) that has the tag 'private' and it should publish to the PSGallery or AZDO accordingly. I'm sure it has something to do with how I'm using the token and credentials (or the API key). Does anyone have any idea what I'm doing wrong here?

EDIT: As part of the xanderu.helpers there is a function new-powershelltemplate and it will auto build a module manifest or script base line that can be used for the publish pipeline

ADDITIONAL EDIT: I have also tried the following to push to myget nuget and it pushes but it wont find the module using find-module -repository myget:

Publish-ToPSGallery -PSGalleryKey $PSGalleryKey `
    -secureRepoName 'MyGet' `
    -secureRepopublishApiKey $nugetAPIKey `
    -secureRepopublishApiURL "https://www.myget.org/F/$feedName/api/v2" `
    -secureRepoUser $secureRepoUser `
    -secureRepoPass $secureRepoPass `
    -codePath .

myget example

Edit #3 I did a little more messing around and when I run

nuget.exe sources

After I run the script, I can see that nuget gets PowershellAZDO as a source. Then I run:

nuget push -Source "PowershellAZDO" -ApiKey AzureDevOps .\xanderu.helpers.0.1.9.nupkg

and I get prompted for a username and password. When I type out the username and PATToken, it pushes the package. This appears to be a bug in the Publish-Module command but I'm unsure as to why. It is like the PSCredential that the module is passed isn't honored. I kept digging and found that in the PowerShellGet v2.2.5 PSModule.psm1 file, there is a call to (line 10990):

Publish-PSArtifactUtility @PublishPSArtifactUtility_Params

and I do in fact see the PSCredential object in there. I've added the following step to the PowerShellGet module that seems to fix the issue... but this is a bug in the source it would appear (this starts on line 6018):

        elseif ($NuGetExePath) {
            & $NuGetExePath sources update -Name $Repository -UserName $Credential.Username -Password $Credential.GetNetworkCredential().Password
            Publish-NugetPackage -NupkgPath $NupkgFullName -Destination $Destination -NugetApiKey $NugetApiKey -NugetExePath $NuGetExePath -Verbose:$VerbosePreference
        }

NuGet Version: 5.7.0.6726

PowerShellGet Version: 2.2.5


Solution

  • This is caused by a bug in the Powershellget module v 2.2.5 that I am using. The root cause is because the repo doesn't have a password passed in, you need to add the password to the configs for nuget. This appears to be resolved in the 3.0.0 version of PowerShellGet. I will need to update my manifest/modules to force the load of that version of the module to resolve the issue.

    Edit: scratch that... its looks like I thought this worked but it was because my nuget.exe creds were preserved.

    Edit2: This works without fail and doesn't require screwing with the core modules. After running the Register-PSRepository command, ensure you run the following as well or the publish pipeline to Azure Devops won't work. This is a bug in the module as it works other places.

    nuget sources update -Name $secureRepoParams.Name -UserName $secureRepoParams.Credential -Password $secureRepoParams.Credential.GetNetworkCredential().Password