2

I am using BitBucket and I have created a plugin to automatically tag the branch based on some information. Things are working nicely in my local machine and push to remote repository works fine, however, when we push the changes into Bitbucket Pipeline (that builds the project using maven) it push fails with the following error:

org.eclipse.jgit.api.errors.TransportException: git@bitbucket.org:testrepo/test-tagging.git: Auth fail
    at org.eclipse.jgit.api.PushCommand.call(PushCommand.java:165)
    at com.mytest.semver.utils.GitUtils.push(GitUtils.java:150)
    at com.mytest.semver.utils.GitUtils.commitAndPush(GitUtils.java:232)
    at com.mytest.semver.maven.plugin.TagMojo.execute(TagMojo.java:231)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:207)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:307)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:193)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:106)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:863)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:288)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:199)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
Caused by: org.eclipse.jgit.errors.TransportException: git@bitbucket.org:testrepo/test-tagging.git: Auth fail
    at org.eclipse.jgit.transport.JschConfigSessionFactory.getSession(JschConfigSessionFactory.java:159)
    at org.eclipse.jgit.transport.SshTransport.getSession(SshTransport.java:137)
    at org.eclipse.jgit.transport.TransportGitSsh$SshPushConnection.<init>(TransportGitSsh.java:322)
    at org.eclipse.jgit.transport.TransportGitSsh.openPush(TransportGitSsh.java:167)
    at org.eclipse.jgit.transport.PushProcess.execute(PushProcess.java:155)
    at org.eclipse.jgit.transport.Transport.push(Transport.java:1250)
    at org.eclipse.jgit.api.PushCommand.call(PushCommand.java:157)
    ... 25 more
Caused by: com.jcraft.jsch.JSchException: Auth fail
    at com.jcraft.jsch.Session.connect(Session.java:512)
    at org.eclipse.jgit.transport.JschConfigSessionFactory.getSession(JschConfigSessionFactory.java:116)
    ... 31 more

From my understanding, the username you can see in the error log is being picked from the local git files in the server as the build process checks out the branch to compile it:

git@bitbucket.org

And it seems it ignores my configured credentials. This is the code I am using to push the changes (The code in GitUtil in the error log):

Git git = Git.open(Paths.get("")); // This will select the current directory
CredentialsProvider cr = new UsernamePasswordCredentialsProvider("my_username", "my_pass");
RefSpec spec = new RefSpec("refs/remotes/origin/develop"); //we only tag this branch
Iterable<PushResult> rev = git.push()
                .setPushTags()
                .setRefSpecs(spec)
                .setCredentialsProvider(cr)
                .call();

Have I missed anything here? Why is it ignoring the credentials? If I have to set the remote branch and url, how do I get it with JGit, there should be somewhere in the configuration?

xbmono
  • 2,084
  • 2
  • 30
  • 50
  • The difference between your localhost and the Bitbucket Pipelines is the SSH configuration. Without explicit configuration (localhost), `JschConfigSessionFactory` creates the default JSch using files in `~/.ssh`—this cannot be done in Bitbucket. Perhaps this post can help you: [Using Keys with JGit to Access a Git Repository Securely](https://stackoverflow.com/questions/13686643/using-keys-with-jgit-to-access-a-git-repository-securely) – Mincong Huang Apr 27 '18 at 12:18
  • Thanks... But do you know where is the path to private key in Bitbucket? I generated ssh key and added that to my username but no luck. See this page: https://confluence.atlassian.com/bitbucket/set-up-an-ssh-key-728138079.html#SetupanSSHkey-ssh1 – xbmono Apr 30 '18 at 02:05
  • I don't know, unfortunately... – Mincong Huang Apr 30 '18 at 07:16

0 Answers0