{"id":27459720,"url":"https://github.com/mcascone/jenkins-function-test","last_synced_at":"2025-04-15T20:24:03.692Z","repository":{"id":219979400,"uuid":"750435712","full_name":"mcascone/jenkins-function-test","owner":"mcascone","description":null,"archived":false,"fork":false,"pushed_at":"2024-01-30T21:35:24.000Z","size":13,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-01-31T18:06:35.980Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Groovy","has_issues":false,"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/mcascone.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}},"created_at":"2024-01-30T16:35:33.000Z","updated_at":"2024-01-30T16:40:29.000Z","dependencies_parsed_at":"2024-01-30T18:16:56.487Z","dependency_job_id":null,"html_url":"https://github.com/mcascone/jenkins-function-test","commit_stats":null,"previous_names":["mcascone/jenkins-function-test"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mcascone%2Fjenkins-function-test","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mcascone%2Fjenkins-function-test/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mcascone%2Fjenkins-function-test/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mcascone%2Fjenkins-function-test/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mcascone","download_url":"https://codeload.github.com/mcascone/jenkins-function-test/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249146451,"owners_count":21220137,"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":[],"created_at":"2025-04-15T20:24:03.164Z","updated_at":"2025-04-15T20:24:03.682Z","avatar_url":"https://github.com/mcascone.png","language":"Groovy","funding_links":[],"categories":[],"sub_categories":[],"readme":"# How I'm discovering if local files can be loaded as vars (to be used as functions)\n\n1. Create the pipeline in a test repo\n2. create a simple jenkins file to load the files\n3. test various configurations:\n   1. `load('src/test.groovy')`\n   1. `load('vars/test.groovy')`\n\n## Results\n\nIt can be done!  The files can be loaded as vars and used as functions.\n\nThere are some tricks/caveats:\n\n1. The script must end with `return this`\n1. Unless the var is defined prior to the pipeline block, it will go out of scope like any other var when its scope closes.\n\n    ```groovy\n    steps {\n      script {\n        def testScript = load('src/test_src.groovy')\n      }\n    }\n    ```\n\n    `testScript` will go out of scope when the `script` block closes.\n\n  Instead, define the var prior to the pipeline block:\n\n  ```groovy\n      def testScript\n\n      pipeline {\n        agent any\n        stages {\n          stage('test') {\n            steps {\n              script {\n                testScript = load('src/test_src.groovy')\n              }\n            }\n          }\n        }\n      }\n  ```\n\n2. The file must be loaded, and subsequently used, in `script` blocks.\n3. You can define a `call()` function and call it with the name of the script.\n  \n    ```groovy\n    def call() {\n      echo 'this is a call'\n    }\n    ```\n\n    ```groovy\n    script {\n      testScript()\n    }\n    ```\n\n4. You can also use class members inline:\n  \n    ```groovy\n    script {\n      def aValue = load('src/test_src.groovy').aFunction()\n    }\n    ```\n\n\n## Conclusion\n\nThis is a viable way to load local files as vars and use them as functions.  It's not as clean as loading them as shared libraries, but it's a good option for small, simple functions that don't need to be shared across multiple pipelines.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmcascone%2Fjenkins-function-test","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmcascone%2Fjenkins-function-test","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmcascone%2Fjenkins-function-test/lists"}