From c7f973b88b3b678f656bc194ac35ff771ad5cdb5 Mon Sep 17 00:00:00 2001 From: James Ruskin Date: Wed, 8 Jan 2025 15:23:22 +0000 Subject: [PATCH] (fix) Enable Sources before Pushing Chocolatey sources that are disabled need to be enabled before we can rely on their credentials. This change ensures that sources are enabled before we attempt to push if they are disabled. --- .../config.xml | 27 ++++++++++++------- jenkins/scripts/Get-UpdatedPackage.ps1 | 27 +++++++++++++------ .../scripts/Invoke-ChocolateyInternalizer.ps1 | 16 ++++++++--- jenkins/scripts/Update-ProdRepoFromTest.ps1 | 19 +++++++++++-- 4 files changed, 67 insertions(+), 22 deletions(-) diff --git a/jenkins/Internalize packages from the Community Repository/config.xml b/jenkins/Internalize packages from the Community Repository/config.xml index 59e6e34..c74691e 100644 --- a/jenkins/Internalize packages from the Community Repository/config.xml +++ b/jenkins/Internalize packages from the Community Repository/config.xml @@ -31,19 +31,28 @@ powershell ''' $temp = Join-Path -Path $env:TEMP -ChildPath ([GUID]::NewGuid()).Guid $null = New-Item -Path $temp -ItemType Directory + $LocalRepoSource = $(choco source --limit-output | ConvertFrom-Csv -Delimiter '|' -Header Name, Uri, Disabled).Where{ + $_.Uri -eq $env:P_DST_URL + }[0] + Write-Output "Created temporary directory '$temp'." ($env:P_PKG_LIST).split(';,') | ForEach-Object { choco download $_ --no-progress --internalize --force --internalize-all-urls --append-use-original-location --output-directory=$temp --source='https://community.chocolatey.org/api/v2/' if ($LASTEXITCODE -eq 0) { - (Get-Item -Path (Join-Path -Path $temp -ChildPath "*.nupkg")).fullname | ForEach-Object { - choco push $_ --source "$($env:P_DST_URL)" --api-key "$($env:P_API_KEY)" --force - if ($LASTEXITCODE -eq 0) { - Write-Verbose "Package '$_' pushed to '$($env:P_DST_URL)'."; - } - else { - Write-Verbose "Package '$_' could not be pushed to '$($env:P_DST_URL)'.`nThis could be because it already exists in the repository at a higher version and can be mostly ignored. Check error logs." - } - } + try { + if ([bool]::Parse($LocalRepoSource.Disabled)) {choco source enable --name="$($LocalRepoSource.Name)" -r | Write-Verbose} + (Get-Item -Path (Join-Path -Path $temp -ChildPath "*.nupkg")).fullname | ForEach-Object { + choco push $_ --source "$($env:P_DST_URL)" --api-key "$($env:P_API_KEY)" --force + if ($LASTEXITCODE -eq 0) { + Write-Verbose "Package '$_' pushed to '$($env:P_DST_URL)'."; + } + else { + Write-Verbose "Package '$_' could not be pushed to '$($env:P_DST_URL)'.`nThis could be because it already exists in the repository at a higher version and can be mostly ignored. Check error logs." + } + } + } finally { + if ([bool]::Parse($LocalRepoSource.Disabled)) {choco source disable --name="$($LocalRepoSource.Name)" -r | Write-Verbose} + } } else { Write-Output "Failed to download package '$_'" diff --git a/jenkins/scripts/Get-UpdatedPackage.ps1 b/jenkins/scripts/Get-UpdatedPackage.ps1 index 3d8573f..0e2aa01 100644 --- a/jenkins/scripts/Get-UpdatedPackage.ps1 +++ b/jenkins/scripts/Get-UpdatedPackage.ps1 @@ -20,6 +20,11 @@ if (([version] (choco --version).Split('-')[0]) -ge [version] '2.1.0') { choco cache remove } +$LocalRepoSource = $(choco source --limit-output | ConvertFrom-Csv -Delimiter '|' -Header Name, Uri, Disabled).Where{ + $_.Uri -eq $LocalRepo -or + $_.Name -eq $LocalRepo +}[0] + Write-Verbose "Getting list of local packages from '$LocalRepo'." $localPkgs = choco search --source $LocalRepo -r | ConvertTo-ChocoObject Write-Verbose "Retrieved list of $(($localPkgs).count) packages from '$Localrepo'." @@ -34,15 +39,21 @@ $localPkgs | ForEach-Object { choco download $_.name --no-progress --internalize --force --internalize-all-urls --append-use-original-location --output-directory=$tempPath --source=$RemoteRepo if ($LASTEXITCODE -eq 0) { - Write-Verbose "Pushing package '$($_.name)' to local repository '$LocalRepo'." - (Get-Item -Path (Join-Path -Path $tempPath -ChildPath "*.nupkg")).fullname | ForEach-Object { - choco push $_ --source $LocalRepo --api-key $LocalRepoApiKey --force - if ($LASTEXITCODE -eq 0) { - Write-Verbose "Package '$_' pushed to '$LocalRepo'." - } - else { - Write-Verbose "Package '$_' could not be pushed to '$LocalRepo'.`nThis could be because it already exists in the repository at a higher version and can be mostly ignored. Check error logs." + try { + if ([bool]::Parse($LocalRepoSource.Disabled)) {choco source enable --name="$($LocalRepoSource.Name)" -r | Write-Verbose} + + Write-Verbose "Pushing package '$($_.name)' to local repository '$LocalRepo'." + (Get-Item -Path (Join-Path -Path $tempPath -ChildPath "*.nupkg")).fullname | ForEach-Object { + choco push $_ --source $LocalRepo --api-key $LocalRepoApiKey --force + if ($LASTEXITCODE -eq 0) { + Write-Verbose "Package '$_' pushed to '$LocalRepo'." + } + else { + Write-Verbose "Package '$_' could not be pushed to '$LocalRepo'.`nThis could be because it already exists in the repository at a higher version and can be mostly ignored. Check error logs." + } } + } finally { + if ([bool]::Parse($LocalRepoSource.Disabled)) {choco source disable --name="$($LocalRepoSource.Name)" -r | Write-Verbose} } } else { diff --git a/jenkins/scripts/Invoke-ChocolateyInternalizer.ps1 b/jenkins/scripts/Invoke-ChocolateyInternalizer.ps1 index 9548372..f2d5561 100644 --- a/jenkins/scripts/Invoke-ChocolateyInternalizer.ps1 +++ b/jenkins/scripts/Invoke-ChocolateyInternalizer.ps1 @@ -43,13 +43,23 @@ begin { Join-Path -ChildPath $Guid | New-Item -ItemType Directory -Path { $_ } | Select-Object -ExpandProperty FullName + + $LocalRepoSource = $(choco source --limit-output | ConvertFrom-Csv -Delimiter '|' -Header Name, Uri, Disabled).Where{ + $_.Uri -eq $RepositoryUrl + }[0] } process { foreach ($item in $Package) { choco download $item --internalize --output-directory="'$TempFolder'" --no-progress --internalize-all-urls --append-use-original-location --source="'$RemoteRepo'" - Get-ChildItem -Path $TempFolder -Filter *.nupkg -Recurse -File | ForEach-Object { - choco push $_.Fullname --source="'$RepositoryUrl'" --api-key="'$NexusApiKey'" --force - Remove-Item -Path $_.FullName -Force + try { + if ([bool]::Parse($LocalRepoSource.Disabled)) {choco source enable --name="$($LocalRepoSource.Name)" -r | Write-Verbose} + + Get-ChildItem -Path $TempFolder -Filter *.nupkg -Recurse -File | ForEach-Object { + choco push $_.Fullname --source="'$RepositoryUrl'" --api-key="'$NexusApiKey'" --force + Remove-Item -Path $_.FullName -Force + } + } finally { + if ([bool]::Parse($LocalRepoSource.Disabled)) {choco source disable --name="$($LocalRepoSource.Name)" -r | Write-Verbose} } } } diff --git a/jenkins/scripts/Update-ProdRepoFromTest.ps1 b/jenkins/scripts/Update-ProdRepoFromTest.ps1 index 68db6b3..65a0e1f 100644 --- a/jenkins/scripts/Update-ProdRepoFromTest.ps1 +++ b/jenkins/scripts/Update-ProdRepoFromTest.ps1 @@ -18,8 +18,18 @@ if (([version] (choco --version).Split('-')[0]) -ge [version] '2.1.0') { choco cache remove } +$LocalRepoSource = $(choco source --limit-output | ConvertFrom-Csv -Delimiter '|' -Header Name, Uri, Disabled).Where{ + $_.Uri -eq $TestRepo -or + $_.Name -eq $TestRepo +}[0] + Write-Verbose "Checking the list of packages available in the test and prod repositories" -$testPkgs = choco search --source $TestRepo --all-versions --limit-output | ConvertFrom-Csv -Delimiter '|' -Header Name, Version +try { + if ([bool]::Parse($LocalRepoSource.Disabled)) {choco source enable --name="$($LocalRepoSource.Name)" -r | Write-Verbose} + $testPkgs = choco search --source $TestRepo --all-versions --limit-output | ConvertFrom-Csv -Delimiter '|' -Header Name, Version +} finally { + if ([bool]::Parse($LocalRepoSource.Disabled)) {choco source disable --name="$($LocalRepoSource.Name)" -r | Write-Verbose} +} $prodPkgs = choco search --source $ProdRepo --all-versions --limit-output | ConvertFrom-Csv -Delimiter '|' -Header Name, Version $tempPath = Join-Path -Path $env:TEMP -ChildPath ([GUID]::NewGuid()).GUID @@ -35,7 +45,12 @@ else { $Packages | ForEach-Object { Write-Verbose "Downloading package '$($_.Name)' v$($_.Version) to '$tempPath'." - choco download $_.Name --version $_.Version --no-progress --output-directory=$tempPath --source=$TestRepo --ignore-dependencies + try { + if ([bool]::Parse($LocalRepoSource.Disabled)) {choco source enable --name="$($LocalRepoSource.Name)" -r | Write-Verbose} + choco download $_.Name --version $_.Version --no-progress --output-directory=$tempPath --source=$TestRepo --ignore-dependencies + } finally { + if ([bool]::Parse($LocalRepoSource.Disabled)) {choco source disable --name="$($LocalRepoSource.Name)" -r | Write-Verbose} + } if ($LASTEXITCODE -eq 0) { $pkgPath = (Get-Item -Path (Join-Path -Path $tempPath -ChildPath '*.nupkg')).FullName