Create environment dashboard view using job dsl - jenkins

can anyone point me in the right direction here:
I am using the Environment Dashboard plugin and Job DSL plugin in Jenkins.
I can manually create a view for a job using the Environment Dashboard option. I want to create the Environment Dashboard on the main Dashboard home of jenkins, and not within any folder.
I want to be able to achieve this by defining it within the Job Dsl script. Is this possible and any sample scripts I can use please?

JobDSL provides a way to check all available API options by the URL:
<your-jenkins-url>/plugin/job-dsl/api-viewer/index.html
The list of the options depends on your Jenkins configuration. I see that it is possible to configure it as wrapper for some jobs:

Related

is there a way to share scripts in jenkins (not using PIPELINE)

Is there a way to have a shared script repo/library in Jenkins?
For example, there are building steps that run scripts I wrote. Instead of copying these script to each repo.. it would be nice to have a way to get them through one central place.
The only thing I found was:
https://jenkins.io/doc/book/pipeline/shared-libraries/
but its for pipelines which isn't an option for me.
What workaround do I have?
We use Managed Scripts plugin in order to share scripts between jobs.
After installing the "Managed Scripts" plugin, you have a new option to create a managed script. These scripts can be reused in build steps in every job. Within the job, a user is able to select the required script from a drop-down menu.

Is there an easy way to convert existing Jenkins freestyle job to New Jenkins 2.x pipeline view

I have tons of existing Freestyle Jenkins jobs in my old Jenkins Instance and now I would like to migrate them to New Jenkins 2.0 pipeline jobs using a Jenkins file.
I am following this page to create a Jenkins file: https://jenkins.io/doc/book/pipeline/jenkinsfile/
Can someone please suggest me if there is any easy way (or) plugin i can use to convert these existing job's to pipeline view.
I haven't tried the plugin but while searching for same i came across this post:
https://jenkins.io/blog/2017/12/15/auto-convert-freestyle-jenkins-jobs-to-coded-pipeline/
Maybe this can help. Will update more after actually giving it a ago. You can go through and try by yourself as well.
don't think there is an automatic tool to convert jobs.
We created all new jobs with Jenkins DSL plugin
for the Jenkinsfile - we created some good examples of our basic flows and each team convert the relevant jobs.

Jenkins manipulate build artifacts

Is there a way/plugin to manipulate artifacts after build on Jenkins?
Basically, from the build result page, you can click on "MANIPULATE" link or button and execute a script/command.
Thank you.
One way you could accomplish this is:
Create a RESTful web service on the box running Jenkins. The endpoint would contain the logic/script that you want to execute. If you edit the artifact on the Jenkins master directly, it will get updated in the UI.
Add some simple HTML to the build description to call the RESTful end point you made in #1. For example, via a link target in a button or a tag. Note, you can also update the build description dynamically for each build using a plugin like Groovy Postbuild.
UPDATE
Note that if you don't want to create a web service running on the master, you can just invoke another job via the Jenkins remote access api that does the update for you using something like Groovy Postbuild Plugin. The Groovy postbuild step runs on the Jenkins master, so you technically have access to the entire filesystem as well as the Jenkins API.
For example, you would:
Create button that would start another job by hitting the url "http://myjenkinsurl/job/myjob/buildWithParameters?param1=test". Depending on your security, you might need to specify a token to kickoff jobs like this.
Inside that job write a groovy script to update the artifacts via Groovy Postbuild. For example, a small groovy script like
'copy C:\\Jenkins\\jobs\\myjob\\myartifact C:\\Backup'.execute(); could be used to backup the artifact somewhere else.

Jenkins: Can I Generate groovy script from existing Job?

I have a job in Jenkins and want the groovy script for the same? Is there a way I can do that?
I have created a Job using Jenkins, like add shell Command, Sync from repo,etc. Now I wish to have groovy script for the same, but I dont want to go to the trouble of writing the entire thing again. Is there something like Export to .groovy?
P.S. I am not sure of the correct tags.
You want to copy your configuration using Groovy script to create a new job or to persist.
Below Links are helpful for clone/ copy and create jobs using groovy script.
http://jenkins-ci.361315.n4.nabble.com/How-to-Copy-Clone-a-Job-via-Groovy-td4097412.html
Can I use Jenkins CLI or some groovy scripts to create a new job
https://wiki.jenkins-ci.org/display/JENKINS/Clone+all+projects+in+a+View
In case you want to persist your Job, always backup your resources file such as config.xml, jenkins.xml,etc..
you can recreate job from config.xml which holds all your job configuration
Check out this plugin. It seems to be appropriate for your purpose. Though it has some restrictions for plugins in your jobs that are not capable of Pipeline syntax. Anyway, it could be useful for code generation.

setting and accessing global environment variable in Jenkins

I have a Jenkins pipeline view. Say for example the first job is BUILD followed by DEPLOY and TEST job. What I'm trying to achieve here is to have a 'rollback logic' in the test job, meaning when the test job is run and it is successful I want to set current build no as a global environment variable (so that I can potentially access build number from any job) possibly called TESTED_BUILD_NO. But if test fails then I want to trigger DEPLOY job by passing TESTED_BUILD_NO which will deploy last test build.
There is a plugin called promotion builds plugin, it mentions PROMOTION_BUILD_NO variable but when I look at /env-vars.html it is not listed there. I tired looking at api/xml as well but no mention of any promotion variables. Can this logic I mention here be achieved using this plugin? If not how is global environment set and accessed in Jenkins?
Instead of using global variables, you can always use lastStableBuild, which is automatically set by jenkins. In DEPLOY job, use link to lastStableBuild from TEST job, which form is : http://JENKINS_ADDRESS/job/JENKINS_JOB/lastStableBuild/
According to jenkins wiki:
Stable build A build is stable if it was built successfully and no publisher reports it as unstable.
You are best advised to manage global variables from the system management screens:
Manage Jenkins -> Configure System -> Global Properties
Much more reliable compared to setting these externally to Jenkins.
this can be changed using script or via execute shell/batch.
Or you can use simple groovy scrip to change the value based on Previous command/build status.

Resources