Beispiel #1
0
func (c *BuildCommand) Run(args []string) int {
	var pipelineName string
	var runId int
	flags := c.Meta.FlagSet("client", FlagSetClient)
	flags.Usage = func() { c.Ui.Output(c.Help()) }
	flags.StringVar(&pipelineName, "pipeline", "", "Name of target pipeline")
	flags.IntVar(&runId, "run_id", 0, "Run ID")
	if err := flags.Parse(args); err != nil {
		log.Errorf("Failes to parse flags: %v", err)
		return 1
	}
	var logOutput io.Writer
	if c.Meta.logOutput != "" {
		fi, err := os.OpenFile(c.Meta.logOutput, os.O_CREATE|os.O_WRONLY, 0644)
		if err != nil {
			log.Errorf("Failed to open log output file '%s'. Error: %s\n", c.Meta.logOutput, err)
			return -1
		}
		defer fi.Close()
		logOutput = fi
	} else {
		logOutput = os.Stdout
	}
	util.ConfigureLogging(c.Meta.logLevel, c.Meta.logFormat, logOutput)
	if pipelineName == "" {
		log.Errorf("Must provide a valid pipeline name")
		return 1
	}
	if runId == 0 {
		log.Errorf("Must provide a valid run id")
		return 1
	}
	return build.BuildPipeline(pipelineName, runId)
}
Beispiel #2
0
func (p *Poller) checkGithubMaterial(pipeline structs.Pipeline, material structs.Material) {
	fields := strings.Split(material.URI, "/")
	client := github.NewClient(nil)
	log.Infof("Getting current sha at %s/%s for '%s' pipeline", fields[0], fields[1], pipeline.Name)
	ref, _, err := client.Git.GetRef(fields[0], fields[1], "heads/master")
	if err != nil {
		log.Errorf("Error checking github material ref for %s pipeline. Error %v", pipeline.Name, err)
		return
	}
	var prevSHA []byte
	err1 := p.db.View(func(tx *bolt.Tx) error {
		b := tx.Bucket([]byte("pollingStatus"))
		prevSHA = b.Get([]byte(pipeline.Name))
		return nil
	})
	if err1 != nil {
		log.Errorf("Failed to fetch previous head SHA for pipeline: %s. Error: %v", pipeline.Name, err1)
		return
	}
	if prevSHA == nil || string(prevSHA[:]) != *ref.Object.SHA {
		log.Infof("Current SHA (%s) is different than  previously built SHA(%s). Triggering build", *ref.Object.SHA, string(prevSHA[:]))
		var runId uint64
		err := p.db.Update(func(tx *bolt.Tx) error {
			status := tx.Bucket([]byte("pollingStatus"))
			r := tx.Bucket([]byte("runs"))
			p, e := r.CreateBucketIfNotExists([]byte(pipeline.Name))
			if e != nil {
				log.Errorf("Failed to create pipeline specific run bucket")
				return e
			}
			runId, _ = p.NextSequence()
			return status.Put([]byte(pipeline.Name), []byte(*ref.Object.SHA))
		})
		if err != nil {
			log.Errorf("Failed to store current head SHA for pipeline: %s. Error: %v", pipeline.Name, err)
			return
		}
		exitCode := build.BuildPipeline(pipeline.Name, int(runId))
		log.Infof("Build exit code: %d", exitCode)
		return
	}
	log.Infof("Current SHA (%s) is same as previously built SHA. Skipping build", *ref.Object.SHA)
}