Answers: <p>I think it should work and I don't know why it isn't.</p>
  <p>It is unusual for someone to load a shared library dynamically using the <code>library</code> step instead of using the <code>@library</code> annotation. Since you are specifying a <code>retriever</code>, why don't you pass the tag to it?</p>
  <p>You can find out all the options for <code>GitSCM</code> by going to the <a href="https://docs.cloudbees.com/docs/admin-resources/latest/pipelines/pipeline-dev-tools" rel="nofollow noreferrer">snippet generator</a> and trying the <code>library</code> step.</p>
  <p>This is what I came up with:</p>
  <pre><code>library changelog: false, identifier: 'jenkins-shared-library', retriever: legacySCM([$class: 'GitSCM', branches: [[name: '41']], extensions: [], userRemoteConfigs: [[credentialsId: 'account', url: 'https://website.com.repo.git']]])
  </code></pre>
  <p>but I personally like to structure my code a bit more</p>
  <pre><code>final Object lib = library(
      [
          identifier: 'jenkins-shared-library',
          changelog: false,
          retriever: legacySCM(
              [
                  $class: 'GitSCM',
                  branches: [
                      [
                          name: '41'
                      ]
                  ],
                  extensions: [],
                  userRemoteConfigs: [
                      [
                          credentialsId: 'account',
                          url: 'https://website.com.repo.git'
                      ]
                  ]
              ]
          )
      ]
  )
  </code></pre>
  <p>I hope that specifying the tag via the GitSCM works for you.</p>
  <p>I think you should drop by <a href="https://community.jenkins.io/" rel="nofollow noreferrer">https://community.jenkins.io/</a> and post about what you are trying to do in your pipeline. When you load a library dynamically because you lose one of the main benefits which is that shared libraries arent subjected to script security, at least when they are loaded via the <code>@library</code> annotation.</p>
  
Answers: CC BY-SA 4.0
Answers: 2021-10-28T17:34:28.470Z
Answers: 2021-10-28T17:34:28.470Z
Answers: Levi
Body: <p>So trying to checkout shared library by tag. This works perfectly with moderSCM, but for reasons we need to use other way (legacySCM);</p>
  <pre><code>def lib = library identifier: 'jenkins-shared-library@41', 
            retriever: legacySCM([$class: 'GitSCM', 
            userRemoteConfigs: [[credentialsId: 'account', 
           url: 'https://website.com.repo.git']]])
  </code></pre>
  <p>Doing that results to:</p>
  <pre><code>00:00:01.518  Loading library jenkins-shared-library@41
  00:00:01.521  Selected Git installation does not exist. Using Default
  </code></pre>
  <p>However it does not checkout the tag &quot;41&quot;. It just checkouts master. Why is the case?</p>
  
Tags: <jenkins><jenkins-pipeline>
Title: Checkout shared library with tag with legacySCm
UserDisplayName: WhoAmI
ThreadText: # Checkout shared library with tag with legacySCm
  
  (Asked by: username_0 on 2021-10-26)
  
  So trying to checkout shared library by tag. This works perfectly with moderSCM, but for reasons we need to use other way (legacySCM);
  
  ```
  def lib = library identifier: 'jenkins-shared-library@41', 
            retriever: legacySCM([$class: 'GitSCM', 
            userRemoteConfigs: [[credentialsId: 'account', 
           url: 'https://website.com.repo.git']]])
  
  ```
  
  Doing that results to:
  
  ```
  00:00:01.518  Loading library jenkins-shared-library@41
  00:00:01.521  Selected Git installation does not exist. Using Default
  
  ```
  
  However it does not checkout the tag "41". It just checkouts master. Why is the case?
  
  **Tags:** jenkins, jenkins-pipeline
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2021-10-28. Score: 0
  
  I think it should work and I don't know why it isn't.
  
  It is unusual for someone to load a shared library dynamically using the `library` step instead of using the `@library` annotation. Since you are specifying a `retriever`, why don't you pass the tag to it?
  
  You can find out all the options for `GitSCM` by going to the [snippet generator](https://docs.cloudbees.com/docs/admin-resources/latest/pipelines/pipeline-dev-tools) and trying the `library` step.
  
  This is what I came up with:
  
  ```
  library changelog: false, identifier: 'jenkins-shared-library', retriever: legacySCM([$class: 'GitSCM', branches: [[name: '41']], extensions: [], userRemoteConfigs: [[credentialsId: 'account', url: 'https://website.com.repo.git']]])
  
  ```
  
  but I personally like to structure my code a bit more
  
  ```
  final Object lib = library(
      [
          identifier: 'jenkins-shared-library',
          changelog: false,
          retriever: legacySCM(
              [
                  $class: 'GitSCM',
                  branches: [
                      [
                          name: '41'
                      ]
                  ],
                  extensions: [],
                  userRemoteConfigs: [
                      [
                          credentialsId: 'account',
                          url: 'https://website.com.repo.git'
                      ]
                  ]
              ]
          )
      ]
  )
  
  ```
  
  I hope that specifying the tag via the GitSCM works for you.
  
  I think you should drop by <https://community.jenkins.io/> and post about what you are trying to do in your pipeline. When you load a library dynamically because you lose one of the main benefits which is that shared libraries arent subjected to script security, at least when they are loaded via the `@library` annotation.
  
  
  ---
  
  