Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

This page contains examples of provider match and function scripts for some common providers. 

...

Code Block
languagegroovy
themeRDark
titleBuild Project(s) on Push
linenumberstrue
import flexagon.ff.common.core.exceptions.FlexNotFoundException;

def repoName = PAYLOAD.repository.name;
def branch = PAYLOAD.ref - 'refs/heads/';
def projectId = QUERY_PARAMS.projectId;
def streamId;
 
LOG.info("Running GitHub push function: ${repoName}, ${branch}");
 
def logs = GITHUB.getChangeLogs(PAYLOAD);
LOG.info("Building projects for revision: ${logs.getRevision()}");

// if projectId is passed, then we assume one project per repo and build that project
if (projectId) 
{
	def logs = GITHUB.getChangeLogs(PAYLOAD);
    LOG.info(String.format('Building projects for revision: %s',logs.getRevision())) //the query param received is a string, we need a long
  def lProjectId = new Long(projectId);
 
   streamId = FLEXDEPLOY.findStreamIdgetStreamId(projectIdlProjectId, branch);
    FLEXDEPLOY.buildProject(streamId, projectIdlProjectId, logs);
} // otherwise  LOG.setMessage("Submitted project ${lProjectId} for build");
}
// otherwise find and build affected projects
else
{ {  
  // findget theall changedproject filesids for the push
 projects affected by this SCM change
  def logsprojects = GITHUBFLEXDEPLOY.getChangeLogs(PAYLOADfindProjectsForChange(repoName, branch, logs);
 
  if LOG.info(Stringprojects.format('Building projects for revision: %s',logs.getRevision()));

    // get all project ids for projects affected by this SCM change
    def projects = FLEXDEPLOY.findProjectsForChange(repoName, branch, logs);

    if (projects.size() == 0) 
    {size() == 0)
  {
    LOG.info('No projects found for change');
    LOG.setMessage("No projects found for change");
  }
  else
  {
    LOG.info("Building projects : ${projects}");
    // get remaining parameters and build each project
    for (def project in projects)
    {
      streamId = getStreamId(project, branch);
      def requestId LOG= FLEXDEPLOY.infobuildProject('No projects found for change'streamId, project, logs);
    }  LOG.info("Successfully submitted build elserequest ${requestId} for ${project}.");
 {   }
     LOG.info(String.format('Building projects : %s',projects))setMessage("Submitted projects ${projects} for build");
      
 // get}
remaining}
parameters
and//this buildwill eachcreate projectthe stream if it doesnt exist
def   for (def project in projects)
        getStreamId(projectId, branch) {
  def streamId;
  try {
    streamId = FLEXDEPLOY.findStreamId(projectprojectId, branch);
  }
  catch(FlexNotFoundException fnfe) {
    defstreamId requestId = FLEXDEPLOY.buildProjectcreateStream(streamId, project, logsprojectId,branch);
  
         LOG.info("SuccessfullyAdded submittedstream build request ${requestIdbranch} to forproject ${projectprojectId}.");
 
      }
  return  }streamId;
}

Bitbucket

Provider Match - Token

...

This sample Bitbucket function script manages FlexDeploy builds and project streams with the assumption it will be triggered from a Bitbucket push event. Branch creation and deletion all falls under the push event in Bitbucket. This function will create and inactivate FlexDeploy streams as branches are created and deleted, and build all affected projects on a push.

Info

Bitbucket does not send changed files in the push event, but that information is available via the diffstat API. The getChangeLogs function will use that API and as such requires a valid user/password to make the api call. It is recommended that BITBUCKET_USER and BITBUCKET_PASSWORD provider properties are created for passing to this function.


Code Block
languagegroovy
themeRDark
titleBitbucket Push Function Script
linenumberstrue
def repoName = PAYLOAD.repository.full_name;
def branch;
def isNewBranch = PAYLOAD.push.changes[0].old == null;
def isDeletedBranch = PAYLOAD.push.changes[0].new == null;
def streamCache = [:];

if (isDeletedBranch)
{
  branch = PAYLOAD.push.changes[0].old.name;
  LOG.info("Running BitbucketPush function: ${repoName}, ${branch}");

  LOG.info("Branch ${branch} has been deleted. Inactivating all associated streams");

  def logs = BITBUCKET.getChangeLogs(PAYLOAD, BITBUCKET_USER, BITBUCKET_PASSWORD);

  // if projectId is passed, then we assume one project per repo and only that project was affected
  if (QUERY_PARAMS.projectId)
  {
    projects = QUERY_PARAMS.projectId;
  }
  else
  {
    projects = FLEXDEPLOY.findProjectsForChange(repoName, branch, logs);
  }

  for (def project in projects)
  {
    LOG.info("Inactivating stream ${branch} for project ${project}");
    FLEXDEPLOY.inactivateStream(project, branch);
  }
}
else
{
  branch = PAYLOAD.push.changes[0].new.name;
  LOG.info("Running BitbucketPush function: ${repoName}, ${branch}");

  if (isNewBranch)
  {
    streamCache = tryCreateStreams(repoName, branch);
  }

  // find the changed files for the push
  def logs = BITBUCKET.getChangeLogs(PAYLOAD, BITBUCKET_USER, BITBUCKET_PASSWORD);

  LOG.info(String.format("Building projects for revision: %s", logs.getRevision()));

  def projects;

  // if projectId is passed, then we assume one project per repo and build that project
  if (QUERY_PARAMS.projectId)
  {
    projects = QUERY_PARAMS.projectId;
  }
  else
  {
    projects = FLEXDEPLOY.findProjectsForChange(repoName, branch, logs);
  }

  LOG.info(String.format("Building projects : %s", projects));

  if (projects.size() == 0) 
  {
    LOG.info("No projects found for change");
  }
  else 
  {  
    for (def project in projects)
    {
      LOG.info("Building project ${project}");
      
      def streamId = findStreamId(project, branch, streamCache, isNewBranch);
      def requestId = FLEXDEPLOY.buildProject(streamId, project, logs);
      LOG.info("Successfully submitted build request ${requestId} for ${project}.");
    }
  }
}

// if this commit was on a new branch, get from cache, otherwise check on server
def findStreamId(projectId, streamName, streamCache, isNewBranch) 
{
  def streamId = null;
  if (isNewBranch) 
  {
    streamId = streamCache[projectId];
  }
  else 
  {
    streamId = FLEXDEPLOY.findStreamId(projectId, streamName);
  }
  return streamId;
}

// this event is only received once (on creation of a new branch)
def tryCreateStreams(repoName, branch) 
{
  def allProjects = FLEXDEPLOY.findProjectsForChange(repoName, null, null);
  def streams = [:];
  
  for (def project in allProjects)
  {
    LOG.info(String.format("Creating stream %s on project %s", branch, project));
    def streamId = FLEXDEPLOY.createStream(project, branch);
    streams[project] = streamId;
  }
  
  return streams;
}

...