samme4life
samme4life

Reputation: 1203

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

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 requirement to move this Jenkinsfile to a 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 provides only the "by Jenkinsfile" option. How can I achieve this?

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

Upvotes: 2

Views: 3523

Answers (3)

samme4life
samme4life

Reputation: 1203

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)
}

Upvotes: 0

RNoB
RNoB

Reputation: 353

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.

Upvotes: 1

Shashank V
Shashank V

Reputation: 11233

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.

Upvotes: 0

Related Questions