Skip to content

Commit

Permalink
Use @cypress/deploy-bits (#171)
Browse files Browse the repository at this point in the history
* use code from deploy-bits to publish to S3, close #170

* remove obsolete code

* remove this branch from deploy job
  • Loading branch information
bahmutov authored Oct 25, 2017
1 parent 39dfab3 commit 3a8b0af
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 133 deletions.
146 changes: 14 additions & 132 deletions cy_scripts/deploy.js
Original file line number Diff line number Diff line change
@@ -1,61 +1,36 @@
/* eslint-disable no-console */

const _ = require('lodash')
const path = require('path')
const gift = require('gift')
const gulp = require('gulp')
const chalk = require('chalk')
const human = require('human-interval')
const Promise = require('bluebird')
const inquirer = require('inquirer')
const awspublish = require('gulp-awspublish')
const parallelize = require('concurrent-transform')
const minimist = require('minimist')
const debug = require('debug')('deploy')
const questionsRemain = require('@cypress/questions-remain')
const scrape = require('./scrape')
const shouldDeploy = require('./should-deploy')
const { configFromEnvOrJsonFile } = require('@cypress/env-or-json-file')
const R = require('ramda')
const la = require('lazy-ass')
const is = require('check-more-types')
const git = require('ggit')
const {
warnIfNotCI,
getDeployEnvironment,
checkBranchEnvFolder,
uploadToS3,
} = require('@cypress/deploy-bits')

const distDir = path.resolve('public')
const isValidEnvironment = is.oneOf(['production', 'staging'])

// initialize on existing repo
const repo = Promise.promisifyAll(gift(path.resolve('..')))

function getS3Credentials () {
const key = path.join('support', '.aws-credentials.json')
const config = configFromEnvOrJsonFile(key)
if (!config) {
console.error('⛔️ Cannot find AWS credentials')
console.error('Using @cypress/env-or-json-file module')
console.error('and key', key)
throw new Error('AWS config not found')
}
return config
}

function getCurrentBranch () {
return git.branchName()
}

function promptForDeployEnvironment () {
return prompt({
type: 'list',
name: 'strategy',
message: 'Which environment are you deploying?',
choices: [
{ name: 'Staging', value: 'staging' },
{ name: 'Production', value: 'production' },
],
})
.get('strategy')
}

function cliOrAsk (property, ask, minimistOptions) {
// for now isolate the CLI/question logic
const askRemaining = questionsRemain({
Expand All @@ -65,83 +40,6 @@ function cliOrAsk (property, ask, minimistOptions) {
return askRemaining(options).then(R.prop(property))
}

const getDeployEnvironment = R.partial(cliOrAsk,
['environment', promptForDeployEnvironment])

function ensureCleanWorkingDirectory () {
return repo.statusAsync()
.catch((e) => {
console.error('Could not get Git status')
console.error(e)
console.error('assuming clean status')
return { clean: true }
})
.then((status) => {
if (!status.clean) {
console.log(chalk.red('\nUncommited files:'))

_.each(status.files, (props, file) => {
console.log(chalk.red(`- ${file}`))
})

console.log('')

throw new Error('Cannot deploy master to production. You must first commit these above files.')
}
})
}

function getPublisher (bucket, key, secret) {
return awspublish.create({
httpOptions: {
timeout: human('10 minutes'),
},
params: {
Bucket: bucket,
},
accessKeyId: key,
secretAccessKey: secret,
})
}

function publishToS3 (publisher) {
const headers = {}

return new Promise((resolve, reject) => {
const files = path.join(distDir, '**', '*')

return gulp.src(files)
.pipe(parallelize(publisher.publish(headers), 100))

// we dont need to gzip here because cloudflare
// will automatically gzip the content for us
// after its cached at their edge location
// but we should probably gzip the index.html?
// .pipe(awspublish.gzip({ext: '.gz'}))

.pipe(awspublish.reporter())
.on('error', reject)
.on('end', resolve)
})
}

function uploadToS3 (env) {
la(isValidEnvironment(env), 'invalid environment', env)
const bucketName = `bucket-${env}`
return Promise.resolve()
.then(getS3Credentials)
.then((json) => {
la(is.object(json), 'missing S3 credentials object for environment', env)
const bucket = json[bucketName]
la(is.unemptyString(bucket), 'Could not find a bucket for environment', env)

console.log('\n', 'Deploying to:', chalk.green(bucket), '\n')
const publisher = getPublisher(bucket, json.key, json.secret)
return publisher
})
.then(publishToS3)
}

function prompt (questions) {
return Promise.resolve(inquirer.prompt(questions))
}
Expand Down Expand Up @@ -213,25 +111,6 @@ function deployEnvironmentBranch (env, branch) {
la(is.unemptyString(branch), 'missing branch to deploy', branch)
la(isValidEnvironment(env), 'invalid deploy environment', env)

const cleanup = () => {
console.log('Target environment:', chalk.green(env))
console.log('On branch:', chalk.green(branch), '\n')
if (env === 'staging') {
return env
}

if (env === 'production') {
if (branch !== 'master') {
throw new Error('Cannot deploy master to production. You are not on the \'master\' branch.')
}

return ensureCleanWorkingDirectory()
} else {
throw new Error(`Unknown environment: ${env}`)
}
}

const uploadEnvToS3 = _.partial(uploadToS3, env)
const maybeCommit = () =>
commitMessage(env, branch)
.catch((err) => {
Expand All @@ -240,9 +119,9 @@ function deployEnvironmentBranch (env, branch) {
console.error(err.message)
})

return Promise.resolve()
.then(cleanup)
.then(uploadEnvToS3)
checkBranchEnvFolder(branch)(env)

uploadToS3(distDir, env)
.then(maybeCommit)
.then(() => scrapeDocs(env, branch))
.then(() => {
Expand All @@ -257,17 +136,20 @@ function doDeploy (env) {
debug('getting current branch')
return getCurrentBranch()
.then((branch) => {
console.log('deploying branch %s to %s', branch, env)
console.log('deploying branch %s to environment %s',
chalk.green(branch), chalk.blue(env))
la(is.unemptyString(branch), 'invalid branch name', branch)
return deployEnvironmentBranch(env, branch)
})
}

function deploy () {
console.log()
console.log(chalk.yellow('Cypress Docs Deployinator'))
console.log(chalk.yellow('Cypress Docs Deploy'))
console.log(chalk.yellow('==============================\n'))

warnIfNotCI()

return getDeployEnvironment()
.then((env) => {
return shouldDeploy(env)
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"deps": "deps-ok && dependency-check . --no-default-entries --entry cy_scripts/deploy.js",
"prebuild": "npm run clean && npm run deps && gulp pre:build",
"build": "node --stack-size=8192 ./index.js generate",
"postbuild": "gulp post:build && git-last -f public/build.json",
"postbuild": "gulp post:build && git-last -m -f public/build.json",
"clean": "hexo clean",
"clean-deps": "rm -rf node_modules",
"predeploy": "NODE_ENV=production npm run build",
Expand All @@ -37,6 +37,7 @@
"postlint": "eslint --fix *.js cy_scripts/*.js scripts/*.js"
},
"devDependencies": {
"@cypress/deploy-bits": "1.6.0",
"@cypress/env-or-json-file": "^1.0.0",
"@cypress/questions-remain": "^1.0.1",
"beeper": "^1.1.1",
Expand Down

0 comments on commit 3a8b0af

Please sign in to comment.