|
| 1 | +git checkout [commit-ref] [filename] |
| 2 | + |
| 3 | +git checkout HEAD file/to/restore |
| 4 | + |
| 5 | +git checkout master~5 filename |
| 6 | + |
| 7 | + |
| 8 | +git restore --source=c5f567 file1/to/restore file2/to/restore |
| 9 | +Or if you want to restore to the content of one commit before c5f567: |
| 10 | + |
| 11 | +git restore --source=c5f567~1 file1/to/restore file2/to/restore |
| 12 | + |
| 13 | + |
| 14 | +Shared Libraries: |
| 15 | + |
| 16 | +You only need to write your code once, and then you can share the same code with all of your pipelines. |
| 17 | + |
| 18 | +You can store your “reusable bits” in a Shared Library in Jenkins. |
| 19 | + |
| 20 | +A shared library is a collection of independent Groovy scripts which you pull into your Jenkinsfile at runtime. |
| 21 | + |
| 22 | +The best part is, the Library can be stored, like everything else, in a Git repository. This means you can version, tag, to with Git. |
| 23 | + |
| 24 | + |
| 25 | +Create the shared library |
| 26 | + |
| 27 | +First you need to create a Git repository which will contain your library of functions (steps). (You can also use Subversion.) |
| 28 | + |
| 29 | +In your repository, create a directory called vars. This will hold your custom steps. Each of them will be a different .groovy file underneath your vars directory, e.g.: |
| 30 | + |
| 31 | + |
| 32 | +vars/ |
| 33 | + deployApplication.groovy |
| 34 | + parseFile.groovy |
| 35 | + sayHello.groovy |
| 36 | + readSystemCredentials.groovy |
| 37 | + doCodeReview.groovy |
| 38 | + |
| 39 | + |
| 40 | +Add your custom steps |
| 41 | + |
| 42 | +Each of your custom steps is a different .groovy file inside your vars/ directory. In Jenkins terminology, these are called Global Variables, which is why they are located inside vars/. |
| 43 | + |
| 44 | +Create a file for your custom step, and fill in the code. For example, a simple greeting function would look like this: |
| 45 | + |
| 46 | + |
| 47 | +#!/usr/bin/env groovy |
| 48 | + |
| 49 | +def call(String name = 'human') { |
| 50 | + echo "Hello, ${name}." |
| 51 | +} |
| 52 | + |
| 53 | + |
| 54 | +After writing that, you should write your custom code within the braces { }. |
| 55 | +You can also add parameters to your method - the example above has one parameter called name, |
| 56 | +which has a default value of human (cos we’re being really personal here.) |
| 57 | + |
| 58 | + |
| 59 | +Set up the library in Jenkins |
| 60 | + |
| 61 | +Now you’ve created your library with custom steps, you need to tell Jenkins about it. |
| 62 | + |
| 63 | +You can define a shared library within a Jenkinsfile, or you can configure the library using the Jenkins web console. Personally, I think it’s better to add from the web console, because you then you can share the library across all of your build jobs. |
| 64 | + |
| 65 | +To add your shared library |
| 66 | + |
| 67 | + |
| 68 | +In Jenkins, go to Manage Jenkins → Configure System. Under Global Pipeline Libraries, add a library with the following settings: |
| 69 | + |
| 70 | +Name: pipeline-library-demo |
| 71 | + |
| 72 | +Default version: Specify a Git reference (branch or commit SHA), e.g. master |
| 73 | + |
| 74 | +Retrieval method: Modern SCM |
| 75 | + |
| 76 | +Select the Git type |
| 77 | + |
| 78 | +Project repository: https://github.com/tutorialworks/pipeline-library-demo.git |
| 79 | + |
| 80 | + |
| 81 | +Use the library in a pipeline |
| 82 | + |
| 83 | +To use the shared library in a pipeline, you add @Library('your-library-name') to the top of your pipeline definition, or Jenkinsfile. Then call your step by name, e.g. sayHello: |
| 84 | + |
| 85 | + @Library('pipeline-library-demo')_ |
| 86 | + |
| 87 | + stage('Demo') { |
| 88 | + echo 'Hello world' |
| 89 | + sayHello 'Dave' |
| 90 | + } |
| 91 | + |
| 92 | + |
| 93 | +If you’re using declarative pipeline, the syntax looks slightly different: |
| 94 | + |
| 95 | + libraries { |
| 96 | + lib('pipeline-library-demo') |
| 97 | + } |
| 98 | + |
| 99 | + pipeline { |
| 100 | + // Your pipeline would go here.... |
| 101 | + } |
0 commit comments