How to access grails plugins directory in cloudbees from jenkins - grails

I would like to execute a pre-build (grails) script from jenkins to replace a file in the plugins directory with a file in my SCM.
#!/bin/bash
PLUGINS_ORIG_DIR="plugins"
PLUGINS_DEST_DIR="/home/<my_user_name>/.grails/2.1.1/projects/judo/plugins"
cp -r $PLUGINS_ORIG_DIR/lang-selector-0.3/* $PLUGINS_DEST_DIR
But the script fails because the $PLUGINS_DEST_DIR cannot be found. Which should be the path or which is the best way to accomplish this?
Thank you.
[EDIT]
I have also tried to create an pre-war event, but it does not work either:
/**
* Copy modified resources to plugins directory, before packing the WAR
*/
eventCreateWarStart = { warName, stagingDir ->
def buildSettings = BuildSettingsHolder.getSettings()
def projectPluginsDir = buildSettings.getProperty("projectPluginsDir")
def baseDir = buildSettings.getProperty("baseDir")
ant.copy(todir:"${projectPluginsDir}/lang-selector-0.3", overwrite:true) {
fileset(dir:"${basedir}/plugins/lang-selector-0.3", includes:"**")
}
ant.copy(todir:"${projectPluginsDir}/jquery-datatables-1.7.5", overwrite:true) {
fileset(dir:"${basedir}/plugins/jquery-datatables-1.7.5", includes:"**")
}
}

as did you set your cloudbees account name ?
then you're wrong, should use /home/jenkins or just $HOME, as builds run on general purpose slaves as "jenkins" user

I have solved it by copying files to ${stagingDir}, instead of ${projectPluginsDir}

Related

Copy subfolder with groovy in Jenkins

I am trying to script a groovy script which copies a complete folder with all subfolder and jobs to the actual folder, where the script is executed.
Here you can see how my folderstructure looks like.
--> Templ
|-->Folder
|-->Folder
|-->Subfolder
|-->Subsubfolder
|-->Subfolder
|-->Folder
-->Execution 2020
|-->Copyscript
I tried with different Plug-Ins like Jobcopy Builder.
Finally I tried with groovy scrips but nothing seems to work.
the simplest way to use AntBuilder
def ant = new AntBuilder()
ant.copy(todir: myDir) {
fileset(dir: "src/test") {
include(name: "**/*.java")
}
}
example taken from here
http://docs.groovy-lang.org/latest/html/documentation/ant-builder.html
to see all parameters of ant copy command see documentation:
https://ant.apache.org/manual/Tasks/copy.html

Gradle - "override" behaviour/properties from build.gradle

I'm not a Gradle expert by any means so please be gentle...
I have a Gradle build which I'm trying to run on Jenkins. The build.gradle contains the following:
repositories {
maven {
url "http://some_internal_corporate_repo"
}
}
The Jenkins server that I am running the job on cannot access "some_internal_corporate_repo".
As I can't modify the build.gradle I would like to know if there's a way I can somehow extend or override the build.gradle, on the Jenkins server, to point to mavenCentral (or similar), maybe via an init file or setting a property etc?
Thanks in advance
EDIT: in the end, because I was using Jenkins, I used it's Groovy support (execute Groovy build step) to address my issue:
def file = new File('build.gradle')
def newConfig = file.text.replace('url "http://some_internal_corporate_repo"', 'url "http://repo.maven.apache.org/maven2/"')
file.text = newConfig
You can define multiple repositories.
The order of declaration determines how Gradle will check for dependencies at runtime
repositories {
maven {
url "http://some_internal_corporate_repo"
}
mavenCentral()
}
You can use a properties to define the maven repo url:
repositories {
maven {
url "${repositories_maven_url}"
}
}
In the gradle.properties file
repositories_maven_url=maven_url
According to the gradle documentation, gradle.properties files are applied in the following order:
gradle.properties in project root directory.
gradle.properties in GRADLE_USER_HOME directory.
system properties, e.g. when -Dgradle.user.home is set on the command line.
Or you can use something like this:
repositories {
maven {
url getMavenUrl()
}
}
/**
* Returns the url of the maven repo.
* Set this value in your ~/.gradle/gradle.properties with repositories_maven_url key
* If the property is not defined returns a default value
* #return
*/
def getMavenUrl() {
return hasProperty('repositories_maven_url') ? repositories_maven_url : "YOUR_DEFAULT_VALUE"
}

Creating Jenkins pipeline using a multi-project gradle build

