redhat-cop / pipeline-library

A repository of Jenkins pipeline files we can reference from elsewhere

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Need a standard method for doing an `oc process | oc apply` via the openshift client plugin

etsauer opened this issue · comments

I believe @syvanen had a way to do this?

@redhat-cop/developer-workflow

This is what I'm currently using in my pipelines:

def applyTemplate(namespace, templateFile, parameterFile) {
  applyTemplates(namespace, templateFile, parameterFile, "", "")
} 

def applyTemplate(namespace, templateFile, parameterFile, cluster_url, cluster_token) {
	openshift.withCluster(cluster_url, cluster_token) {
		openshift.withProject("${namespace}") {
			// Using OC Process type of combination of template and param
			def models = openshift.process( "--filename=${templateFile}", "--param-file=${parameterFile}", "--ignore-unknown-parameters")
			echo "This template contained ${models.size()} objects"
			
                         // Following is avoiding a bug on OCP 3.7
                         // We need to find DeploymentConfigs and the container.spec.image
                         // This copies the value from previous DeploymentConfig and copies it into the
                         // processes template from above 
			for ( o in models ) {
				if (o.kind == "DeploymentConfig") {
					def dcSelector = openshift.selector("deploymentconfig/${o.metadata.name}")
					def foundObjects = dcSelector.exists()
					if (foundObjects) { 
						echo "This DC exists, copying the image value"
						def dcObjs = dcSelector.objects( exportable:true )
						echo "Image now: ${dcObjs[0].spec.template.spec.containers[0].image}"
						o.spec.template.spec.containers[0].image = dcObjs[0].spec.template.spec.containers[0].image
					}
				}
			}
			def created = openshift.apply( models )
			echo "Created: ${created.names()}"
		}
	}
}