Skip to content

Commit

Permalink
kubernetes cloud; add jobs to view by regexp
Browse files Browse the repository at this point in the history
  • Loading branch information
dennyzhang committed May 24, 2019
1 parent af9fa48 commit cb6dcbe
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 12 deletions.
31 changes: 20 additions & 11 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,27 @@ File me [[https://github.com/dennyzhang/cheatsheet-jenkins-groovy-A4/issues][Iss
| Create a Jenkins user | [[https://github.com/dennyzhang/cheatsheet-jenkins-groovy-A4/blob/master/create-jenkins-user.groovy][create-jenkins-user.groovy]] |
| Groovy manages files/folders | [[https://github.com/dennyzhang/cheatsheet-jenkins-groovy-A4/blob/master/files-folder.groovy][files-folder.groovy]] |
| Configure max executors in Jenkins | [[https://github.com/dennyzhang/cheatsheet-jenkins-groovy-A4/blob/master/master-executors.groovy][master-executors.groovy]] |
| Config jenkins kubernetes plugin | [[https://github.com/dennyzhang/cheatsheet-jenkins-groovy-A4/blob/master/jenkins-kubernetes-cloud.groovy][jenkins-kubernetes-cloud.groovy]] |
| Configure slack plugin | [[https://github.com/dennyzhang/cheatsheet-jenkins-groovy-A4/blob/master/config-slack.groovy][config-slack.groovy]] |
| Reference | [[https://github.com/cloudbees/jenkins-scripts][GitHub: cloudbees/jenkins-scripts]], [[https://github.com/jenkinsci/pipeline-examples][GitHub: jenkinsci/pipeline-examples]] |
** Jenkins Kubernetes Via Groovy
| Name | Comment |
|-----------------------------------+----------------------------------|
| Config jenkins kubernetes plugin | [[https://github.com/dennyzhang/cheatsheet-jenkins-groovy-A4/blob/master/jenkins-kubernetes-cloud.groovy][jenkins-kubernetes-cloud.groovy]] |
| Validate Kubernetes jenkins setup | [[https://github.com/dennyzhang/cheatsheet-jenkins-groovy-A4/blob/master/validate-kubernetes-cloud.groovy][validate-kubernetes-cloud.groovy]] |
** Jenkins View Via Groovy
| Name | Comment |
|-----------------------------------------+---------------------------------------------------------------------------|
| [[https://javadoc.jenkins.io/hudson/model/ListView.html#setIncludeRegex-java.lang.String-][Add a list of jobs by regexp to a view]] | =myView.setIncludeRegex(".*Integration.*")= [[https://github.com/dennyzhang/cheatsheet-jenkins-groovy-A4/blob/master/addjobstoview-byregexp.groovy][addjobstoview-byregexp.groovy]] |
| Create jenkins views and add jobs to it | [[https://github.com/dennyzhang/cheatsheet-jenkins-groovy-A4/blob/master/jenkins-views.groovy][jenkins-views.groovy]] |
| [[https://github.com/jan-molak/jenkins-build-monitor-plugin/blob/master/build-monitor-plugin/src/main/java/com/smartcodeltd/jenkinsci/plugins/buildmonitor/BuildMonitorView.java][Add a view of build monitor view plugin]] | [[https://github.com/dennyzhang/cheatsheet-jenkins-groovy-A4/blob/master/build-monitor-views.xml][build-monitor-views.xml]] |
** Jenkins Job Via Groovy
| Name | Comment |
|-----------------------------------------+---------------------------------------------------------|
| List all my jenkins jobs | =println Jenkins.instance.projects.collect { it.name }= |
| List all jenkins jobs | [[https://github.com/dennyzhang/cheatsheet-jenkins-groovy-A4/blob/master/list-all-jobs.groovy][list-all-jobs.groovy]] |
| Add a list of jobs by regexp to a view | [[https://github.com/dennyzhang/cheatsheet-jenkins-groovy-A4/blob/master/addjobstoview-byregexp.groovy][addjobstoview-byregexp.groovy]] |
| Create jenkins views and add jobs to it | [[https://github.com/dennyzhang/cheatsheet-jenkins-groovy-A4/blob/master/jenkins-views.groovy][jenkins-views.groovy]] |
| Create and trigger a job | [[https://github.com/dennyzhang/cheatsheet-jenkins-groovy-A4/blob/master/create-jenkins-job.groovy][create-jenkins-job.groovy]] |
| Manage jenkins jobs | [[https://github.com/dennyzhang/cheatsheet-jenkins-groovy-A4/blob/master/manage-jenkins-jobs.groovy][manage-jenkins-jobs.groovy]] |
| Cancel queued jenkins jobs by regexp | [[https://github.com/dennyzhang/cheatsheet-jenkins-groovy-A4/blob/master/kill-queued-jenkins.groovy][kill-queued-jenkins.groovy]] |
| Name | Comment |
|-----------------------------------------+---------------------------------------------------------------------------|
| List all my jenkins jobs | =println Jenkins.instance.projects.collect { it.name }= |
| List all jenkins jobs | [[https://github.com/dennyzhang/cheatsheet-jenkins-groovy-A4/blob/master/list-all-jobs.groovy][list-all-jobs.groovy]] |
| Create and trigger a job | [[https://github.com/dennyzhang/cheatsheet-jenkins-groovy-A4/blob/master/create-jenkins-job.groovy][create-jenkins-job.groovy]] |
| Manage jenkins jobs | [[https://github.com/dennyzhang/cheatsheet-jenkins-groovy-A4/blob/master/manage-jenkins-jobs.groovy][manage-jenkins-jobs.groovy]] |
| Cancel queued jenkins jobs by regexp | [[https://github.com/dennyzhang/cheatsheet-jenkins-groovy-A4/blob/master/kill-queued-jenkins.groovy][kill-queued-jenkins.groovy]] |
** Jenkins Security Via Groovy
| Name | Comment |
|------------------------------------------+----------------------------------------|
Expand All @@ -69,7 +77,6 @@ File me [[https://github.com/dennyzhang/cheatsheet-jenkins-groovy-A4/issues][Iss
| Create a jenkins secret text | [[https://github.com/dennyzhang/cheatsheet-jenkins-groovy-A4/blob/master/create-secret-text.groovy][create-secret-text.groovy]] |
| Configure authorization in Jenkins | [[https://github.com/dennyzhang/cheatsheet-jenkins-groovy-A4/blob/master/matrix-authorization-strategy.groovy][matrix-authorization-strategy.groovy]] |
| [[https://stackoverflow.com/questions/35960883/how-to-unlock-jenkins][Jenkins skip wizzard when initialization]] | -Djenkins.install.runSetupWizard=false |

** Load Jenkins settings via folder copy
| Name | Comment |
|--------------------------------------+--------------------------------------------------------------------------|
Expand Down Expand Up @@ -148,7 +155,9 @@ File me [[https://github.com/dennyzhang/cheatsheet-jenkins-groovy-A4/issues][Iss
** Groovy File
| Name | Comment |
|---------------------------------+------------------------------------------------------------------------|
| [[https://stackoverflow.com/questions/7729302/how-to-read-a-file-in-groovy-into-a-string][Read file into a string]] | =String fileContents = new File('/tmp/test.txt).text= |
| Read file content as a variable | =def env = System.getenv()=, =def content = readFile("/tmp/test.txt")= |
| [[https://jenkins.io/doc/pipeline/examples/][Write file in pipeline]] | =writeFile file: "output/my.txt", text: "This is a test"= |
| Read and write json files | [[https://github.com/dennyzhang/cheatsheet-jenkins-groovy-A4/blob/master/json-file.groovy][json-file.groovy]] |
| Obtain a relative path | [[https://github.com/dennyzhang/cheatsheet-jenkins-groovy-A4/blob/master/json-file.groovy][relative-path.groovy]] |
** Groovy Dictionary
Expand Down
8 changes: 7 additions & 1 deletion addjobstoview-byregexp.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// Link: https://cheatsheet.dennyzhang.com/cheatsheet-jenkins-groovy-a4
// --
// Created : <2018-04-20>
// Updated: Time-stamp: <2019-05-01 16:51:38>
// Updated: Time-stamp: <2019-05-23 14:37:35>
//-------------------------------------------------------------------
// imports
import jenkins.model.Jenkins
Expand All @@ -27,3 +27,9 @@ for (item in Jenkins.instance.projects.collect()) {
myView.doAddJobToView(item.name)
}
}

// Add by regexp
viewName = 'Integration'
jenkins.addView(new ListView(viewName))
myView = hudson.model.Hudson.instance.getView(viewName)
myView.setIncludeRegex(".*Integration.*")
35 changes: 35 additions & 0 deletions build-monitor-views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<views>
<hudson.model.AllView>
<owner class="hudson" reference="../../.."/>
<name>all</name>
<filterExecutors>false</filterExecutors>
<filterQueue>false</filterQueue>
<properties class="hudson.model.View$PropertyList"/>
</hudson.model.AllView>
<com.smartcodeltd.jenkinsci.plugins.buildmonitor.BuildMonitorView plugin="[email protected]+build.201809061734">
<owner class="hudson" reference="../../.."/>
<name>AutoReport</name>
<filterExecutors>false</filterExecutors>
<filterQueue>false</filterQueue>
<properties class="hudson.model.View$PropertyList"/>
<jobNames>
<comparator class="hudson.util.CaseInsensitiveComparator"/>
<string>DetectTestbedFormatChanges</string>
<string>MonitorDownloadURLs</string>
<string>NightlyProvisionBasicTest</string>
<string>NightlyProvisionGear2Test</string>
<string>NightlyProvisionIntegrationTest</string>
<string>ProvisionPKSEnvNimbus</string>
<string>ProvisionPKSEnvNimbusAdvancew</string>
</jobNames>
<jobFilters/>
<columns/>
<recurse>false</recurse>
<title>AutoReport</title>
<config>
<displayCommitters>false</displayCommitters>
<buildFailureAnalyzerDisplayedField>Name</buildFailureAnalyzerDisplayedField>
<order class="com.smartcodeltd.jenkinsci.plugins.buildmonitor.order.ByName"/>
</config>
</com.smartcodeltd.jenkinsci.plugins.buildmonitor.BuildMonitorView>
</views>
27 changes: 27 additions & 0 deletions validate-kubernetes-cloud.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!groovy
//-------------------------------------------------------------------
// @copyright 2018 DennyZhang.com
// Licensed under MIT
// https://www.dennyzhang.com/wp-content/mit_license.txt
//
// File: jenkins-kubernetes-cloud.groovy
// Author : Denny <https://www.dennyzhang.com/contact>
// Link: https://cheatsheet.dennyzhang.com/cheatsheet-jenkins-groovy-a4
// --
// Created : <2018-04-20>
// Updated: Time-stamp: <2019-05-23 22:30:46>
//-------------------------------------------------------------------
// https://github.com/carlossg/jenkins-kubernetes-plugin/blob/master/src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud.java
import org.csanchez.jenkins.plugins.kubernetes.*
import jenkins.model.*
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientException;
try {
def j = Jenkins.getInstance()
def client = j.clouds[0].connect()
client.pods().list();
} catch (KubernetesClientException e) {
println("Error testing connection %s" + e.getMessage())
} catch (Exception e) {
println("Error testing connection %s" + e.getMessage())
}

0 comments on commit cb6dcbe

Please sign in to comment.