-
Task
-
Resolution: Won't Do
-
Major
-
None
Here's my question, I want to compile jenkins pipeline into jar for shared library to import and use it, so that I can protect my jenkins pipeline source code.
As we know, shared library can use third-party libraries, I write my own third-party library and compile into jar then make shared library to import and use it, but I don't know how to call jenkins pipeline steps in my own third-party library.
Here's what I do:
1. I create my own third-party library write by groovy language and compile it into jar, source code like this:
// src/main/groovy/DemoLibrary.groovy // I want this library to run jenkins pipeline step package com.example import org.jenkinsci.plugins.workflow.steps.durable_task.ShellStep class DemoLibrary { // this function can run command in jenkins master node // runShell(script: script, cwd: cwd) // return: process def runShell(args) { def cmd = args.script if (args.cwd && args.cwd != "") { cmd = "cd ${args.cwd} && ${cmd}" } def cmds = ['bash', '-c', cmd] def proc = cmds.execute() proc.waitFor() if (proc.exitValue() != 0) { throw new Exception("[ERROR] run shell error: ${proc.err.text}") } return proc } // i want this function to call jenkins "sh" step, but i don't know how to get StepContext in shared library // runStepShell(script: script, context: context) // return: stepExecution def runStepShell(args) { def shellStep = new ShellStep(args.script) def stepExecution = shellStep.start(args.context) retrun stepExecution } }
2. I create my shared library, source code like this:
// vars/demoLibrary.groovy @Grab('com.example:demo-library:0.1.0') @Field demoLib = new DemoLibrary() def demoStage() { docker.image("alpine:latest").inside("--user 1000:1000") { def script = "hostname" // run step "sh" can show the hostname of the docker container sh script: script // but run runShell show the hostname of the jenkins master def proc = demoLib.runShell(script: script) echo "${proc.text}" // how can i get the docker stepContext to make my third-party library to run jenkins sh step? demoLib.rrunStepShell(script: script, context: context) } }
Is it possible I can call jenkins steps in my own third-party library? This stucked me for several days. Thx