github.com/olli-ai/jx/v2@v2.0.400-0.20210921045218-14731b4dd448/pkg/builds/jenkinsfiles.go (about)

     1  package builds
     2  
     3  import (
     4  	"bufio"
     5  	"bytes"
     6  	"fmt"
     7  
     8  	"github.com/olli-ai/jx/v2/pkg/config"
     9  	"github.com/olli-ai/jx/v2/pkg/jenkinsfile"
    10  	corev1 "k8s.io/api/core/v1"
    11  )
    12  
    13  type JenkinsConverter struct {
    14  	Indentation          string
    15  	KubernetesPluginMode bool
    16  	ProjectConfig        *config.ProjectConfig
    17  
    18  	indentCount int
    19  	buffer      bytes.Buffer
    20  	writer      *bufio.Writer
    21  }
    22  
    23  // NewJenkinsConverter creates a new JenkinsConverter instance
    24  func NewJenkinsConverter(projectConfig *config.ProjectConfig) *JenkinsConverter {
    25  	answer := &JenkinsConverter{
    26  		ProjectConfig: projectConfig,
    27  		Indentation:   "  ",
    28  	}
    29  	answer.writer = bufio.NewWriter(&answer.buffer)
    30  	return answer
    31  }
    32  
    33  func (j *JenkinsConverter) ToJenkinsfile() (string, error) {
    34  	projectConfig := j.ProjectConfig
    35  	pack := projectConfig.BuildPack
    36  	j.startBlock("pipeline")
    37  
    38  	j.startBlock("agent")
    39  	if pack != "" {
    40  		j.println(fmt.Sprintf(`agent 'jenkins-%s'`, pack))
    41  	}
    42  	j.endBlock()
    43  
    44  	j.environmentBlock(projectConfig.Env)
    45  
    46  	j.startBlock("stages")
    47  
    48  	pipelines := projectConfig.PipelineConfig
    49  	if pipelines != nil {
    50  		for pipelineKind, pipeline := range pipelines.Pipelines.AllMap() {
    51  			if pipeline == nil {
    52  				continue
    53  			}
    54  			for _, branchBuild := range pipeline.All() {
    55  				build := branchBuild.Lifecycle
    56  				if build == nil {
    57  					continue
    58  				}
    59  
    60  				branchPattern := ""
    61  
    62  				switch pipelineKind {
    63  				case jenkinsfile.PipelineKindRelease:
    64  					branchPattern = "master"
    65  				case jenkinsfile.PipelineKindPullRequest:
    66  					branchPattern = "PR-*"
    67  				case jenkinsfile.PipelineKindFeature:
    68  					branchPattern = "feature*"
    69  				default:
    70  					return "", fmt.Errorf("unknown pipeline kind %s", pipelineKind)
    71  				}
    72  				j.startBlock(fmt.Sprintf(`stage '%s'`, pipelineKind))
    73  
    74  				if branchPattern != "" {
    75  					j.startBlock("when")
    76  					j.println(fmt.Sprintf(`branch '%s'`, branchPattern))
    77  					j.endBlock()
    78  				}
    79  				j.environmentBlock(pipelines.Env)
    80  
    81  				j.startBlock("step")
    82  				j.startContainer()
    83  				for _, step := range build.Steps {
    84  					cmd := step.GetFullCommand()
    85  					j.println(fmt.Sprintf(`sh "%s"`, cmd))
    86  				}
    87  				j.endContainer()
    88  
    89  				j.endBlock()
    90  
    91  				j.endBlock()
    92  			}
    93  		}
    94  	}
    95  
    96  	j.endBlock()
    97  	j.endBlock()
    98  	return j.String(), nil
    99  }
   100  
   101  func (j *JenkinsConverter) startBlock(blockHeader string) {
   102  	j.println(blockHeader + " {")
   103  	j.indentCount++
   104  }
   105  
   106  func (j *JenkinsConverter) endBlock() {
   107  	j.indentCount--
   108  	j.println("}")
   109  }
   110  
   111  func (j *JenkinsConverter) writeIndent() {
   112  	for i := 0; i < j.indentCount; i++ {
   113  		j.writeString(j.Indentation)
   114  	}
   115  }
   116  
   117  func (j *JenkinsConverter) println(text string) {
   118  	j.writeIndent()
   119  	j.writeString(text)
   120  	j.writeString("\n")
   121  }
   122  
   123  func (j *JenkinsConverter) String() string {
   124  	j.writer.Flush() //nolint:errcheck
   125  	return j.buffer.String()
   126  }
   127  
   128  func (j *JenkinsConverter) writeString(text string) {
   129  	j.writer.WriteString(text) //nolint:errcheck
   130  }
   131  
   132  func (j *JenkinsConverter) startContainer() {
   133  	pack := j.ProjectConfig.BuildPack
   134  	if j.KubernetesPluginMode && pack != "" {
   135  		j.startBlock(fmt.Sprintf(`container('%s')`, pack))
   136  	}
   137  }
   138  
   139  func (j *JenkinsConverter) endContainer() {
   140  	pack := j.ProjectConfig.BuildPack
   141  	if j.KubernetesPluginMode && pack != "" {
   142  		j.endBlock()
   143  	}
   144  }
   145  
   146  func (j *JenkinsConverter) environmentBlock(envVars []corev1.EnvVar) {
   147  	if len(envVars) > 0 {
   148  		j.startBlock("environment")
   149  		for _, env := range envVars {
   150  			if env.Value != "" {
   151  				j.println(fmt.Sprintf(`%s = "%s"`, env.Name, env.Value))
   152  			}
   153  		}
   154  		j.endBlock()
   155  	}
   156  
   157  }