AshJune
AshJune

Reputation: 193

using versions in Jenkins Pipeline shared libraries

I am storing my Jenkins Pipeline as groovy scripts inside the shared libraries in Jenkins. I want to use different versions for different jobs. My shared Library script called as sample_jenkins.groovy is as follows:

def call(body) {
  // evaluate the body block, and collect configuration into the object
  def config = [:]
  body.resolveStrategy = Closure.DELEGATE_FIRST
  body.delegate = config
  body()

  // now build, based on the configuration provided
  node {
    stage ('First Stage'){
      echo "This is first stage"
    }
    stage('Second Stage'){
      echo "This is second stage"
    }
  }
}

I have tagged the different versions in my sharedLib repo as v1.0 and v2.0. Incase I want to use v1.0, I have referenced to it as follows with @Library annotation as follows.

From my job, I have accessed the shared Library by calling the groovy script and used the annotation @Library('[email protected]') above it, like in this picture:

pipeline script consuming version <code>v1.0</code> with <code>@Library</code>

I am getting errors when I run the job. Here are the errors I am seeing.

pipeline script error messages

I get the same error when I use @Library('project-examples@master'). Am I going wrong somewhere? How are we supposed to use versioning if this is not the case? How do I tell Jenkins which version to use? Can someone give me a clear picture of how to make use of versions in jenkins shared library?

Upvotes: 0

Views: 3846

Answers (1)

David M. Karr
David M. Karr

Reputation: 15235

Now I remember what the problem very likely is. Groovy annotations have to annotate "something". It's handy if you have an import statement, as @Library can go on that, even if they have no real relation. If you don't have an import statement, then you have to do something that looks really odd. You have to have it annotate "_".

Review https://jenkins.io/doc/book/pipeline/shared-libraries/ again for an example of this.

Upvotes: 4

Related Questions