Upload files to userContent of jenkins server - jenkins

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))'
}

Related

Where is the documentation for git scriptPath if creating a job DSL pipeline?

E.g. if you were creating the Job DSL for a pipeline such as mentioned here Defining jenkinsfile script path when creating pipeline where is the documentation for scriptPath?
I don't see anything here: https://plugins.jenkins.io/git/
as described in the comment, the script path is the relative location of your Jenkinsfile in the git repository.
here you can find information about Jenkinsfile:
https://www.jenkins.io/doc/book/pipeline/jenkinsfile/
here you can find information about syntax:
https://www.jenkins.io/doc/book/pipeline/syntax/
in addition in your Jenkins gui once created a pipeline item, configure it and you can find the link to "Pipeline Syntax" that help you to generate a script.

How to have modular Jenkins Pipeline?

I would like to create a Jenkins declarative pipeline and would like to have the pipeline structure as following:
mainPipeline.groovy
stage1.groovy
stage2.groovy
stage3.groovy
mainPipeline looks like the following:
pipeline {
stages {
stage('stage1') {
// Call method from the file Stage1.groovy
}
stage('stage2') {
// Call method from the file Stage2.groovy
}
}
}
I have two main questions:
How do I link these files to a Library?
How do I configure Jenkins Pipeline, so that Jenkins not only knows the main JenkinsFile which is mainPipeline but also the submodules?
I would not recommend to separate your Jenkinsfile into separate files, since there are better options:
You can execute jobs within your pipeline with Pipeline: Build Step plugin. Use this to execute stages that gonna be used by multiple jobs. For example I use this to deploy my applications in a common deploy job.
You can extend Jenkins with your own libraries, which you can load per job or for all jobs. See: Extending with Shared Libraries
For both methods the defining Jenkinsfiles/Groovy scripts can come from SCM.
If you really want to load script from the project path then check this question. If you want to use multiple Jenkinsfiles from the project path, you can just add more Jenkinsfiles as "Project Recognizers" when you configure the job.

How to trigger stages on file

I am writing a Jenkins pipeline. I am looking for trick to trigger pipeline stages when one JSON file exists in path.

What are seed jobs in Jenkins and how does it work?

What are seed jobs in Jenkins and how does it work ?
Can we create a new job from seed job without using github ?
That depends on context. Jenkins itself does not provide "seed jobs".
There's plugins that allow creating jobs from other jobs, like the excellent Job-DSL plugin. With that, you can create jobs where a groovy script creates a larger number of jobs for you.
The Job-DSL plugin refers to those jobs as "seed jobs" (but they're regular freestyle or pipeline jobs). The Job-DSL plugin does not require a github connection.
The seed job is a normal Jenkins job that runs the Job DSL script; in turn, the script contains instructions that create additional jobs. In short, the seed job is a job that creates more jobs. In this step, you will construct a Job DSL script and incorporate it into a seed job. The Job DSL script that you’ll define will create a single freestyle job that prints a 'Hello World!' message in the job’s console output.
A Job DSL script consists of API methods provided by the Job DSL plugin; you can use these API methods to configure different aspects of a job, such as its type (freestyle versus pipeline jobs), build triggers, build parameters, post-build actions, and so on. You can find all supported methods on the API reference site.
The jobs we used for creating new jobs are called Seed Jobs and this seed job generates new jobs using Jenkins files (using JobDSL plugin).
Here, we disabling this feature (Enable script security for Job DSL scripts)
Jenkins Dashboard→ Manage Jenkins → Configure Global Security
Way to create seed job :
JobDSL scripts for generating new jobs.
Job1.groovy
job("Job1"){
description("First job")
authenticationToken('secret')
label('dynamic')
scm {
github('Asad/jenkins_jobDSL1', 'master')
}
triggers {
gitHubPushTrigger()
}
steps {
shell ('''
echo "test"
''')
}
}
buildPipelineView('project-A') {
title('Project A CI Pipeline')
displayedBuilds(5)
selectedJob('Job1')
showPipelineParameters()
refreshFrequency(60)
}
and create same way others Job2.groovy and so on.
For Jenkins Job DSL documentation:-
Follow https://jenkinsci.github.io/job-dsl-plugin/
Think about a job - what is it actually ?
It is actually just a java/jre object that represents like this
How you generates such job/build ?
Configure Jenkins UI -> rest api to Jenkins url -> Jenkins service receive your call on the relevant endpoint -> calling to the relevant code/method and generate this new job
How Seed job will make it ?
Configure seed job on Jenkins UI only once -> run this seed job - > this code run against the internal Jenkins methods and skip all the manual process describes above
Now, when your code can talk directly to Jenkins code , things are much easier.just update your code on the relevant repo - and you are done

jenkins pipeline unable to read files

I have a simple Jenkinsfile where I want to load some data from the workspace. I am using the pipeline plugin to leverage the Jenkinsfile inside of the repository. The build is farmed off to a matching Jenkins agent. When I try to use "readFile" I get the following message:
java.io.FileNotFoundException: /path/to/jenkins/workspace/XXXXX/project/data.json (No such file or directory)
I also get the same message when trying to load a Groovy file from the workspace.
My Jenkinsfile looks like:
node('master') {
stage "Start"
echo "Starting"
stage "Load File"
def myJson = readFile "data.json"
}
Any ideas why I can't read these files?
Thanks,
Tim
When Jenkins processes a Jenkinsfile it does not automatically pull down the entire source repository. You need to execute "checkout scm" to pull down the contents of the repository. If you fail to do so no other files will be available to the pipeline script.

Resources