2

Has anyone had any success connecting things like content builder or cloud pages to GitHub? I have been searching far and wide and have come across this amazing repository that almost does what I need (https://github.com/Bizcuit/vscode-ampscript).

The reason being as our organization grows and things become more complex we would love the ability to track changes and have reviewers before things are published. Most of my experience comes from GitHub and I am struggling to find any source of versioning within SFMC. Any recommendations would be helpful!

1
4

I have been using this approach to host my Cloud Page code in a private GitHub repo. You should create this private repo, along with a personal token, so Cloud Pages can fetch the code while this is still hidden from curious eyes by not being public.

In the Cloud Page itself, you need to define where it should look for the code, using this approach:

<script runat="server">
  Platform.Load('Core', '1.1')

  // Global Variables
  // Change these variables according to your own settings
  var userDetails = {
           username : "stackexchange",
           token    : "c0b6a550b1aaaca1292e11cbe786e56d40b98c49",
           repoName : "mysecretrepo",
           fileName : "code.js"
        }
  // Do not change anything below this section
  // get repo content
  function getPrivateRepoContent(obj){

      //Building the request URL = https://api.github.com/repos/stackexchange/mysecretrepo/contents/code.js
      var resource = 'https://api.github.com/repos/'+ obj.username + '/' + obj.repoName + '/contents/' + obj.fileName

      var req = new Script.Util.HttpRequest(resource);
          req.emptyContentHandling = 0;
          req.retries = 2;
          req.continueOnError = true;
          req.contentType = "application/json; charset=utf-8"
          req.setHeader("Authorization","token " + obj.token);
          req.setHeader("User-Agent", obj.username + '/' + obj.repoName);
          //  This header is very important! It allows to get the file content raw version. With that we can treat the the file as content like a charm :)
          req.setHeader("Accept", "application/vnd.github.v3.raw");
          req.setHeader("Cache-Control", "");
          req.method = "GET";

      var resp = req.send();

      // Assigning the response content to a AMPScript variable
      Platform.Variable.SetValue("@runCode", resp.content)
  }

  //Run the function passing the user settings as parameter
  getPrivateRepoContent(userDetails)
</script>

After the code being executed, you need to reference the @runCode variable, and wrap it in TreatAsContent, so the ampscript etc within it will actually be processed:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Running code from Github private repositories</title>
</head>
 <body>
   <!-- Getting the response from SSJS and treat as a content in the HTML -->
  %%=TreatAsContent(@runCode)=%%

   </body>
</html>

This approach is really useful for development, both in terms of version control, but also in terms of reducing latency, as you don't have to publish the changes in Cloud Pages constantly. You actually don't even have to be logged in to Marketing Cloud for the development to happen.

It is not a bulletproof solution, and I will not recommend it for building content for emails or high volume Cloud Pages, as each time the Cloud Page/Content Block renders, a call is being made to GitHub.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.