Skip to content

Instantly share code, notes, and snippets.

@lee-at-work
Created March 15, 2021 18:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lee-at-work/50b3e4ff3da0a6e300c12b6fcccb3e45 to your computer and use it in GitHub Desktop.
Save lee-at-work/50b3e4ff3da0a6e300c12b6fcccb3e45 to your computer and use it in GitHub Desktop.
You can copy and paste the method `void uploadAllNugetToArtifactory()` into your Jenkinsfile and use it in your `script { ... }`.
def call(Map params = [:]) {
uploadAllNugetToArtifactory(params)
}
void uploadAllNugetToArtifactory(Map params = [:]) {
pipelineHelper(sharedLibrary: this, parameters: params) {
params.nugetPackOutputDir = params.get('nugetPackOutputDir', 'nuget-pack-out')
params.nugetReleases = params.get('nugetReleases', 'nuget-releases')
params.nugetFeatures = params.get('nugetFeatures', 'nuget-features')
params.uploadFirstPackage = params.get('uploadFirstPackage', false )
// check gitversion access
if (env.GitVersion_SemVer == '') {
throw new Exception("dotnet tool GitVersion is required.")
}
def preRelease = env.GitVersion_PreReleaseLabel && !(env.BRANCH_NAME ==~ /(?i)(master|releases?\/.+)/)
def nugetRepository = preRelease ? params.nugetFeatures : params.nugetReleases
def filesToUploadPattern = "**/${params.nugetPackOutputDir}/*.nupkg"
// find files to upload
def findFilesToUpload = findFiles(glob: filesToUploadPattern)
if (findFilesToUpload.length == 0) {
throw new Exception("No packages were found using the pattern ${filesToUploadPattern}")
}
// check if all release package already exists
if (!preRelease) {
def artifactoryURL = infra.getArtifactoryUrl()
def spec = new UploadSpec()
// Reduce list to the 1st file
if (params.uploadFirstPackage) {
while(findFilesToUpload.length() > 1) { findFilesToUpload.removeLast() }
}
// Test that we can even find any packages to upload, otherwise fail build
boolean skipPublish = false
findFilesToUpload.each { file ->
def head = new URL("${artifactoryURL}/threeshape-nuget/${file.name}").openConnection()
head.setRequestMethod("HEAD")
def alreadyPublished = head.getResponseCode().equals(200)
skipPublish |= alreadyPublished
if (alreadyPublished) {
echo "Upload of ${file.name} nuget to [${nugetRepository}] skipped as it already exists."
}
spec.addSpec(
pattern: "${file}",
target: "${nugetRepository}/"
)
}
if (skipPublish) {
echo "Upload of nuget from [${env.JOB_NAME}@${env.BRANCH_NAME}], publishing to [${nugetRepository}] skipped as it already exists."
} else {
uploadToArtifactory(repository : nugetRepository, uploadSpec : spec)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment