Skip to content

Instantly share code, notes, and snippets.

@jfix
Last active May 13, 2019 17:25
Show Gist options
  • Save jfix/9fb7d9e2510d112e83ee49af0fb9e27f to your computer and use it in GitHub Desktop.
Save jfix/9fb7d9e2510d112e83ee49af0fb9e27f to your computer and use it in GitHub Desktop.
/*
This script takes the current status of the develop branch
and merges it automatically with the master branch.
It will ignore any modifications in the master branch.
This will trigger a deployment of the master branch to
the live server.
Not everyone has the right to make changes to the master
branch. Which is why we're using a GitHub token for a
generic account.
*/
require('dotenv').config()
const fs = require('fs-extra')
const git = require('simple-git/promise')()
const path = require('path')
const prompts = require('prompts')
const tempDir = require('temp-dir')
// Abort if the .env file doesn't exist or is not correctly filled in
if (!process.env.GITHUB_NAME || !process.env.GITHUB_TOKEN) {
console.log(`\nThe GitHub login and/or token are missing. Do you have an .env file?\n`)
process.exit(1)
}
// Necessary for cloning the remote repository
const masterRepoUrl = `https://${process.env.GITHUB_NAME}:${process.env.GITHUB_TOKEN}@github.com/cis-itn-oecd/t4-eo-digital-report.git`
const masterRepoDir = path.join(tempDir, 't4-eo-digital-report')
console.log(`\n Deleting temp directory first ${masterRepoDir} ...\n`)
fs.removeSync(masterRepoDir)
;
(async () => {
try {
// Display warning and force user to type in a phrase/word
const response = await prompts({
type: 'text',
message: `Do you have committed and pushed all your changes to 'develop'?\n This will push your changes to the Live server!\n To continue type 'Yes!' to confirm or anything else to abort.`,
name: 'value'
})
if (response.value != `Yes!`) {
console.log(`\n -------------------------------------------------`)
console.log(`\n Aborting because input was not 'Yes!'\n`)
process.exit(1)
}
console.log(`\n -------------------------------------------------\n`)
console.log(` Ok, now pushing changes to the branch 'master' ...\n`)
// ==============================================================
await git
// make sure we see something while it works
.outputHandler((command, stdout, stderr) => {
stdout.pipe(process.stdout)
stderr.pipe(process.stderr)
})
// clone repo locally, with default branch 'develop'
.clone(masterRepoUrl, masterRepoDir, ['--branch', 'develop'])
console.log('Now merging ...')
await git.checkout('master')
await git.merge(['-srecursive', '-Xtheirs', 'develop'])
console.log('Now pushing ...')
await git.push('origin', 'master')
} catch(err) {
console.log(`An error occurred: ${err}.`)
}
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment