From da1c31e7564c0ed00398d98497b51f1195f99e0b Mon Sep 17 00:00:00 2001 From: Rui Lopes Date: Sat, 28 Oct 2017 14:00:12 +0100 Subject: [PATCH] provision powershell repositories --- README.md | 1 + Vagrantfile | 1 + .../src/main/groovy/provision.groovy | 9 +++ .../windows/use-powershell-repository.ps1 | 74 +++++++++++++++++++ 4 files changed, 85 insertions(+) create mode 100644 provision/windows/use-powershell-repository.ps1 diff --git a/README.md b/README.md index 9becf66..f4b3bb0 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,7 @@ This will: * Create the `adhoc-package` repository. * Create the `npm-group`, `npm-hosted` and `npmjs.org-proxy` repositories. * Create the `chocolatey-group`, `chocolatey-hosted` and `chocolatey.org-proxy` repositories. + * Create the `powershell-group`, `powershell-hosted` and `powershellgallery.com-proxy` repositories. * Configure the NuGet `nuget-hosted` repository to accept pushing with an API key. * Schedule a task to remove the old snapshots from the `maven-snapshots` repository. * Create users and a custom `deployer` role. diff --git a/Vagrantfile b/Vagrantfile index e808c1d..080f0f5 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -28,5 +28,6 @@ Vagrant.configure(2) do |config| config.vm.provision :shell, inline: "echo '#{nexus_ip} #{nexus_domain}' | Out-File -Encoding Ascii -Append c:/Windows/System32/drivers/etc/hosts" config.vm.provision :shell, path: 'provision/windows/ps.ps1', args: ['provision-base.ps1', nexus_domain] config.vm.provision :shell, path: 'provision/windows/ps.ps1', args: ['use-chocolatey-repository.ps1', nexus_domain] + config.vm.provision :shell, path: 'provision/windows/ps.ps1', args: ['use-powershell-repository.ps1', nexus_domain] end end diff --git a/provision/provision-nexus/src/main/groovy/provision.groovy b/provision/provision-nexus/src/main/groovy/provision.groovy index 0003fae..1b4fd99 100644 --- a/provision/provision-nexus/src/main/groovy/provision.groovy +++ b/provision/provision-nexus/src/main/groovy/provision.groovy @@ -26,6 +26,15 @@ repository.createNpmProxy("npmjs.org-proxy", "https://registry.npmjs.org", "defa repository.createNpmGroup("npm-group", ["npm-hosted", "npmjs.org-proxy"], "default") +// create a powershell repository backed by the default blob store. +repository.createNugetHosted("powershell-hosted", "default", true, WritePolicy.ALLOW_ONCE) +// create a powershell proxy repository backed by the default blob store. +// see https://help.sonatype.com/display/NXRM3/.NET+Package+Repositories+with+NuGet +repository.createNugetProxy("powershellgallery.com-proxy", "https://www.powershellgallery.com/api/v2/", "default") +// create a powershell group repository that merges the powershell-host and powershellgallery.com-proxy together. +repository.createNugetGroup("powershell-group", ["powershell-hosted", "powershellgallery.com-proxy"], "default") + + // create a chocolatey repository backed by the default blob store. repository.createNugetHosted("chocolatey-hosted", "default", true, WritePolicy.ALLOW_ONCE) // create a chocolatey proxy repository backed by the default blob store. diff --git a/provision/windows/use-powershell-repository.ps1 b/provision/windows/use-powershell-repository.ps1 new file mode 100644 index 0000000..bbd0e2f --- /dev/null +++ b/provision/windows/use-powershell-repository.ps1 @@ -0,0 +1,74 @@ +param( + [string]$nexusDomain = 'nexus.example.com' +) + +# NB this is needed to answer 'Yes' to all PS related questions. +$ConfirmPreference = 'None' + +Write-Host 'Default PowerShell sources:' +Get-PSRepository + +Write-Host 'Configuring PowerShell to only use the nexus server...' +Get-PSRepository | Unregister-PSRepository +Install-PackageProvider NuGet -Force +Register-PSRepository ` + -Name nexus ` + -SourceLocation https://$nexusDomain/repository/powershell-group/ ` + -PublishLocation https://$nexusDomain/repository/powershell-hosted/ ` + -InstallationPolicy Trusted + +Write-Host 'Current PowerShell sources:' +Get-PSRepository + +Write-Host 'Installing the Sql Server module from the nexus server...' +Install-Module SqlServer + +Write-Host 'Installing nuget and configuring PowerShellGet to use it...' +choco install -y nuget.commandline +$psGetNugetPath = 'C:\ProgramData\Microsoft\Windows\PowerShell\PowerShellGet\NuGet.exe' +mkdir (Split-Path -Parent $psGetNugetPath) | Out-Null +New-Item ` + -ItemType SymbolicLink ` + -Path $psGetNugetPath ` + -Target 'C:\ProgramData\chocolatey\lib\NuGet.CommandLine\tools\NuGet.exe' ` + | Out-Null + +Write-Host 'Publishing the ExampleGreeter module into the nexus server...' +cd $env:TEMP +mkdir ExampleGreeter | Out-Null +Push-Location ExampleGreeter +Set-Content ` + -Encoding Ascii ` + ExampleGreeter.psm1 ` + @' +function Write-Greeting([string]$name) { + "Hello $name!" +} +'@ +New-ModuleManifest ` + ExampleGreeter.psd1 ` + -ModuleVersion '1.0.0' ` + -Author 'John Doe' ` + -Description 'The Classic Hello World' ` + -LicenseUri 'https://opensource.org/licenses/MIT' ` + -ProjectUri 'https://example.com/ExampleGreeter' ` + -RootModule 'ExampleGreeter.psm1' ` + -Tags ` + hello, + example ` + -FunctionsToExport ` + Write-Greeting ` + -CmdletsToExport @() ` + -VariablesToExport @() ` + -AliasesToExport @() +Test-ModuleManifest ExampleGreeter.psd1 +Publish-Module ` + -Path . ` + -Repository nexus ` + -NuGetApiKey (Get-Content c:\vagrant\shared\jenkins-nuget-api-key) +Pop-Location + +Write-Host 'Installing and using the ExampleGreeter module...' +Install-Module ExampleGreeter +Get-Module ExampleGreeter +Write-Greeting 'World'