How to write a post-receive script for bit bucket

Posted . Visible to the public.

The post script is useful when execute or trigger any action after pushing the code in repository

Following example shows how to push the code to s3 bucket after pushing the code.

#!/bin/bash
set -x #echo on

tag_push_trigger()
{    
    masterTag=$(git for-each-ref | grep $newrev | grep refs/heads/master )
    
    if [[ -z "$masterTag" ]]; then
      echo " Tag is not created for master branch "       
    else
      tag_create_zipfile  
    fi
}


tag_create_zipfile()
{
    export AWS_ACCESS_KEY_ID=xxxxxxxxxxxxxxxxxxxxxx
    export AWS_SECRET_ACCESS_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    export AWS_DEFAULT_REGION=ap-southeast-2

    echo " create zip file of tag "
    # Get the latest tag
    tag=$(git describe --tags --abbrev=0)
    # Create a zip file out of the latest tag release
    # proDir="codepool"
    proDir="project"
    
    git archive $tag --format=zip --output="./$proDir.zip"
    # move the zip file to s3 bucket
    aws s3 cp "$proDir.zip" s3://nestarter-example-com-state-store
    rm "$proDir.zip"
}

uat_push_trigger()
{
    export AWS_ACCESS_KEY_ID=xxxxxxxxxxxxxxxxxxxxxx
    export AWS_SECRET_ACCESS_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    export AWS_DEFAULT_REGION=ap-southeast-2

    echo " create zip file of tag "
    
    # Create a zip file out of the latest tag release
    # proDir="codepool"
    proDir="project"
    
    git archive release-UAT --format=zip --output="./$proDir.zip"
    # move the zip file to s3 bucket
    aws s3 cp "$proDir.zip" s3://xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    rm "$proDir.zip"
}

while read oldrev newrev ref
do
  echo "The new rev is $newrev "
  echo "The old rev is $oldrev "
  echo "The ref is $ref "

  if [[ "$ref" == refs/tags/* ]]; then
    tag_push_trigger $oldrev $newrev $ref    
  elif [[ "$ref" == refs/heads/release-UAT ]]; then
    uat_push_trigger
  else
    echo " Could not trigger deployment"
  fi            
	
done
vasan
Last edit
vasan
Posted by vasan to vasan's deck (2018-12-03 05:49)