github.com/bndr/gojenkins@v1.1.0/README.md (about)

     1  # Jenkins API Client for Go
     2  
     3  [![GoDoc](https://godoc.org/github.com/bndr/gojenkins?status.svg)](https://godoc.org/github.com/bndr/gojenkins)
     4  [![Go Report Cart](https://goreportcard.com/badge/github.com/bndr/gojenkins)](https://goreportcard.com/report/github.com/bndr/gojenkins)
     5  [![Build Status](https://travis-ci.org/bndr/gojenkins.svg?branch=master)](https://travis-ci.org/bndr/gojenkins)
     6  
     7  ## About
     8  
     9  Jenkins is the most popular Open Source Continuous Integration system. This Library will help you interact with Jenkins in a more developer-friendly way.
    10  
    11  These are some of the features that are currently implemented:
    12  
    13  * Get information on test-results of completed/failed build
    14  * Ability to query Nodes, and manipulate them. Start, Stop, set Offline.
    15  * Ability to query Jobs, and manipulate them.
    16  * Get Plugins, Builds, Artifacts, Fingerprints
    17  * Validate Fingerprints of Artifacts
    18  * Get Current Queue, Cancel Tasks
    19  * etc. For all methods go to GoDoc Reference.
    20  
    21  ## Installation
    22  
    23      go get github.com/bndr/gojenkins
    24  
    25  ## Usage
    26  
    27  ```go
    28  
    29  import (
    30    "github.com/bndr/gojenkins"
    31    "context"
    32    "time"
    33    "fmt"
    34  )
    35  
    36  ctx := context.Background()
    37  jenkins := gojenkins.CreateJenkins(nil, "http://localhost:8080/", "admin", "admin")
    38  // Provide CA certificate if server is using self-signed certificate
    39  // caCert, _ := ioutil.ReadFile("/tmp/ca.crt")
    40  // jenkins.Requester.CACert = caCert
    41  _, err := jenkins.Init(ctx)
    42  
    43  
    44  if err != nil {
    45    panic("Something Went Wrong")
    46  }
    47  
    48  queueid, err := jenkins.BuildJob(ctx, "#jobname", nil)
    49  if err != nil {
    50    panic(err)
    51  }
    52  build, err := jenkins.GetBuildFromQueueID(ctx, queueid)
    53  if err != nil {
    54    panic(err)
    55  }
    56  
    57  // Wait for build to finish
    58  for build.IsRunning(ctx) {
    59    time.Sleep(5000 * time.Millisecond)
    60    build.Poll(ctx)
    61  }
    62  
    63  fmt.Printf("build number %d with result: %v\n", build.GetBuildNumber(), build.GetResult())
    64  
    65  ```
    66  
    67  API Reference: https://godoc.org/github.com/bndr/gojenkins
    68  
    69  ## Examples
    70  
    71  For all of the examples below first create a jenkins object
    72  ```go
    73  import "github.com/bndr/gojenkins"
    74  
    75  jenkins, _ := gojenkins.CreateJenkins(nil, "http://localhost:8080/", "admin", "admin").Init(ctx)
    76  ```
    77  
    78  or if you don't need authentication:
    79  
    80  ```go
    81  jenkins, _ := gojenkins.CreateJenkins(nil, "http://localhost:8080/").Init(ctx)
    82  ```
    83  
    84  you can also specify your own `http.Client` (for instance, providing your own SSL configurations):
    85  
    86  ```go
    87  client := &http.Client{ ... }
    88  jenkins, := gojenkins.CreateJenkins(client, "http://localhost:8080/").Init(ctx)
    89  ```
    90  
    91  By default, `gojenkins` will use the `http.DefaultClient` if none is passed into the `CreateJenkins()`
    92  function.
    93  
    94  ### Check Status of all nodes
    95  
    96  ```go
    97  nodes := jenkins.GetAllNodes(ctx)
    98  
    99  for _, node := range nodes {
   100  
   101    // Fetch Node Data
   102    node.Poll(ctx)
   103  	if node.IsOnline(ctx) {
   104  		fmt.Println("Node is Online")
   105  	}
   106  }
   107  
   108  ```
   109  
   110  ### Get all Builds for specific Job, and check their status
   111  
   112  ```go
   113  jobName := "someJob"
   114  builds, err := jenkins.GetAllBuildIds(ctx, jobName)
   115  
   116  if err != nil {
   117    panic(err)
   118  }
   119  
   120  for _, build := range builds {
   121    buildId := build.Number
   122    data, err := jenkins.GetBuild(ctx, jobName, buildId)
   123  
   124    if err != nil {
   125      panic(err)
   126    }
   127  
   128  	if "SUCCESS" == data.GetResult(ctx) {
   129  		fmt.Println("This build succeeded")
   130  	}
   131  }
   132  
   133  // Get Last Successful/Failed/Stable Build for a Job
   134  job, err := jenkins.GetJob(ctx, "someJob")
   135  
   136  if err != nil {
   137    panic(err)
   138  }
   139  
   140  job.GetLastSuccessfulBuild(ctx)
   141  job.GetLastStableBuild(ctx)
   142  
   143  ```
   144  
   145  ### Get Current Tasks in Queue, and the reason why they're in the queue
   146  
   147  ```go
   148  
   149  tasks := jenkins.GetQueue(ctx)
   150  
   151  for _, task := range tasks {
   152  	fmt.Println(task.GetWhy(ctx))
   153  }
   154  
   155  ```
   156  
   157  ### Create View and add Jobs to it
   158  
   159  ```go
   160  
   161  view, err := jenkins.CreateView(ctx, "test_view", gojenkins.LIST_VIEW)
   162  
   163  if err != nil {
   164    panic(err)
   165  }
   166  
   167  status, err := view.AddJob(ctx, "jobName")
   168  
   169  if status != nil {
   170    fmt.Println("Job has been added to view")
   171  }
   172  
   173  ```
   174  
   175  ### Create nested Folders and create Jobs in them
   176  
   177  ```go
   178  
   179  // Create parent folder
   180  pFolder, err := jenkins.CreateFolder(ctx, "parentFolder")
   181  if err != nil {
   182    panic(err)
   183  }
   184  
   185  // Create child folder in parent folder
   186  cFolder, err := jenkins.CreateFolder(ctx, "childFolder", pFolder.GetName())
   187  if err != nil {
   188    panic(err)
   189  }
   190  
   191  // Create job in child folder
   192  configString := `<?xml version='1.0' encoding='UTF-8'?>
   193  <project>
   194    <actions/>
   195    <description></description>
   196    <keepDependencies>false</keepDependencies>
   197    <properties/>
   198    <scm class="hudson.scm.NullSCM"/>
   199    <canRoam>true</canRoam>
   200    <disabled>false</disabled>
   201    <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
   202    <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
   203    <triggers class="vector"/>
   204    <concurrentBuild>false</concurrentBuild>
   205    <builders/>
   206    <publishers/>
   207    <buildWrappers/>
   208  </project>`
   209  
   210  job, err := jenkins.CreateJobInFolder(ctx, configString, "jobInFolder", pFolder.GetName(), cFolder.GetName())
   211  if err != nil {
   212    panic(err)
   213  }
   214  
   215  if job != nil {
   216  	fmt.Println("Job has been created in child folder")
   217  }
   218  
   219  ```
   220  
   221  ### Get All Artifacts for a Build and Save them to a folder
   222  
   223  ```go
   224  
   225  job, _ := jenkins.GetJob(ctx, "job")
   226  build, _ := job.GetBuild(ctx, 1)
   227  artifacts := build.GetArtifacts(ctx)
   228  
   229  for _, a := range artifacts {
   230  	a.SaveToDir("/tmp")
   231  }
   232  
   233  ```
   234  
   235  ### To always get fresh data use the .Poll() method
   236  
   237  ```go
   238  
   239  job, _ := jenkins.GetJob(ctx, "job")
   240  job.Poll()
   241  
   242  build, _ := job.getBuild(ctx, 1)
   243  build.Poll()
   244  
   245  ```
   246  
   247  ## Testing
   248  
   249      go test
   250  
   251  ## Contribute
   252  
   253  All Contributions are welcome. The todo list is on the bottom of this README. Feel free to send a pull request.
   254  
   255  ## TODO
   256  
   257  Although the basic features are implemented there are many optional features that are on the todo list.
   258  
   259  * Kerberos Authentication
   260  * CLI Tool
   261  * Rewrite some (all?) iterators with channels
   262  
   263  ## LICENSE
   264  
   265  Apache License 2.0