github.com/jenkins-x/jx/v2@v2.1.155/pkg/cmd/opts/pipelines.go (about)

     1  package opts
     2  
     3  import (
     4  	"strconv"
     5  	"strings"
     6  
     7  	"github.com/jenkins-x/jx/v2/pkg/builds"
     8  
     9  	v1 "github.com/jenkins-x/jx-api/pkg/apis/jenkins.io/v1"
    10  	"github.com/jenkins-x/jx-logging/pkg/log"
    11  	"github.com/jenkins-x/jx/v2/pkg/gits"
    12  	"github.com/jenkins-x/jx/v2/pkg/kube"
    13  	"github.com/jenkins-x/jx/v2/pkg/util"
    14  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    15  )
    16  
    17  // GetLatestPipelineBuildByCRD returns the latest pipeline build
    18  func (o *CommonOptions) GetLatestPipelineBuildByCRD(pipeline string) (string, error) {
    19  	// lets find the latest build number
    20  	jxClient, ns, err := o.JXClientAndDevNamespace()
    21  	if err != nil {
    22  		return "", err
    23  	}
    24  	pipelines, err := jxClient.JenkinsV1().PipelineActivities(ns).List(metav1.ListOptions{})
    25  	if err != nil {
    26  		return "", err
    27  	}
    28  
    29  	buildNumber := 0
    30  	for _, p := range pipelines.Items {
    31  		if p.Spec.Pipeline == pipeline {
    32  			b := p.Spec.Build
    33  			if b != "" {
    34  				n, err := strconv.Atoi(b)
    35  				if err == nil {
    36  					if n > buildNumber {
    37  						buildNumber = n
    38  					}
    39  				}
    40  			}
    41  		}
    42  	}
    43  	if buildNumber > 0 {
    44  		return strconv.Itoa(buildNumber), nil
    45  	}
    46  	return "1", nil
    47  }
    48  
    49  // GetPipelineName return the pipeline name
    50  func (o *CommonOptions) GetPipelineName(gitInfo *gits.GitRepository, pipeline string, build string, appName string) (string, string) {
    51  	if pipeline == "" {
    52  		pipeline = o.GetJenkinsJobName()
    53  	}
    54  	if build == "" {
    55  		build = builds.GetBuildNumber()
    56  	}
    57  	if gitInfo != nil && pipeline == "" {
    58  		// lets default the pipeline name from the Git repo
    59  		branch, err := o.Git().Branch(".")
    60  		if err != nil {
    61  			log.Logger().Warnf("Could not find the branch name: %s", err)
    62  		}
    63  		if branch == "" {
    64  			branch = "master"
    65  		}
    66  		pipeline = util.UrlJoin(gitInfo.Organisation, gitInfo.Name, branch)
    67  	}
    68  	if pipeline == "" && appName != "" {
    69  		suffix := appName + "/master"
    70  
    71  		// lets try deduce the pipeline name via the app name
    72  		jxClient, ns, err := o.JXClientAndDevNamespace()
    73  		if err == nil {
    74  			pipelineList, err := jxClient.JenkinsV1().PipelineActivities(ns).List(metav1.ListOptions{})
    75  			if err == nil {
    76  				for _, pipelineResource := range pipelineList.Items {
    77  					pipelineName := pipelineResource.Spec.Pipeline
    78  					if strings.HasSuffix(pipelineName, suffix) {
    79  						pipeline = pipelineName
    80  						break
    81  					}
    82  				}
    83  			}
    84  		}
    85  	}
    86  	if pipeline == "" {
    87  		// lets try find
    88  		log.Logger().Warnf("No $JOB_NAME environment variable found so cannot record promotion activities into the PipelineActivity resources in kubernetes")
    89  	} else if build == "" {
    90  		// lets validate and determine the current active pipeline branch
    91  		p, b, err := o.GetLatestPipelineBuild(pipeline)
    92  		if err != nil {
    93  			log.Logger().Warnf("Failed to try detect the current Jenkins pipeline for %s due to %s", pipeline, err)
    94  			build = "1"
    95  		} else {
    96  			pipeline = p
    97  			build = b
    98  		}
    99  	}
   100  	return pipeline, build
   101  }
   102  
   103  // getLatestPipelineBuild for the given pipeline name lets try find the Jenkins Pipeline and the latest build
   104  func (o *CommonOptions) GetLatestPipelineBuild(pipeline string) (string, string, error) {
   105  	log.Logger().Infof("pipeline %s", pipeline)
   106  	build := ""
   107  	jxClient, ns, err := o.JXClientAndDevNamespace()
   108  	if err != nil {
   109  		return pipeline, build, err
   110  	}
   111  	kubeClient, err := o.KubeClient()
   112  	if err != nil {
   113  		return pipeline, build, err
   114  	}
   115  	devEnv, err := kube.GetEnrichedDevEnvironment(kubeClient, jxClient, ns)
   116  	webhookEngine := devEnv.Spec.WebHookEngine
   117  	if webhookEngine == v1.WebHookEngineProw || webhookEngine == v1.WebHookEngineLighthouse {
   118  		return pipeline, build, nil
   119  	}
   120  
   121  	jenkins, err := o.JenkinsClient()
   122  	if err != nil {
   123  		return pipeline, build, err
   124  	}
   125  	paths := strings.Split(pipeline, "/")
   126  	job, err := jenkins.GetJobByPath(paths...)
   127  	if err != nil {
   128  		return pipeline, build, err
   129  	}
   130  	build = strconv.Itoa(job.LastBuild.Number)
   131  	return pipeline, build, nil
   132  }