Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clone repos under directory with same name as repo by default #776

Open
tudortimi opened this issue Oct 23, 2023 · 6 comments
Open

Clone repos under directory with same name as repo by default #776

tudortimi opened this issue Oct 23, 2023 · 6 comments

Comments

@tudortimi
Copy link

What feature do you want to see added?

The plugin generally creates a workspace based on the name of the Bitbucket project, repo and branch names. This means that a repo called some-repo will not be cloned under a directory called some-repo (as a git clone would do), but under something like some-project_some-repo_some-branch. This can cause issues with tools that use the directory name (e.g. Gradle uses the directory name as a project name).

It's clear that all three names have to be used in order to generate unique directory names. It would be great if the plugin would have the option of creating an extra directory where the code is clone: some-project_some-repo_some-branch/some-repo.

Upstream changes

No response

@tudortimi
Copy link
Author

This might already be possible and just needs some documentation showing how to do it. I tried to configure this behavior, but couldn't really do it.

The first impediment is that it's not easy to get the repo name. I had to do the following to extract it: scm.getUserRemoteConfigs()[0].getUrl().tokenize('/').last().split('\\.')[0]. (Taken from StackOverflow.)

I tried to use customWorkspace to create a new directory with the repo name:

pipeline {
    agent {
        node {
            label ''
            customWorkspace repoName
        }
    }
}

This ended up just creating a directory called some-repo directly under the node's workspace root. I can't find a way to get the "existing" workspace path (the one based on the project, repo and branch names) and append to that.

@KalleOlaviNiemitalo
Copy link
Contributor

Git plugin documentation suggests this can be achieved with dir. I did not try, but perhaps it would go like this:

pipeline {
    agent any
    options {
      skipDefaultCheckout true
    }    
    stages {
        stage('Checkout') {
            steps {
                dir('subdir') {
                    checkout scm
                }
            }
        }
    }
}

would have the option of creating an extra directory

That should not be the default behavior, because longer directory paths have a higher risk of exceeding the MAX_PATH limit on Windows.

@tudortimi
Copy link
Author

tudortimi commented Oct 23, 2023

dir has the disadvantage that all other steps also have to do dir to change, otherwise they will run in the workspace root. This is why I wanted to change the location of the workspace.

@tudortimi
Copy link
Author

@KalleOlaviNiemitalo Can you point me to the code that sets the value of the workspace directory currently? I don't know whether this is handled by the plugin or whether this is something built into Jenkins itself.

@KalleOlaviNiemitalo
Copy link
Contributor

@tudortimi, I think it's WorkspaceLocatorImpl in branch-api-plugin. That implements the WorkspaceLocator extension point but is not listed on that page because of bug jenkins-infra/backend-extension-indexer#58.

@KalleOlaviNiemitalo
Copy link
Contributor

The WorkspaceLocator extension point is called from Slave.getWorkspaceFor in core, which is called from AbstractBuild.decideWorkspace in core; that one also checks for a custom workspace.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants