Skip to content

Instantly share code, notes, and snippets.

@aespinosa
Last active September 16, 2024 18:27
Show Gist options
  • Save aespinosa/6bd1b8d2d65aed2179de to your computer and use it in GitHub Desktop.
Save aespinosa/6bd1b8d2d65aed2179de to your computer and use it in GitHub Desktop.
create a jenkins job in the script console
import jenkins.model.Jenkins;
import hudson.model.FreeStyleProject;
import hudson.tasks.Shell;
job = Jenkins.instance.createProject(FreeStyleProject, 'job-name')
job.buildersList.add(new Shell('echo hello world'))
job.save()
build = job.scheduleBuild2(5, new hudson.model.Cause.UserIdCause())
build.get() # Block until the build finishes
generatedJobs = build.getAction(javaposse.jobdsl.plugin.actions.GeneratedJobsBuildAction).getItems()
# FIXME skip .scheduleBuild2() on Folder jobs
generatedJobs.each { j -> j.scheduleBuild2(5, new hudson.model.Cause.UserIdCause()) }
@cjacobs-at-comscore
Copy link

Now, how to create a groovy build step? Specifically a system groovy build step? I'm not a java developer and google isn't helping me.

@djgio02
Copy link

djgio02 commented Jan 7, 2020

is there a way I can add the SCM section in this script? something like:
job.buildersList.add (new GitSCM'repo.git'))
or what would be the right sintax for this request?

@aespinosa
Copy link
Author

@djgio02 you can figure it out by looking at the javadocs of the git-scm jenkins plugin.

@tlopo
Copy link

tlopo commented Sep 2, 2021

I spent some time trying to figure out the SCM part, I am adding a comment here so it may help someone in the future:

import jenkins.model.Jenkins
import hudson.model.FreeStyleProject
import hudson.model.Label
import hudson.tasks.BatchFile
import hudson.plugins.git.GitSCM
import hudson.plugins.git.BranchSpec
  
def scm = new GitSCM('https://github.com/tlopo/WinAppDriver-Sample.git')
scm.branches = [new BranchSpec("*/master")]
  
command = """ 
powershell -Command npm install -g appium
powershell -Command reg add "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\AppModelUnlock" /t REG_DWORD /f /v "AllowDevelopmentWithoutDevLicense" /d "1"
powershell "Start-Process 'cmd' -ArgumentList '/c \\"C:\\Program Files (x86)\\Windows Application Driver\\WinAppDriver.exe\\" 127.0.0.1 4723/wd/hub  1>winappdriver_out.log 2>winappdriver_err.log' -WindowStyle Hidden
.\\mvnw.cmd test
"""
  
job = Jenkins.instance.createProject(FreeStyleProject, 'winappdriver-poc')
job.setScm scm
job.buildersList.add(new BatchFile(command))
job.setAssignedLabel( Jenkins.instance.getLabel('win'))
job.save()

@DonaldAshworthJr
Copy link

Responding to an ancient post with hopes of some direction :o)

Needing to create a new branch using the beginnings of the script below (I need to use script like this). I am needing scm.??? for 1) complete a checkout and 2) push to origin (in script).

Many thanks in advance.

[Manual Git Steps that do what I need]

// CHECK-OUT - To create a new branch and switch to it, use git checkout -b <branch_name>.
git checkout -b release-dev master

// CHECK-IN
git push origin release-dev

[Partial Synthesis]

import hudson.util.RemotingDiagnostics
import jenkins.model.Jenkins
import hudson.plugins.git.GitSCM

def scm = new GitSCM("https://someurl/api/v3/repos/HPEFS/don-test")
scm.branches = [[name: "*/release-dev"]]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment