Jenkins Pipeline - Jenkins file in the repository

I don’t think that having the jenkinsfile in the repository is really a good idea, since when you update it going to trigger the job.
May be have it in a separate repository that the code. Is it even possible ?

Hi @onhelial,
Yes, you can definitely set up Jenkins in a separate repository, but there are pros and cons. If your Jenkins is in a different repo from the application repo, you may need to add a step to clone the application repo in the Jenkinsfile.

1 Like

Hi @raymond.baoly ,
What are the prons and cons?
What is your preference ?
What is the best practice ?

Hi @onhelial,

I’d like to share my experience and thoughts. This is just my perspective, so it might not apply to every case.

Pros of Jenkins in a Separate Repository

  1. Reusability: You can share pipelines across multiple projects. This makes it easier to set up a new project and reuse Jenkinsfiles.
  2. Version Control: Pipeline changes are tracked separately, reducing the risk of accidental edits by developers.
  3. Team Independence: DevOps can manage pipelines without affecting developers. For example, if you’re working with microservices and have 30+ services, you wouldn’t want to clone every single repository just to update the Jenkinsfile.

Cons

  1. Context Loss: It’s harder for developers to debug pipeline issues. Developers may want to check the commands used in the Jenkinsfile to understand how their application is being built. They usually prefer having this information in the same place as their code.
  2. Complexity: Keeping pipelines and code in sync can be tricky. For example, if an application update requires a pipeline change, you’ll need to update both the application and the Jenkins repository. Ensuring the changes work together can be challenging.

Best Practices

The best approach depends on the complexity of your projects:

  • For smaller projects, keeping the Jenkinsfile inside the application repo is simpler.
  • For larger setups with many repositories, consider automating Jenkinsfile updates using scripts. Or using tools like Terraform can help create and manage Jenkins pipeline jobs efficiently.

Hi, I have another question about storing the Jenkinsfile in the repository (Git).

If I have different stages specified in the TEST branch compared to the PROD branch, then when I merge code from TEST into PROD, my Jenkinsfile will be overwritten. What’s the best solution for handling this scenario?

Hi joydeep9932,

You can use a single Jenkinsfile for all environments by using a “when” condition in each stage. For example:

        stage('Deploy to Testing') {
            when {
                branch 'test'
            }
            steps {
                script {
                    echo "Deploying to Testing"
                    // Add deployment steps specific to Testing environment here
                    sh './deploy.sh --env test'
                }
            }
        }
1 Like

Got it! Thanks, buddy!