1

I'm trying to configure Jenkins as a code, and trying to put pipeline code in configuration. So far I've found in doc (https://github.com/jenkinsci/job-dsl-plugin/tree/master/docs) that I can do it if I pull the script from Git, but since my pipeline is a simple groovy script, I'm trying to figure out how it can be defined in jobs.jcasc.yaml?

My pipeline looks like that:

pipeline {
    agent {
        kubernetes {
        }
    }
    stages {
        stage("test") {
            steps {
                script {
                    sshagent (credentials: ['ssh-key']) {
                        sh "some code"
                    }
                }
            }
        }
    }
}

The only option I've seen is:

- script: |
    pipelineJob('name') {
      description('build')
      definition {
        cpsScm {
          lightweight(true)
          scm {
            git {
              remote {
                url("URL")
                credentials("key")
              }
              branch("master")
            }
          }
          scriptPath("jenkinsfile")
        }
      }
    }
syncerror
  • 131
  • 1
  • 10

1 Answers1

1

Ok, I've found the way. Looks like script itself can be defined like this:

  pipelineJob('name') {
  definition {
      cps {
        script('''
            pipeline {
                agent {
                    kubernetes {
                    }
                }
                stages {
                    stage("name") {
                        steps {
                            script {
                                sshagent (credentials: ['name']) {
                                    sh "do something"
                                }
                            }
                        }
                    }
                 }
            }
        ''')
        }
  }
}
syncerror
  • 131
  • 1
  • 10