You can use the Bit Bucket REST api, here is some Perl I use to import a repository into bitbucket:
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use File::Basename;
my $numArgs = $#ARGV + 1;
if($numArgs < 2) {
die "Usage: $0 [Bit Bucket Project e.g. FW, BDPE] [repo name] [-d dry run (optional)]";
}
my $bitbucketProject = lc $ARGV[0];
my $repoName = $ARGV[1];
my $dryRun = $ARGV[2];
my %moduleHash;
my $bitBucketServer = "localhost";
my $user = "admin";
my $password = "bitbucket";
print "Bit Bucket Project: $bitbucketProject\n";
print "Repository name: $repoName\n";
sub importRepo {
my $command = sprintf("curl -u %s:%s -X POST -H \"Content-Type: application/json\" -d '{
\"name\": \"%s\",
\"scmId\": \"git\",
\"forkable\": true
\}' http://%s:7990/rest/api/1.0/projects/%s/repos", $user, $password, $repoName, $bitBucketServer, $bitbucketProject);
if ($dryRun) {
print "$command\n";
} else {
print "Doing import\n";
system $command;
}
my $bitbucketUrl = sprintf("ssh://git\@%s:7999/%s/%s.git", $bitBucketServer, lc $bitbucketProject, $repoName);
my $gitCommand = sprintf("cd %s; pwd; git repack -a -d -f; git push %s --mirror", $repoName, $bitbucketUrl);
if ($dryRun) {
print "$gitCommand\n";
} else {
print "Running git\n";
system $gitCommand;
}
}
importRepo();
Then you can wrap around that with a shell script:
#!/bin/bash
BITBUCKETPROJECT=$1
if [ $# -ne 2 ]; then
echo "Usage: $0 [Bit Bucket Project] [Path to repos]"
exit 1;
fi
echo "Bit bucket project: $BITBUCKETPROJECT"
for f in *; do
if [[ -d $f ]]; then
echo $f
./importRepository.pl $BITBUCKETPROJECT $f
fi
done
Assumes that all of your repos have been cloned into the current directory.
https://developer.atlassian.com/static/rest/bitbucket-server/latest/bitbucket-rest.html
Just ignore the username; add your access key as an SSH key credential in Jenkins and put whatever you want in the username field. When you clone a Bitbucket repository using SSH keys you almost always use git
as the username anyway.
For instance, here is an example Pipeline that will check out a repo using an access key with the ID myAccessKey
:
git(credentialsId: 'myAccessKey', url: 'ssh://git@bitbucket.my.org:7999/project/repo.git')
As you should be able to see, it doesn't matter what you put for the username field in your Jenkins SSH credential, because you checkout the repo as the git
user regardless.
Alternatively, put git
for the username when creating your credential.
Best Answer
User groups can no longer be managed using REST API 2.0. There is no available workaround as of this moment.
https://developer.atlassian.com/cloud/bitbucket/deprecation-notice-v1-apis/?_ga=2.181452386.417879656.1531244294-1231042520.1527887148
https://bitbucket.org/site/master/issues/16556/no-api-support-for-groups-in-v2