Skip to content

Commit

Permalink
New hasGlobalResource step
Browse files Browse the repository at this point in the history
And make hasGlobalVar step NonCPS.
  • Loading branch information
samrocketman committed Dec 14, 2022
1 parent ac33645 commit 8da9edc
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
41 changes: 41 additions & 0 deletions vars/hasGlobalResource.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
Copyright 2014-2022 Sam Gleske - https://github.com/samrocketman/jervis
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/**
hasGlobalResource will check to see if a file exists in the resources
directory of any shared library. This will allow for conditional
libraryResource steps. Skip attempting to load the resource if it doesn't
exist.
*/
import hudson.model.Run
import org.jenkinsci.plugins.workflow.libs.LibrariesAction
import org.jenkinsci.plugins.workflow.libs.LibraryRecord

/**
Check if a global library provides a resource.
*/
@NonCPS
Boolean lookForGlobalLibraryResource(Run run, String resourceFile) {
def action = run.getAction(LibrariesAction)
action.libraries.any { LibraryRecord library ->
File file = new File(run.getRootDir(), "libs/${library.name}/resources/${resourceFile}")
file.exists()
}
}

@NonCPS
Boolean call(String resourceFile) {
lookForGlobalLibraryResource(currentBuild.rawBuild, resourceFile)
}
7 changes: 4 additions & 3 deletions vars/hasGlobalVar.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
/**
hasGlobalVar will check to see if a global variable exists which is possible
loaded by anotehr pipeline library. This is useful for optionally detecting
loaded by another pipeline library. This is useful for optionally detecting
and calling variables provided by other pipelines. It gives a means of
gracefully falling back if the variables provided by other pipelines are not
loaded or do not exist.
Expand All @@ -29,10 +29,11 @@ import org.jenkinsci.plugins.workflow.libs.LibraryAdder
functionality provided by other global libraries (only if they are defined).
*/
@NonCPS
boolean hasGlobalVar(String var) {
Boolean hasGlobalVar(String var) {
var in ExtensionList.lookup(LibraryAdder.GlobalVars.class)[0].forRun(currentBuild.rawBuild)*.name
}

def call(String var) {
@NonCPS
Boolean call(String var) {
hasGlobalVar(var)
}

0 comments on commit 8da9edc

Please sign in to comment.