Can hidden values be passed from the pipelineJob to the script in Jenkins Scripted Pipeline?

Viewed 350

I'd like to reuse a script in multiple pipelineJob definitions, whilst hardcoding some of the parameter values and have them invisible to the user. This will allow me to make the script more generic, and hide the unneccessary technical bits from the user.

For example, I want to use the same 'myapp_build.groovy' script but pass in different git repos or OS types to use (which I've labelled as 'hiddenParam' to indicate it should be hidden):

def jobDslScript = 'pipelines/myapp_build.groovy'

pipelineJob("my-app-build") {
  description("Build my-app image")
  parameters {
    choiceParam('environment', ['dev','uat','prod'], 'Environment to build in')
    stringParam('gitBranchName', '', 'git branch name')
    hiddenParam('gitRepoName', 'https://my-repo.github.com', 'git repo path')
    hiddenParam('os_type', 'linux', 'OS to use')
  }
  definition {
    cps {
      script(readFileFromWorkspace(jobDslScript))
      sandbox()
    }
  }
}

pipelineJob("my-other-app-build") {
  description("Build my-other-app image")
  parameters {
    choiceParam('environment', ['dev','uat','prod'], 'Environment to build in')
    stringParam('gitBranchName', '', 'git branch name')
    hiddenParam('gitRepoName', 'https://my-other-repo.github.com', 'git repo path')
    hiddenParam('os_type', 'windows', 'OS to use')
  }
  definition {
    cps {
      script(readFileFromWorkspace(jobDslScript))
      sandbox()
    }
  }
}

This creates 2 different pipelineJobs that call the same script with different parameters.

Currently I achieve this by having these 'hiddenParam's as choiceParam's with only 1 option, and this seems pointless to display, as the user can't change it (and I don't want them to change it). The 'myapp_build.groovy' script handles all the different parameters, and I don't want to use multiple almost-identical copies of the same script just to have the different 'hidden' values set at the start.

Is there a way to do this? One restriction I have is that I'm not able to add new plugins, so if there is a way to achieve this using the vanilla pipelineJob that would be great.

0 Answers
Related