PowerShell Step: Offline Nuget Bootstrap and Import-PackageProvider

I have a Jenkins controller on Linux with two Jenkins agents on Windows. I need to bootstrap nuget offline as these agents aren’t connected to the internet. I’ve following the bootstrap steps here: https://docs.microsoft.com/en-us/powershell/scripting/gallery/how-to/getting-support/bootstrapping-nuget?view=powershell-7.2.

Made sure to set my PATH environment variable on my node for those directories and I can ‘ls’ and see what the directories have in them within my pipeline. However I still get this error at the Import-PackageProvider run:

This same command works just fine when I’m logged in locally to the machine.

Any ideas?

is this something a plugin is doing? or something in your pipeline that your triggering? Are you able to post your pipeline script at all?

@halkeye here is the pipeline script.

pipeline {
	agent {
		label 'service'
	}
	environment {
		ARTIFACTORY = credentials('artifactory-credential')
	}
	stages {
		stage('Deploy') {
			steps {			
				powershell '''
				$ManifestArgs = @{
					CompanyName = "<Company>"
					Author = "<Author"
					Description = "<Description"
					Guid = [guid]::NewGuid()
					RootModule = "<RootModule>"
					ModuleVersion = "0.0.1"
					FunctionsToExport = "<FunctionsToExport>"
				}
				New-ModuleManifest @ManifestArgs -Path ".\\<Module>.psd1"
				ls "C:\\ProgramData\\Microsoft\\Windows\\PowerShell\\PowerShellGet\\"
				Import-PackageProvider -Name "Nuget" -RequiredVersion "2.8.5.216" -Force
				Register-PSRepository -Name Artifactory -SourceLocation "http://<server>:8082/artifactory/api/nuget/nuget-cs-dev/" -PackageManagementProvider NuGet -InstallationPolicy Trusted -PublishLocation "http://<server>:8082/artifactory/api/nuget/nuget-cs-dev/" -Credential $ARTIFACTORY
				'''
			}
		}
	}
}

It’s the plugin for Pipeline - Nodes and Processes.

where did you do that? I don’t see that in the pipeline at all. Did you set it in the agent configuration?

how did you confirm it? Most of the time that means someone tested it with their local user, but not the user that runs jenkins. Best way to confirm is bat('echo %PATH%')

I set the environment variables on the agent configuration. To validate this, in testing prior I ran powershell ‘$env:path’.

When I said the same command I meant ‘Import-PackageProvider’. I did test this with my local user, but I can also test it with the user account that the jenkins service runs as.

I’ll supply a couple of outputs in a few minutes.

My admin account run locally:

Service account run locally:

Local environment variables of service account (this is where I realized I configured the non service-account node for env vars not this one):
jenkins_sa_windows_env

Though it didn’t make a difference as the environment vars were the same as local admin:

Jenkins run with correct env vars set at agent level:

And the pipeline script that I’m using with no redactions:

pipeline {
	agent {
		label 'service'
	}
	stages {
		stage('Test') {
			steps {			
				powershell '''
				$env:PATH
				Get-PackageProvider -ListAvailable
				Import-PackageProvider -Name Nuget -RequiredVersion 2.8.5.201 -Force
				'''
			}
		}
	}
}

Sorry for so many posts. As a new user I can’t post more than one embedded image at a time.

I personally do linux and shell scripting, so i can’t be a lot of help. I’m just asking questions hoping it can spark something. I don’t think its a jenkins specific issue. It feels like some env variable is not making it into the pipeline.

Looking at https://docs.microsoft.com/en-us/powershell/scripting/gallery/how-to/getting-support/bootstrapping-nuget?view=powershell-7.2 it seems that nuget.exe needs to be in the path.

if you Get-Command nuget.exe (or something) does it return that it found it in the path?

So in PowerShell that would be Get-ChildItem . Or we can just make it cmd or Linux like:

I’ll provide Jenkins output of that shortly.

Just don’t post images, please. Use markdown and copy the output as text (as you did in the above reply).

I’d say do not run Get-PackageProvider -ListAvailable at all - this commands seems to need the Internet.

What if you follow Manually bootstrapping NuGet.exe to support publish operations on a machine that is not connected to the Internet literally and copy the Microsoft.PackageManagement.NuGetProvider.dll somewhere under C:\Program Files\PackageManagement\ProviderAssemblies\NuGet\2.8.5.208 (or a different version number)? I can see it is already on your PATH, so maybe this is done already?

After this, Import-PackageProvider might work.

I am confused by the doc if NuGet.exe is needed or not. If yes, just copy it anywhere on $env:PATH.

I was denied posting for 22 hours, sorry about that.

I’ll use markdown in the future, thanks for the info. Get-PackageProvider -ListAvailable queries the internet but also queries the local machine, so it doesn’t need the internet but will warn if you’re not connected.

I did end up figuring this out.

  • 32-bit PowerShell was being executed. Found this by running [Environment]::Is64BitProcess() within the PowerShell pipeline step. Which seems to be invoked by Java.
  • Mistakenly had a 32-bit version of Java installed, so while my server was 64-bit, the Jenkins-controller was seeing it as 32-bit.
  • I tried uninstalling the 32-bit version of Java, installed a 64-bit version, deleted the agent, and the service, but recreated the agent, connected and the agent showed up as 64-bit, still 32-bit PowerShell was getting executed.
  • Deploy an entirely new server. Installed 64-bit Java, created new agent, connected, agent showed as 64-bit.

That last bullet point got me past the original issue and with a lot of fiddling around with string interpolation I’m now able to use Publish-Module to Artifactory using the PowerShell pipeline step.

Not sure why the uninstall/re-install didn’t work and kept invoking 32-bit powershell but I assume it has to deal with a Java configuration not getting refreshed.

1 Like