Commit 387c86a6 authored by Andy Cobaugh's avatar Andy Cobaugh

ci wait: multiple logical fixes, add flags for project-name, tag, sha, add...

ci wait: multiple logical fixes, add flags for project-name, tag, sha, add more informational output
parent de01ed7a
Pipeline #106215 passed with stage
in 22 seconds
......@@ -42,6 +42,9 @@ type ciWaitBaseURLData struct {
}
func init() {
ciWaitCmd.PersistentFlags().String("project-name", os.Getenv("CI_PROJECT_NAME"), "Name of project (CI_PROJECT_NAME)")
ciWaitCmd.PersistentFlags().String("sha", os.Getenv("CI_COMMIT_SHA"), "Commit SHA (CI_COMMIT_SHA)")
ciWaitCmd.PersistentFlags().String("tag", os.Getenv("CI_COMMIT_TAG"), "Commit tag (CI_COMMIT_TAG)")
ciWaitCmd.PersistentFlags().StringSliceP("urls", "u", ciWaitURLsDefault, "List of URL templates to check in order to find version endpoint")
ciWaitCmd.PersistentFlags().Int("url-max-tries", 10, "Max tries to locate the version endpoint")
ciWaitCmd.PersistentFlags().Duration("url-delay", 10*time.Second, "Time to wait between attempts to locate version endpoint")
......@@ -57,15 +60,14 @@ func init() {
func runCiWaitCmd(cmd *cobra.Command, args []string) error {
start := time.Now()
data := ciWaitBaseURLData{
Project: os.Getenv("CI_PROJECT_NAME"),
Project: viper.GetString("project-name"),
Env: environmentSuffix,
}
newCommit := os.Getenv("CI_COMMIT_SHA")
newVersion := os.Getenv("CI_COMMIT_TAG")
newCommit := viper.GetString("sha")
newVersion := viper.GetString("tag")
var url string
var initialBody []byte
var newBody []byte
hc := &http.Client{Timeout: viper.GetDuration("timeout")}
log.WithFields(log.Fields{
......@@ -138,10 +140,12 @@ func runCiWaitCmd(cmd *cobra.Command, args []string) error {
"delay": viper.GetDuration("update-delay").String(),
}).Infof("Waiting for version update")
cli.Default.Padding = 2 * InitialPadding
// wait for version to update
for i := 1; i <= viper.GetInt("update-max-tries"); i++ {
cli.Default.Padding = InitialPadding
log.WithField("try", i).Infof("Checking URL: %s", url)
cli.Default.Padding = 2 * InitialPadding
// check version url and compare
resp, err := hc.Get(url)
if err != nil {
......@@ -190,8 +194,13 @@ func runCiWaitCmd(cmd *cobra.Command, args []string) error {
} else {
log.Warn("No update to version endpoint")
}
log.Infof("Initial: %s", initialBody)
log.Infof("Current: %s", newBody)
} else {
log.WithFields(log.Fields{"code": resp.StatusCode}).Warn("No OK response")
}
log.WithFields(log.Fields{"code": resp.StatusCode}).Warn("No OK response")
log.Warnf("Sleeping for %s ...", viper.GetDuration("update-delay").String())
time.Sleep(viper.GetDuration("update-delay"))
......@@ -200,8 +209,6 @@ func runCiWaitCmd(cmd *cobra.Command, args []string) error {
log.WithFields(log.Fields{
"url": url,
"old": initialBody,
"new": newBody,
"commit": newCommit,
"version": newVersion,
}).Errorf("Did not observe an update to the version endpoint after %s", time.Since(start).Truncate(time.Millisecond))
......@@ -215,7 +222,6 @@ func runCiWaitCmd(cmd *cobra.Command, args []string) error {
// or 3) if all else fails, newBody is different from oldBody
func ciWaitVersionCompare(initialBody []byte, newBody []byte, commit string, version string) bool {
var nb map[string]interface{}
err := json.Unmarshal(newBody, &nb)
if err != nil {
// unable to unmarshal newBody, do a straight string comparison
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment