Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-62591

Is it possible to compile jenkins pipeline into jar for shared library to use it?

    XMLWordPrintable

    Details

    • Similar Issues:

      Description

      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

        Attachments

          Activity

          cookeem cookeem cookeem created issue -
          cookeem cookeem cookeem made changes -
          Field Original Value New Value
          Description 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 use 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:
          {code:java}
          // 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
              }
          }
          {code}
          2. I create my shared library, source code like this:

           
          {code:java}
          // 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)
              }
          }
          {code}

          Is it possible I can call jenkins steps in my own third-party library? This stucked me for several days. Thx
          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:
          {code:java}
          // 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
              }
          }
          {code}
          2. I create my shared library, source code like this:

           
          {code:java}
          // 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)
              }
          }
          {code}
          Is it possible I can call jenkins steps in my own third-party library? This stucked me for several days. Thx
          Hide
          jglick Jesse Glick added a comment -

          We do not use Jira for asking questions unless some code change is planned. Can use https://www.jenkins.io/mailing-lists/ or chat or StackOverflow etc.

          Show
          jglick Jesse Glick added a comment - We do not use Jira for asking questions unless some code change is planned. Can use https://www.jenkins.io/mailing-lists/ or chat or StackOverflow etc.
          jglick Jesse Glick made changes -
          Resolution Won't Do [ 10001 ]
          Status Open [ 1 ] Closed [ 6 ]

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            cookeem cookeem cookeem
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: