jenkinsci / configuration-as-code-plugin

Jenkins Configuration as Code Plugin
https://plugins.jenkins.io/configuration-as-code
MIT License
2.69k stars 718 forks source link

Organization folder Branch-indexing prevent jenkins to run #2063

Closed matanz121 closed 2 years ago

matanz121 commented 2 years ago

Jenkins and plugins versions report

Environment ```text Paste the output here ```

What Operating System are you using (both controller, and any agents involved in the problem)?

We are using EKS 1.21. running Jenkins latest version on K8S with jcasc and helm chart latest version.

Reproduction steps

  1. Deploying jenkins via code - running on k8s.
  2. In the startup of the jenkins pod - looking at the logs we can see branch indexing is start
  3. The more repos we have, the more branches we have - the more time it take to jenkins to be up.
  4. For us it takes around 4 hours!

Expected Results

Run jenkins without try to index branches every time the jenkins pod is going up.

Actual Results

Jenkins is down for around 4 hours until branch indexing is finish.

Anything else?

Hope for a way to avoid branch indexing at startup time

jetersen commented 2 years ago

via JobDSL:

organizationFolder('Some folder name') {
  buildStrategies {
    skipInitialBuildOnFirstBranchIndexing()
  }
}

Also I suggest to clean up branches in repositories or apply branch filters.

On GitHub a sensible filter is to only build main and pull requests.

matanz121 commented 2 years ago

Hi @jetersen - First thanks for your answer. correct me if I'm wrong but "skipInitialBuildOnFirstBranchIndexing" will skip the build that going to be triggered from the scan. Is there an option to avoid the scanning? We have an old jenkins - not running on k8s and not face the scan (branch indexing) at start up time.

jetersen commented 2 years ago

Turn off the period/cron trigger and rely on manual scans and/or events from SCM to trigger branch indexing?

matanz121 commented 2 years ago

@jetersen yes, exactly what I need. trigger builds rely on SCM events. I'm using Jenkins's Helm chart with Jcasc and I have not configured a cron or periodic scan. (unless there's a default even if not configured)

jetersen commented 2 years ago

If your using JobDSL it will trigger a branch indexing.

Using skipInitialBuildOnFirstBranchIndexing should prevent that from happening.

matanz121 commented 2 years ago

We are using jCasc with JobDSL - please correct me if I'm wrong but I attached the code from our template - is that ok? image

jetersen commented 2 years ago

and the configure block?

You can use code blocks instead of screenshots:

Example of code block:

```groovy
organizationFolder('Some folder name') {
  buildStrategies {
    skipInitialBuildOnFirstBranchIndexing()
  }
}

Rendered codeblock:
```groovy
organizationFolder('Some folder name') {
  buildStrategies {
    skipInitialBuildOnFirstBranchIndexing()
  }
}
matanz121 commented 2 years ago

Hi @jetersen, Thanks for your assistance. attached the whole configuration

          - script: >
              organizationFolder('test') {
                buildStrategies {
                  skipInitialBuildOnFirstBranchIndexing()
                }
                description("test jobs")
                displayName('test TDP')
                organizations {
                  bitbucket {
                    serverUrl("https://bitbucket.org")
                    repoOwner("test")
                    credentialsId("test-${name}")
                    traits {
                      webhookRegistrationTrait {
                        mode('ITEM')
                      }
                      submoduleOptionTrait {
                        extension {
                          disableSubmodules(true)
                          recursiveSubmodules(false)
                          trackingSubmodules(false)
                          reference(null)
                          timeout(15)
                          parentCredentials(true)
                          shallow(false)
                        }
                      }
                    }
                  }
                }
                configure { node ->
                    def traits = node / navigators / 'com.cloudbees.jenkins.plugins.bitbucket.BitbucketSCMNavigator' / traits
                    traits << 'com.cloudbees.jenkins.plugins.bitbucket.BranchDiscoveryTrait' {
                        strategyId('3')
                    }
                    traits << 'org.jenkinsci.plugins.bitbucket.filter.ProjectKeyRegexFilterTrait' {
                        regex('(TDP)')
                    }
                    traits << 'jenkins.scm.impl.trait.RegexSCMSourceFilterTrait' {
                        regex('.*')
                    }
                    traits << 'jenkins.plugins.git.traits.CloneOptionTrait' {
                      extension( class: 'hudson.plugins.git.extensions.impl.CloneOption' ) {
                        shallow('false')
                        noTags('false')
                        depth('0')
                        honorRefspec('false')
                      }
                    }
                    traits << 'jenkins.plugins.git.traits.LocalBranchTrait' {
                        localBranch('**')
                    }
                }
              }
jetersen commented 2 years ago

Oh ya... I remember why your not building pull requests on bitbucket cloud.

https://jira.atlassian.com/browse/BCLOUD-5814

I made a comment back when it was 6-7 years old: https://jira.atlassian.com/browse/BCLOUD-5814?focusedCommentId=2213342&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#comment-2213342

My advice. Do not use BitBucket 😆 https://www.tickcounter.com/countup/103550/atlassian-time-to-solve

Hopefully you have enabled bitbucket cloud to delete branches after they have been merged?

Everytime JobDSL runs it triggers a scan because it replaces the entire org folder config and determines it needs to do another scan.

I'd suggest to use skipInitialBuildOnFirstBranchIndexing and you should be fine.

matanz121 commented 2 years ago

Hi @jetersen , Funny to see the issue from 6-7 years ago, unfortunately we running on bitbucket and can't move to github. more over... as a devops team we enabled the deletion of branches after they merged but most of the teams not doing it - so lot lot of branches in each repo.

below you can see the jenkins running after I killed the pod so he start again and made his "magic". From what I can see - the folder scanning was not running - but each repo triggered a scan, maybe is there an option to disable it as well?

image

matanz121 commented 2 years ago

Just to demonstrate how this issue stuck us - when jenkins is going up - couple of hours the UI looks like that - image

jetersen commented 2 years ago

Hmmm again cleanup branches? You can use the plugin to exclude branch of a certain age.

https://github.com/jenkinsci/scm-filter-aged-refs-plugin

Bitbucket cloud offers to cleanup all branches in repository that are merged.

Of course... My approach would be to nuke all branches.

git is a distributed version control system. Let them push their new branches.

Also if they use sourcetree they should disable the option to push all references...

jetersen commented 2 years ago

This is not a JCasC issues this is stubborn bitbucket users who do not want to see the light 😜

GitHub has great integration with Jira Cloud if that is the hard requirement. GitLab also has great integration with Jira Cloud.

matanz121 commented 2 years ago

Hi @jetersen, Thanks a lot for all your assistance! I made it work with the plugin you gave me + the above suggestion. do you know if there an option to use the scm-filter via code? "bitbucket-scm-filter-aged-refs"

jetersen commented 2 years ago

@matanz121 You should be able to visit https://your-jenkins-url.company.com/plugin/job-dsl/api-viewer/index.html to find the DSL for bitbucket-scm-filter-aged-refs Should be available as bitbucketAgedRefsTrait

matanz121 commented 2 years ago

@jetersen Thank you!!! Appreciate it!