Jenkins scripted pipeline contains constructs that are not present in the file. Please assist what are these?

Viewed 41

I am new to jenkins and struggling to find out what the below is? The jenkins file for my project has contents similar to the below

stage("Build and push"){
    def projectImage = dockerPushAndBuild {
       Id = "123456"
       image = "abc"
       .
       .
       buildArgs = '-f ./Dockerfile .'
    }
    
    deployTheImage {
       project = 'Testing'
       service = 'MyService'
       .
       .
    }
    
}

Assuming that on 2nd Line (def projectImage), this script is creating an object, what is the "deployTheImage"? The construct "deployTheImage" doesn't have any "sh" or "bash" or any build/push/deploy commands inside. It simply sets some variables as shown in the sample. Also, I can't find anything called "deployTheImage" in the same file.

I am new to jenkins and just looking for some guidance in setting up a scripted go pipeline. Any references or samples will be greatly appreciated.

1 Answers

These seem to be custom steps. There are multiple ways to write custom steps. You can write them as custom plugins or use Jenkins shared Libraries for this. Basically, by using shared libraries you can store your pipeline code in a Git repo and reuse the code in different pipelines. In your case, I'm assuming that you are using global shared libraries. Based on this let me explain what's happening.

Basically, both dockerPushAndBuild and deployTheImage are custom steps, the step dockerPushAndBuild seems to be returning something(Probably the image name after building) after the execution, and the step deployTheImage doesn't seem to be returning anything.

As an example let's consider the first step dockerPushAndBuild.

def projectImage = dockerPushAndBuild {
       Id = "123456"
       image = "abc"
       .
       .
       buildArgs = '-f ./Dockerfile .'
    }

This custom step should reside in your global library and should be added to your pipeline. Then you can call this as shown above. Here the step accepts parameters like Id, image, buildArgs etc. In your shared library, you should see a script called dockerPushAndBuild.groovy in the vars directory. This may have some content similar to the below.

//vars/dockerPushAndBuild.groovy
def call(Closure body) {
    
    def config = [:]
    body.resolveStrategy = Closure.DELEGATE_FIRST
    body.delegate = config
    body()
    // Read your parameters
    println(config.buildArgs)
    
    // Do something
    node {
        echo "${config.buildArgs}"
        sh "docker build .........."
    }
    // return something 
    return "Return a String!!!!"
}

As you can see above, the custom step will perform some tasks and return a string back to the pipeline.

You can read more here.

Related