I have a multi-project Gradle build working locally.
There is a parent gradle file, build.gradle
The settings.gradle file assigns the projects to their respective gradle build files:
include 'load'
include 'loadRemote'
project(':loadRemote').buildFileName = 'remoteLoad_build.gradle'
project(':load').buildFileName = 'load_build.gradle'
rootProject.name = 'EquipLoad'
The build.gradle parent file calls a buildAll command to build the 2 projects from the command line locally.
I created a Jenkins file to build both projects. But the Jenkins pipeline does not recognize the specific project tasks.
These are the tasks for the buildAll command
gradle.projectsEvaluated {
task compileAll (dependsOn: [project(':loadRemote').remoteLoadCleanCompileStage]) {
compileAll.finalizedBy project(':load').loadCleanCompileStage
}
task packageAll (dependsOn: [project(':loadRemote').remoteLoadPackage]) {
packageAll.finalizedBy project(':load').loadPackage
}
task buildAll (dependsOn: [compileAll]) {
buildAll.finalizedBy packageAll
}
}
The error in Jenkins is that it does not recognize the task project(':loadRemote').remoteLoadCleanCompileStage
How can I identify a multi-build project in Jenkins?
Do I have to add the settings.gradle file?
UPDATE
I thought that the different build files could not be located in the project so I added this to the settings.gradle file
rootProject.name = 'EquipLoad'
include 'load'
project(':load').projectDir = new File(settingsDir, rootProject.rootDir.getAbsolutePath() + "/Load")
project(':load').buildFileName = 'load_build.gradle'
include 'loadRemote'
project(':loadRemote').projectDir = new File(settingsDir, rootProject.rootDir.getAbsolutePath() + "/LoadRemote")
project(':loadRemote').buildFileName = 'remoteLoad_build.gradle'
The error is still the same, the build.gradle file (parent) does not recognize the dependency task project(':loadRemote').remoteLoadCleanCompileStage
Looking at the debug statements, the child build gradle files are found and identified:
Evaluating project ':loadRemote' using build file '/var/.../loadRemote/remoteLoad_build.gradle'.
The same text is shown for the load build file.
Yet the tasks within these gradle build files are not recognized in the parent build.gradle file.
The problem was a simple case sensitive mistake.
I named the folders: Load and LoadRemote. But identified them in the gradle scripts as ':load' and ':loadRemote'. By changing the script text to ':Load' and ':LoadRemote' fixed my problem.

Jenkins Shared Library resource not found

