Skip to content

Instantly share code, notes, and snippets.

@loopiezlol
Created April 10, 2018 14:12
Show Gist options
  • Save loopiezlol/e00c35b0166b4eae891ec6b8d610f83c to your computer and use it in GitHub Desktop.
Save loopiezlol/e00c35b0166b4eae891ec6b8d610f83c to your computer and use it in GitHub Desktop.
flow showing off how to clone, update and push to a git repo from an AWS lambda function
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')
}
@david-j-davis
Copy link

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

@dorian-coygo
Copy link

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)

@david-j-davis
Copy link

david-j-davis commented Oct 18, 2019 via email

@evan-coygo
Copy link

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

@yefriddavid
Copy link

yefriddavid commented Mar 16, 2020

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"])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment