Jenkins Job Migration to Jenkinsfile - jenkins

I am searching for a way to convert existing jenkins pipeline jobs with parameters to Jenkinsfile that include the parameter within it.
Maybe xml to Jenkinsfile or something but I am yet to discover a way (found job to DSL or something like that)
I am looking to get job to scripted groovy parameter

Related

Is a Jenkinsfile valid standalone groovy?

I'm trying to wrap my head around how this declarative Jenkinsfile is Groovy. I want to write supporting code to execute this outside the Jenkins environment, in pure Groovy, if that's possible. I've been writing example groovy code but still am unsure what "pipeline", "agent", and "stages" are.
Any tips to understand this structure is appreciated
EDIT: I edited this question with simplified code below. I'm just wondering if there is a way that this can be turned into valid groovy code without the preprocessor/groovyshell environment that is utilized by Jenkins
pipeline {
stages {
// extra code here
}
}
No, you can't run Jenkinsfile as a standalone Groovy script. In short, Jenkins executes the pipeline code inside a pre-configured GroovyShell that knows how to evaluate things like pipeline, agent, stages, and so forth. However, there is a way to execute Jenkinsfie without the Jenkins server - you can use JenkinsPipelineUnit test library to write JUnit/Spock unit tests that will evaluate your Jenkinsfile and display the call stack tree. It uses mocks, so you can treat it as interaction-based testing, to see if a specific part of your pipeline gets executed. Plus, you can catch some code errors prior to running the pipeline on the server.
A simple unit test for the declarative pipeline can look like this:
import com.lesfurets.jenkins.unit.declarative.*
class TestExampleDeclarativeJob extends DeclarativePipelineTest {
#Test
void should_execute_without_errors() throws Exception {
def script = runScript("Jenkinsfile")
assertJobStatusSuccess()
printCallStack()
}
}
You can find more examples in the official README.md - https://github.com/jenkinsci/JenkinsPipelineUnit
Alternatively, you can try Jenkinsfile Runner command-line tool that can execute your Jenkinsfile outside of the Jenkins server - https://github.com/jenkinsci/jenkinsfile-runner
UPDATE
I edited this question with simplified code below. I'm just wondering if there is a way that this can be turned into valid groovy code without the preprocessor/groovyshell environment that is utilized by Jenkins.
Your pipeline code example looks like a valid Jenkinsfile, but you can't turned it into a Groovy code that can be run e.g. from the command-line as a regular Groovy script:
$ groovy Jenkinsfile
This won't work, because Groovy is not aware of the Jenkins Pipeline syntax. The syntax is added as a DSL via the Jenkins plugin, and it uses a dedicated GroovyShell that is pre-configured to interpret the pipeline syntax correctly.
If you are interested in checking if the syntax of the Jenkins Pipeline is correct, there are a few different options:
npm-groovy-lint (https://github.com/nvuillam/npm-groovy-lint) can validate (and even auto-fix) the syntax of your Jenkinsfile without connecting to the Jenkins server,
Command-Line Pipeline Linter (https://www.jenkins.io/doc/book/pipeline/development/#linter) can send your pipeline code to the Jenkins server and validate its syntax.
These are a few tools that can help you with catching up the syntax errors before you run the pipeline. But that's just a nice addon to your toolbox. The first step, as always, is to understand what the syntax means, and the official documentation (https://www.jenkins.io/doc/book/pipeline/syntax) is the best place to start.

Jenkins - How to pass environmental variable from freestyle job to pipeline job

I have a freestyle job in Jenkins and would normally pass parameters to another freestyle job using the Predefined parameters option.
example:
PROJECT=Myproject
PATH=/depot/workspace/
Previously I could access the above values through the KEY in the downstream job through the environment by using ${PROJECT} OR ${PATH}.
My problem now is that I have a pipeline job that needs to access the above values but when using ${PROJECT} OR ${PATH} it does not work.
So, in general how I want it to work is have the freestyle job run first and pass the parameters to the downstream pipeline job.
You might need to use "${params.PROJECT}" in your pipeline to access the parameters.

How to pass env vars to a MultibranchPipelineJob created by Jenkins Job DSL?

I am creating a MultibranchPipelineJob with Jenkins Job DSL. I want to pass some environment variables to the job, but I can't figure out how to do that from the documentation.
MultibranchPipeline Job no longer support parameters
You can use Folder Properties Plugin to set your Env variables, which can be accessed by all the jobs within that folder. https://plugins.jenkins.io/folder-properties/
However, the MultiBranch pipeline job has a lot of performance issues so we moved away from multibranch pipeline jobs. We wrote a DSL job that would act as a multibranch pipeline job - which will scan through the git branches and create Simple pipeline jobs as needed.
You pass them as parameters like this:
parameters {
stringParam("MyVariable1", "my-value1")
stringParam("MyVariable2", "${my-dynamic-value2}")
}
Then consume them in the job using parameters or environment (both work equally) as this:
echo "my vars are ${parameters.MyVariable1} or ${env.MyVariable2}"

Upload files to userContent of jenkins server

I have a multi-branch pipeline job from which I would like to upload a file to the Jenkins userContent location using a Groovy script. I tried the job-dsl-plugin to use userContent method, but it throws the error shown below:
java.lang.NoSuchMethodError: No such DSL method 'userContent' found among steps
Reference: https://github.com/jenkinsci/job-dsl-plugin/wiki/Job-DSL-Commands
Do I need to configure anything in order to upload a file to userContent? Is there any other way to upload a file to the userContent location?
You can not simply mix Pipeline DSL and Job DSL. See Use Job DSL in Pipeline scripts for instructions on using the Job DSL build step as a Pipeline step.
node {
jobDsl scriptText: 'userContent("test.txt", new ByteArrayInputStream("test".bytes))'
}

JenkinsFile buildWithParameters URL, where is my property?

I have been working with JenkinsFile pipeline as code for a while, but yet to find a solution for the following case.
We trigger build, from a GIT repo, the URL contains parameters like this:
http://localhost:8080/job/Pipeline-Pull-Request-Composer/buildWithParameters?token=secret_token&git_ref_change=${PULL_REQUEST_FROM_ID}&pull_request_url=${PULL_REQUEST_URL}
Now, my question is that the parameters are not available in the pipeline, unless I define a job parameter with the same name.
How can I configure the pipeline to read the parameter from the URL in an jenkins env variable/property ?
Thank You

Resources