I have just started looking into a shared libarary with jenkins in order to combine a load of scripts and pipelines across multiple repos that are pretty much identical.
I have the shared lib loaded and working but when tryign to execute the scripts i the resources folder i keep geting not found errors:
../releaseTagging-EO2DMYOPJ6JGB6JT5Q2RSFJWJWWPALA7F25H7CQNYBEV4ITTEB6Q#tmp/build.sh: not found
I am creating a copy of the file using the following:
createTempLocation(String path) {
String tmpDir = pwd tmp: true
return tmpDir + File.separator + new File(path).getName()
}
and
copyGlobalLibraryScriptcall(String srcPath, String destPath = null) {
destPath = destPath ?: createTempLocation(srcPath)
writeFile file: destPath, text: libraryResource(srcPath)
echo "copyGlobalLibraryScript: copied ${srcPath} to ${destPath}"
sh "chmod +x ${destPath}"
echo "added executable permissions to ${destPath}"
return destPath
}
I am then calling the last function thusly:
runBuild(Map config) {
def script = copyGlobalLibraryScript('build.sh')
sh script
}
(i realise i can collapse the above function in to one line)
This in turn then gets called via (trimed the whole file to relevent part):
pipeline {
agent any
stages {
stage('Build') {
steps {
timestamps {
checkout scm
bbNotify( key: buildKey, name: BuildName) {
runBuild()
}
stash includes: '**', name: 'RelToSTAN'
}
}
}
}
This all fails with the error at the top of the question, however when sshing on to the build server i can find that file int he location specified.
I dont understand why Jenkins cannot find it and execute it.
The issue will be the following:
When using a java File object it‘ll always refer to some location on the Jenkins master. And of course it usually cannot run inside the sandbox.
On the other hand the readFile and writeFile methods always refer to some path on the build agent reserved by the node block where the call is encapsulated.
Long story short: Do not use the File class. Unfortunately you’ll need to create the temp path manually. But that shouldn’t be too hard.

How do I clear my Jenkins/Hudson build history?

I recently updated the configuration of one of my hudson builds. The build history is out of sync. Is there a way to clear my build history?
Please and thank you
Use the script console (Manage Jenkins > Script Console) and something like this script to bulk delete a job's build history https://github.com/jenkinsci/jenkins-scripts/blob/master/scriptler/bulkDeleteBuilds.groovy
That script assumes you want to only delete a range of builds. To delete all builds for a given job, use this (tested):
// change this variable to match the name of the job whose builds you want to delete
def jobName = "Your Job Name"
def job = Jenkins.instance.getItem(jobName)
job.getBuilds().each { it.delete() }
// uncomment these lines to reset the build number to 1:
//job.nextBuildNumber = 1
//job.save()
This answer is for Jenkins
Go to your Jenkins home page → Manage Jenkins → Script Console
Run the following script there. Change copy_folder to your project name
Code:
def jobName = "copy_folder"
def job = Jenkins.instance.getItem(jobName)
job.getBuilds().each { it.delete() }
job.nextBuildNumber = 1
job.save()
My post
If you click Manage Hudson / Reload Configuration From Disk, Hudson will reload all the build history data.
If the data on disk is messed up, you'll need to go to your %HUDSON_HOME%\jobs\<projectname> directory and restore the build directories as they're supposed to be. Then reload config data.
If you're simply asking how to remove all build history, you can just delete the builds one by one via the UI if there are just a few, or go to the %HUDSON_HOME%\jobs\<projectname> directory and delete all the subdirectories there -- they correspond to the builds.
Afterwards restart the service for the changes to take effect.
Here is another option: delete the builds with cURL.
$ curl -X POST http://jenkins-host.tld:8080/jenkins/job/myJob/[1-56]/doDeleteAll
The above deletes build #1 to #56 for job myJob.
If authentication is enabled on the Jenkins instance, a user name and API token must be provided like this:
$ curl -u userName:apiToken -X POST http://jenkins-host.tld:8080/jenkins/job/myJob/[1-56]/doDeleteAll
The API token must be fetched from the /me/configure page in Jenkins. Just click on the "Show API Token..." button to display both the user name and the API token.
Edit: one might have to replace doDeleteAll by doDelete in the URLs above to make this work, depending on the configuration or the version of Jenkins used.
Here is how to delete ALL BUILDS FOR ALL JOBS...... using the Jenkins Scripting.
def jobs = Jenkins.instance.projects.collect { it }
jobs.each { job -> job.getBuilds().each { it.delete() }}
You could modify the project configuration temporarily to save only the last 1 build, reload the configuration (which should trash the old builds), then change the configuration setting again to your desired value.
If you want to clear the build history of MultiBranchProject (e.g. pipeline),
go to your Jenkins home page → Manage Jenkins → Script Console and run the following script:
def projectName = "ProjectName"
def project = Jenkins.instance.getItem(projectName)
def jobs = project.getItems().each {
def job = it
job.getBuilds().each { it.delete() }
job.nextBuildNumber = 1
job.save()
}
This one is the best option available.
Jenkins.instance.getAllItems(AbstractProject.class).each {it -> Jenkins.instance.getItemByFullName(it.fullName).builds.findAll { it.number > 0 }.each { it.delete() } }
This code will delete all Jenkins Job build history.
Using Script Console.
In case the jobs are grouped it's possible to either give it a full name with forward slashes:
getItemByFullName("folder_name/job_name")
job.getBuilds().each { it.delete() }
job.nextBuildNumber = 1
job.save()
or traverse the hierarchy like this:
def folder = Jenkins.instance.getItem("folder_name")
def job = folder.getItem("job_name")
job.getBuilds().each { it.delete() }
job.nextBuildNumber = 1
job.save()
Deleting directly from file system is not safe. You can run the below script to delete all builds from all jobs ( recursively ).
def numberOfBuildsToKeep = 10
Jenkins.instance.getAllItems(AbstractItem.class).each {
if( it.class.toString() != "class com.cloudbees.hudson.plugins.folder.Folder" && it.class.toString() != "class org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject") {
println it.name
builds = it.getBuilds()
for(int i = numberOfBuildsToKeep; i < builds.size(); i++) {
builds.get(i).delete()
println "Deleted" + builds.get(i)
}
}
}
Go to "Manage Jenkins" > "Script Console"
Run below:
def jobName = "build_name"
def job = Jenkins.instance.getItem(jobName)
job.getBuilds().each { it.delete() }
job.save()
Another easy way to clean builds is by adding the Discard Old Plugin at the end of your jobs. Set a maximum number of builds to save and then run the job again:
https://wiki.jenkins-ci.org/display/JENKINS/Discard+Old+Build+plugin
Go to the %HUDSON_HOME%\jobs\<projectname> remove builds dir and remove lastStable, lastSuccessful links, and remove nextBuildNumber file.
After doing above steps go to below link from UI
Jenkins-> Manage Jenkins -> Reload Configuration from Disk
It will do as you need
If using the Script Console method then try using the following instead to take into account if jobs are being grouped into folder containers.
def jobName = "Your Job Name"
def job = Jenkins.instance.getItemByFullName(jobName)
or
def jobName = "My Folder/Your Job Name
def job = Jenkins.instance.getItemByFullName(jobName)
Navigate to: %JENKINS_HOME%\jobs\jobName
Open the file "nextBuildNumber" and change the number. After that reload Jenkins configuration. Note: "nextBuildNumber" file contains the next build no that will be used by Jenkins.
Tested on jenkins 2.293 over linux. It will remove all the build logs but not the corellative build number
cd /var/lib/jenkins/jobs
find . -name "builds" -exec rm -rf {} \;
Be careful with this command because it executes a rm -rf on each find result. You could exec this first to validate if the result are only the builds folder of you jobs
find . -name "builds"
If you are looking for a solution where you have job inside a Folder you can use getItemByFullName function. It also supports white space in folder and job name.
def jobName = "folder_name/job_name"
def job = Jenkins.instance.getItemByFullName(jobName)
job.getBuilds().each { it.delete() }
job.nextBuildNumber = 1
job.save()

Resources