Missed Team ’24? Catch up on announcements here.

×
Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How to auto pull code from repo to my server ?

thunderclap560
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
March 17, 2016

I want when client push code to repo then repo handle request and pull code to myserver, how to work it ?

2 answers

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

1 vote
Johannes Kilian
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 17, 2016

How I solved this task:

  • Within Bitbucket Server (we use 4.3.2 currently)  I installed the plugin HTTP Request Post-Receive Hook for BitBucket Server
  • Configured the plugin with the following POST Url:

    http://myMachine:8002/pushed?useremail=${user.email}&$projectname={project.name}&projectkey=${project.key}&repositoryname=${repository.name}&repositoryslug=${repository.slug}
  • On my local machine (myMachine), where I want the code pulled to, I implemented a simple web-server, which listens on port 8002 (see above). This webserver waits for receiving the request above. If the webserver gets the request, it automatically pulls the repository from Bitbucket-Server.
    It's a really tiny web-server, scripted in Perl using Dancer (I shortened it a little (removed output - but it still should work)

    use Dancer2;
    use Path::Tiny;
    use Git::Repository;
    set serializer => 'JSON';
    set port       => 8002;
    set logger     => "Console::Colored";
    set log        => 'debug';
    get '/' => sub{
        return {message => "Server to perform forced pull of local repository from BITBUCKET"};
    };
    post '/pushed' => sub {
        my $p;
        my $icnt = 0;
        my %p = params;
        foreach my $currItem (keys(%p)){
            $icnt++;
            $p  = "?" if $icnt==1;
            $p .= "&" if $icnt>1;
            $p .= $currItem."=".$p{$currItem}    
        }
        debug (request->path.$p);
        my $email = params->{useremail}?params->{useremail}:"UNKNOWN";
        my $pn = params->{projectname}?params->{projectname}:"UNKNOWN";
        my $pk = params->{projectkey}?params->{projectkey}:"UNKNOWN";
        my $rn = params->{repositoryname}?params->{repositoryname}:"UNKNOWN";
        my $rs = params->{repositoryslug}?params->{repositoryslug}:"UNKNOWN";
            
        if ($pk =~ m{myDesiredBitbucketProject}ix) {
            if ($rs =~ m{myDesiredRepository}ix) {
                my $path = path("D:\projects\myDesiredRepository");
                my $savePath = Path::Tiny->cwd;
                chdir $path->stringify;
                my $r = Git::Repository->new( work_tree =>  $path->stringify );
                my $cmd = $r->command( 'reset', '--hard' );
                $cmd->close;
                $cmd = $r->command( 'fetch', 'origin' );
                $cmd->close;
                $cmd = $r->command( 'reset', '--hard', 'origin/master' );
                @output = $cmd->stdout->getlines();
                $cmd->close;
                chdir $savePath->stringify;
                return {message => @output};
            }
        }
        return {message => "Pushed to Project <$pn> (<$pk>), Repository <$rn> (<$rs>) by $email"};
    };
    any qr{.*} => sub {
        #status 'not_found';
        #template 'special_404', { path => request->path };
        warning ("Called Invalid route <".request->path.">");
    };
    dance;

 

0 votes
JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 17, 2016

There is a similar approach described here: https://scriptrunner.adaptavist.com/latest/bitbucket/PostReceiveHooks.html#_update_static_site

Except that the update initiated via the BB server.

 

 

 

 

 

TAGS
AUG Leaders

Atlassian Community Events