Google Apps Script that can talk to the GitHub API
This is where the "Architecture" really shines. We are building a Serverless Pipeline.
To make this work, the Apps Script needs to "talk" to GitHub using a Personal Access Token (PAT). This script will act as the bridge, taking the binary data from Google Drive and committing it to your GitHub repository via the REST API.Prerequisites (Do these first)
- Generate a GitHub PAT: Go to GitHub Settings → Developer Settings → Personal Access Tokens (Fine-grained). Give it "Contents" Read & Write access to your specific repo.
- AppSheet Folder: Identify the folder in Google Drive where AppSheet saves images (usually
/AppSheet/data/AppName_Images/).
The Apps Script: driveToGitHub.gs
Copy this into your Google Apps Script editor. This function will be triggered by your AppSheet Automation webhook.
JavaScript
/**
* Triggered by AppSheet Webhook to push image to GitHub
*/
function pushImageToGitHub(e) {
// 1. Configuration - Replace these with your actual details
const GITHUB_TOKEN = "YOUR_GITHUB_TOKEN"; // Store in Script Properties for better security
const REPO_OWNER = "YOUR_GITHUB_USERNAME";
const REPO_NAME = "github-repo";
const BRANCH = "main";
const TARGET_PATH = "images/raw/"; // Where the raw JPG goes
try {
// 2. Get data from AppSheet Webhook (e.g., the filename)
const data = JSON.parse(e.postData.contents);
const fileName = data.ImageName; // The column in AppSheet holding the image path
// 3. Find the file in Google Drive
// Note: AppSheet stores images in a specific folder.
// We search for the file by name.
const files = DriveApp.getFilesByName(fileName);
if (files.hasNext()) {
const file = files.next();
const blob = file.getBlob();
const base64Content = Utilities.base64Encode(blob.getBytes());
// 4. Prepare GitHub API Request
const url = `https://api.github.com/repos/${REPO_OWNER}/${REPO_NAME}/contents/${TARGET_PATH}${fileName}`;
const payload = {
"message": `Upload raw image via AppSheet: ${fileName}`,
"content": base64Content,
"branch": BRANCH
};
const options = {
"method": "PUT",
"headers": {
"Authorization": `Bearer ${GITHUB_TOKEN}`,
"Accept": "application/vnd.github.v3+json"
},
"contentType": "application/json",
"payload": JSON.stringify(payload),
"muteHttpExceptions": true
};
// 5. Execute the Push
const response = UrlFetchApp.fetch(url, options);
console.log("GitHub Response: " + response.getContentText());
return ContentService.createTextOutput("Success: Image Pushed to GitHub");
} else {
return ContentService.createTextOutput("Error: File not found in Drive");
}
} catch (error) {
console.error("Error: " + error.toString());
return ContentService.createTextOutput("Error: " + error.toString());
}
}
// Required to handle the Post request from AppSheet
function doPost(e) {
return pushImageToGitHub(e);
}
How to set up the AppSheet Trigger
- Deploy as Web App: In the Apps Script editor, click Deploy > New Deployment. Select "Web App". Set "Execute as: Me" and "Who has access: Anyone". Copy the Web App URL.
- AppSheet Automation: * Go to Automation > Bots in AppSheet.
- Create a bot triggered on Adds Only for your data table.
- Add a step: Run a Webhook.
- Url: Paste your Apps Script Web App URL.
- HTTP Verb: POST
- HTTP Content Type: JSON
- Body: Send the column name containing the image (e.g.,
{"ImageName": "<<[ImageColumn]>>"}).
Next Step Suggestion
Once you get this script to successfully push a .jpg to your GitHub /images/raw/ folder, the next step is to write the GitHub Action (YAML).

Post a Comment