{"id":17403718,"url":"https://github.com/link89/jenkins-oo-shared-lib","last_synced_at":"2026-05-02T14:35:55.760Z","repository":{"id":81055221,"uuid":"362140071","full_name":"link89/jenkins-oo-shared-lib","owner":"link89","description":"An un-opinionated Jenkins shared library built with the object-oriented principles in mind. ","archived":false,"fork":false,"pushed_at":"2021-05-11T09:30:09.000Z","size":15,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-02-02T00:47:49.099Z","etag":null,"topics":["groovy","jenkins","jenkins-pipeline","object-oriented"],"latest_commit_sha":null,"homepage":"","language":"Groovy","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/link89.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-04-27T14:23:51.000Z","updated_at":"2022-10-28T05:41:12.000Z","dependencies_parsed_at":"2023-03-25T13:33:02.570Z","dependency_job_id":null,"html_url":"https://github.com/link89/jenkins-oo-shared-lib","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/link89%2Fjenkins-oo-shared-lib","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/link89%2Fjenkins-oo-shared-lib/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/link89%2Fjenkins-oo-shared-lib/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/link89%2Fjenkins-oo-shared-lib/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/link89","download_url":"https://codeload.github.com/link89/jenkins-oo-shared-lib/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245937242,"owners_count":20696974,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["groovy","jenkins","jenkins-pipeline","object-oriented"],"created_at":"2024-10-16T19:07:25.391Z","updated_at":"2026-05-02T14:35:55.723Z","avatar_url":"https://github.com/link89.png","language":"Groovy","funding_links":[],"categories":[],"sub_categories":[],"readme":"# jenkins-oo-shared-lib\n\nAn un-opinionated Jenkins shared library built with the object-oriented\nprinciples in mind.\n\nI know there are already popular Jenkins share libraries, like\n[Piper](https://github.com/SAP/jenkins-library) for example, are powerful and \npopular. But for solutions like Piper, one of the common concern for a team \nto adopt them is that they are opinionated, especially to a team that have \nalready had a bunch of pipeline scripts. In order to use those libraries, you \nneed to not only have the knowledge of Jenkins, but also the knowledge of domain \nspecific conventions and concepts defined by them, which would take you extra \ntime before you can start to use it in your own task.\n\nIn my opinion, an ideal Jenkins libraries should avoid the restrictions mentioned \nabove. That's why I design this share library to help me maintain my pipeline scripts.\n\nI would suggest you to use it with \n[Jenkins-Job-Builder](https://jenkins-job-builder.readthedocs.io/en/latest/index.html)\nif you want to get rid of the Jenkins' web UI to set up your pipeline.\n\n## Goals\n\nBy creating this new Jenkins share library, my hope is that\n\n- It should be un-opinionated, so that it can be easily adopted with little efforts.\n\n- Any well-trained Java/Groovy programmers can start to use it without any extra \n  knowledge besides of Jenkins. That's why it is built with the popular \n  object-oriented paradigm.\n  \n- IDE friendly. IDE features like auto suggestions and refactor tools should \n  work correctly when you are working with this library.\n  \n## Installation\n\nFollow the Jenkins' official \n[guideline](https://www.jenkins.io/doc/book/pipeline/shared-libraries/#global-shared-libraries) \nto add this library in global or folder level. Then you can start to use it in\nyour own pipeline script. \n  \n## Get Started\n\n### Migrate Your Scripts in 1 Minute\n\nGiven you already have a normal scripted pipeline that check out the source code \nfrom github, which may look like this:\n\n```groovy\nnode {\n  checkout([\n          $class: 'GitSCM',\n          branches: [[name: '*/main']],\n          extensions: [[$class: 'CleanCheckout']],\n          userRemoteConfigs: [\n                  [url: 'git@github.com:link89/selenium-federation.git']\n          ]\n  ])\n}\n```\n\nTo migrate your script to this library, what you need is import this library to\nyour script, overwrite the `doRun` method by copy-paste your original script,\nand add `jenkins.` to the steps functions provided by Jenkins.\n```groovy\n\n// Import this share library, you should add it to Jenkins before you start to use it.\n@Library('jenkins-oo-shared-lib')\nimport com.github.link89.jenkins.BaseJob\n\n// Start to use the method defined in BaseJob by deriving your new class from it\nclass SimpleJob extends BaseJob {\n  void doRun() {\n    // to access the native methods of Jenkins script via `jenkins`\n    jenkins.node {\n      jenkins.checkout([\n              $class: 'GitSCM',\n              branches: [[name: '*/main']],\n              extensions: [[$class: 'CleanCheckout']],\n              userRemoteConfigs: [\n                      [url: 'git@github.com:link89/selenium-federation.git']\n              ]\n      ])\n    }\n  }\n}\n\n// You need to pass the script handler to the job object,\n// so that it can access the native methods provided by Jenkins.\nnew SimpleJob(jenkins: this).run()\n```\n\nSee, it doesn't take too much effort to migrate your script to this library.\n\n### Using this Shared Library in Your Script\n\nSince the checkout is a common operation, I have already provided a friendly \nmethod to do the same thing with less code. Now your can rewrite your checkout \nmethod to make your script more clean.\n\n```groovy\n@Library('jenkins-oo-shared-lib')\nimport com.github.link89.jenkins.BaseJob\n\nclass SimpleJobV2 extends BaseJob {\n  void doRun() {\n    jenkins.node {\n      // Use the method define in `BaseJob` to simplify your script.\n      gitSimpleCheckout([\n              url: 'git@github.com:link89/selenium-federation.git',\n              branch: 'master',\n      ])\n    }\n  }\n}\nnew SimpleJobV2(jenkins: this).run()\n```\n\nNow you may want to parameterize the job so that other users can decide what  \nproject and branch to check out. Luckily, this shared library will load `yaml`\nconfiguration for you automatically when it exists. What you need is add a \nmulti-lines string field named `CONFIGS` to your job and fill it with the \nfollowing default value\n\n```yaml\ngit:\n  url: git@github.com:link89/selenium-federation.git \n  branch: main\n```\n\nNow your script will look like this\n```groovy\n@Library('jenkins-oo-shared-lib')\nimport com.github.link89.jenkins.BaseJob\n\nclass SimpleJobV3 extends BaseJob {\n  void doRun() {\n    jenkins.node {\n      // `c` is a magic method to help you read value from CONFIGS\n      gitSimpleCheckout(c('git') )\n    }\n  }\n}\nnew SimpleJobV3(jenkins: this).run()\n```\n\n### Implement Your Own Share Methods\n\nLet's move forward. Suppose the project you are working on is a nodejs project,\nthat use `nvm` to manage the nodejs dependency. You may find there is a Jenkins\nplugin named [nvm-wrapper](https://plugins.jenkins.io/nvm-wrapper/), but it \ndidn't support to read the version from `.nvmrc` file. Since you know it will be \nuseful for other nodejs projects, then you can implement your helper method in\nthe `BaseJob` class to handle `.nvmrc` file.\n\n```groovy\nclass BaseJob {\n  /* ... */\n  /* ... */\n\n  protected String getDefaultNodeJsVersion() { 'v12.22.1' }\n    \n  /**\n   * A helper method to read NodeJs version from .nvmrc file\n   * It is suppose to use with Jenkins nvm plugin\n   * @return NodeJs version\n   */\n  String getNodeJsVersionFromNvmrc() {\n    try {\n      // the trim is essential or else the nvm plugin may fail!\n      return jenkins.readFile('.nvmrc').trim()\n    } catch (Exception e) {\n      return defaultNodeJsVersion\n    }\n  }\n}\n```\n\nAnd use it in your script:\n\n```groovy\nclass SimpleJobV4 extends BaseJob {\n  void doRun() {\n    jenkins.node {\n      gitSimpleCheckout(c('git') )\n      jenkins.nvm(nodeJsVersionFromNvmrc) {\n        jenkins.sh 'npm install'\n        jenkins.sh 'npm publish'\n      }\n    }\n  }\n}\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flink89%2Fjenkins-oo-shared-lib","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flink89%2Fjenkins-oo-shared-lib","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flink89%2Fjenkins-oo-shared-lib/lists"}