Skip to content
This repository has been archived by the owner on Mar 14, 2024. It is now read-only.

Custom objects

glechev edited this page Jun 30, 2021 · 1 revision

Custom Objects

For complex project, you may need to extract common code from different spec files. You can use Java, Groovy or any other language that runs in the JVM. Java will work out-of-the-box with [Maven generated project](Working with Maven projects), for anything else you'll need to add additional configuration. You can also extend the existing functionality with your own. The example bellow will use Java.

Java code goes into src/main/java folder by Maven convention and we'll be using the com.example package (meaning com/example sub folder).

package com.example;

import java.util.List;

import lombok.Builder;

import com.vmware.devops.model.codestream.Input;
import com.vmware.devops.model.codestream.PipelineTask;
import com.vmware.devops.model.codestream.Stage;

public class TestExtension {
    public static Stage reusableStage() {
        return Stage.builder()
                .name("Stage")
                .tasks(List.of(
                        PipelineTask.builder()
                                .name("Task")
                                .pipeline("pipeline")
                                .build()
                ))
                .build();
    }

    public static class ReusableStageWithBuilder extends Stage {
        @Builder(builderMethodName = "childBuilder")
        public ReusableStageWithBuilder(String param1, String param2) {
            this.setName("Stage-With-Params");
            this.setTasks(List.of(
                    PipelineTask.builder()
                            .name("Task-With-Params")
                            .pipeline("pipeline-with-params")
                            .inputs(List.of(
                                    new Input("param1",
                                            param1),
                                    new Input("param2", param2)
                            ))
                            .build()
            ));
        }
    }
}

In the above, we define 2 reusable Codestream stages with different format.

  • reusableStage is simply a method. This is recommended when your code doesn't require a lot of arguments.
  • ReusableStageWithBuilderis a class implementation that levereges the Lombok builder functionality. This is recommended when you have multiple arguments that you'd like to pass.

This is how you refer the stages in the content files:

import com.example.TestExtension
import com.vmware.devops.model.codestream.Pipeline

return Pipeline.builder()
        .name("pipeline")
        .stages([
                TestExtension.reusableStage(),
                TestExtension.ReusableStageWithBuilder.childBuilder()
                        .param1("Hello")
                        .param2("World")
                        .build()
        ])
        .build()