go调用jenkins api

go 调用jenkins的api来帮我们实现一部分操作 适用于批量执行同一个任务 亦或者要结合cmdb

package main

import (
	"fmt"
	"github.com/bndr/gojenkins"
	"log"
)

func main() {
	jenkins := gojenkins.CreateJenkins(nil, "url", "admin", "xxx")
	_, err := jenkins.Init()
	if  err != nil {
		fmt.Println(err)
	}
	//连接成功
	fmt.Println("is ok")
	//获取节点状态
	nodes ,_ := jenkins.GetAllNodes()
	for _,node := range nodes {
		node.Poll()
		if ok,_ := node.IsOnline();ok {
			nodename := node.GetName()
			log.Printf("node is %s",nodename)
		}
	}
	//获取任务信息
	jobs ,err := jenkins.GetAllJobNames()
	if err != nil {
		fmt.Println(err)
	}

	for _,job := range  jobs {
		fmt.Println(job.Name,job.Url)
	}
	test := map[string]string{"aa":"/etc","bb":"cc"}
	a,err  := jenkins.BuildJob("test",test)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(a)
	al ,err := jenkins.CopyJob("test","test-all")
	fmt.Println(al)
	if err != nil  {
		fmt.Println(err)
		return
	}
	//aaa ,err := jenkins.GetJob("test",)
	//if err != nil  {
	//	fmt.Println(err)
	//	return
	//}
}

你可能感兴趣的:(GO语言编程)