Pipeline: Build with git step on a specific branch is triggered by a different branch

Article ID:115003945572
2 minute readKnowledge base

Issue

  • I use the git step to build a specific branch develop, set up to build on changes but the build may be triggered on changes on another branch like for example test/develop

Resolution

The git step is meant to be use for real simple use cases. You cannot specify the repository name origin and use for example origin/develop as a branch specifier, you must use develop:

git branch: 'develop', url: 'https://github.example.com/myorg/myproject.git'

If you have set up a build with an SCM Trigger - such as polling or build on changes pushed to GitHub - the Git plugin may have several match for develop, for example origin/develop, origin/test/develop.

For such cases, the checkout step is preferred:

checkout([$class: ‘GitSCM’, branches: [[name: ‘origin/develop’]], userRemoteConfigs: [[url: ‘git@git.example.com:myorg/myproject.git’]]])

If you are changing the step of an existing build, remember that a pipeline needs to be built at least once so that the Git plugin refreshes its metadata (branch being tracked, current revisions, etc …​). In rare cases, the old data may still be tracked by the Git plugin. It is always best to start from a clean workspace and / or a fresh clone. There are different solutions in place can be used to start from a fresh repository and ensure that only the desired branch is tracked.

1) The deleteDir() step can be used before the checkout step to remove the workspace

2) The WipeWorkspace extension can be used in the checkout step to force a re-clone. Here is an example:

```
checkout([$class: 'GitSCM',
    branches: [[name: 'origin/develop']],
    extensions: [[$class: 'WipeWorkspace']],
    userRemoteConfigs: [[url: 'git@git.example.com:myorg/myproject.git']]
])
```

3) Otherwise, create a new Pipeline job.

Tested product/plugin versions

The latest update of this article has been tested/verified with

References