Jenkins CI/CD - Keep the Jenkinsfile in a separte repository which I want to build

2.6k views Asked by At

I have a SpringBoot API application which is configured in a Jenkins pipeline for CI/CD. The process is defined using a Jenkinsfile which currently resides in the root of the source repo. Now there's a requirment to move this Jenkinsfile to ta separate repo from the source repo. Here is an image of the current settings of my pipeline's SCM and Build Configurations.

enter image description here

Note that the Mode dropdown list of the Build Configuration provide only "by Jenkinsfile" option. How can I achieve this? Any answers, ideas or suggestions would be highly appreciated.

Please note that I'm new to Jenkins configurations. Hence, much appreciate if someone can help with code snippet or a gist with examples.

Thanks

3

There are 3 answers

0
Shashank V On

You can set the Repository URL for Jenkinsfile under Advanced Project Options -> Pipeline -> Definition -> SCM -> Repositories -> Repository URL. You can specify a different repository here.

3
RNoB On

All you have to do is create a new Repository that contains the Jenkinsfile. Then you have to change your Job to get the Jenkinsfile from the new Repository.

Then you have to edit your Pipeline to get the sources from the old Repository where the sources are:

checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], gitTool: 'Default', submoduleCfg: [], userRemoteConfigs: [[url: 'ssh://git@git-pull-url']]])

You can generate this peace of code here:

<your.jenkins.url>/jenkins/job/<your-pipeline-job>/pipeline-syntax/

and then select checkout: Check out from version control as Sample Step.

0
samme4life On

I figured out a way to keep the Jenkins pipeline separate from the Jenkinsfile and pass the project specific params from the Jenkisfile

Then the Jenkinsfile looked like this,

#!groovy

def args = [
        appName: 'app-name',
        appSpace: 'app-space',
        jenkinsSlavelabel: 'jenkinsSlavelabel'
]

node('jenkinsSlaveLabel') {
    deleteDir()

    checkout scm

    def jenkinsBuilder = fileLoader.fromGit('groovy-file-name.groovy', 'git-url', 'branch-filter', 'git-repo-credentials-id', 'jenkinsSlavelabel')
    jenkinsBuilder(args)
}