-
-
Save loopiezlol/e00c35b0166b4eae891ec6b8d610f83c to your computer and use it in GitHub Desktop.
const fs = require('fs') | |
const path = require('path') | |
const process = require('process') | |
const { spawnSync } = require('child_process') | |
const { GITHUB_TOKEN, GITHUB_USERNAME, GITHUB_EMAIL } = process.env | |
// leaving this without https:// in order to reuse it when adding the remote | |
const gitRepositoryURL = 'github.com/owner/repo-name.git' | |
const repositoryName = 'repo-name' | |
function runCommand (commandString, options) { | |
const [command, ...args] = commandString.match(/(".*?")|(\S+)/g) | |
const cmd = spawnSync(command, args, options) | |
// you should probably obfuscate the credentials before logging | |
const errorString = cmd.stderr.toString() | |
if (errorString) { | |
throw new Error( | |
`Git command failed | |
${commandString} | |
${errorString}` | |
) | |
} | |
} | |
exports.handle = async function (event, context, callback) { | |
// install git binary | |
await require('lambda-git')() | |
// change the cwd to /tmp | |
process.chdir('/tmp') | |
// clone the repository and set it as the cwd | |
// sadly, clone doesn't support --porcelain | |
runCommand(`git clone --quiet https://${gitRepositoryURL}`) | |
process.chdir(path.join(process.cwd(), repositoryName)) | |
// make your modifications to the local repository | |
fs.appendFileSync( | |
path.join(process.cwd(), 'README.md'), | |
'\nAutomagically inserted line' | |
) | |
// update local git config with email and username (required) | |
runCommand(`git config --local user.email ${GITHUB_EMAIL}`) | |
runCommand(`git config --local user.name ${GITHUB_USERNAME}`) | |
// stage local files | |
runCommand('git add .') | |
// commit changes | |
runCommand('git commit -m "commit by :robot:"') | |
// replace the remote with an authenticated one | |
runCommand('git remote rm origin') | |
runCommand( | |
`git remote add origin https://${GITHUB_USERNAME}:${GITHUB_TOKEN}@${gitRepositoryURL}` | |
) | |
// push changes to remote | |
runCommand('git push --porcelain --set-upstream origin master') | |
// terminate the lambda | |
callback(null, 'bye') | |
} |
Hi David. I ran into the same issue. This won't work on node10. Need to downgrade to node8.10 (that was the workaround I used)
I just got this email from AWS, this will stop working soon if it requires node 8
We are contacting you as we have identified that your AWS Account currently has one or more Lambda functions using Node.js 8.10, which will reach its EOL at the end of 2019.
What’s happening?
The Node community has decided to end support for Node.js 8.x on December 31, 2019 [1]. From this date forward, Node.js 8.x will stop receiving bug fixes, security updates, and/or performance improvements. To ensure that your new and existing functions run on a supported and secure runtime, language runtimes that have reached their EOL are deprecated in AWS
People this line saved my life (aws lambda + nodjs)
const bug = spawnSync("ln", ["-s", "/usr/lib64/libpcre.so.1.2.0", "/tmp/git/usr/lib64/libpcre.so.0"])
Thanks, I ran into this error, not sure if this is a lambda permission issue or something:
git: error while loading shared libraries: libpcre.so.0